Common Lisp! John Paxton Montana State University Summer 2003.

Slides:



Advertisements
Similar presentations
Messiaen Quartet for the end of time And another.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Tricks.
Common Lisp! John Paxton Montana State University Summer 2003.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
1 Introduction of Lisp function e.g. (+ 2 2) Shift+Enter Execute …… (+ 1 2) 3 ( ) 15 (- 1 6) -5 ( ) -5 (- ( ) (+
A function (negatives L) that takes a list of numbers L and returns a list containing only negative numbers from L. A recursive example to remind you (defun.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Common Lisp! John Paxton Montana State University Summer 2003.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Common Lisp! John Paxton Montana State University Summer 2003.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
Common Lisp! John Paxton Montana State University Summer 2003.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
Basic Introduction to Lisp
Functional Programming: Lisp MacLennan Chapter 10.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Tail Recursion.
CS 550 Programming Languages Jeremy Johnson
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Racket CSC270 Pepper major portions credited to
CS 270 Math Foundations of CS Jeremy Johnson
Writing LISP functions
Lecture #8 מבוא מורחב.
Modern Programming Languages Lecture 20 Fakhar Lodhi
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Common Lisp II.
Programming Languages
Lisp.
The general format of case is the following: (case <key form>
LISP primitives on sequences
Lists in Lisp and Scheme
Presentation transcript:

Common Lisp! John Paxton Montana State University Summer 2003

Montana Facts State Animal: Grizzly Bear

Montana Facts State Flag

Recursion Tail recursion occurs if the final answer is known when the base case is hit. Most Lisp compilers can detect tail recursion and can translate the tail recursion into an equivalent looping structure.

Rest Recursion (defun my-reverse (alist) (if (null alist) nil (append (my-reverse (rest alist)) (list (first alist))) )

Rest Recursion > (my-reverse '(1 2 3)) (3 2 1) > (trace my-reverse)

Rest Recursion > (my-reverse '(1 2 3)) 1. Trace: (MY-REVERSE '(1 2 3)) 2. Trace: (MY-REVERSE '(2 3)) 3. Trace: (MY-REVERSE '(3)) 4. Trace: (MY-REVERSE 'NIL) 4. Trace: MY-REVERSE ==> NIL 3. Trace: MY-REVERSE ==> (3) 2. Trace: MY-REVERSE ==> (3 2) 1. Trace: MY-REVERSE ==> (3 2 1) (3 2 1)

Tail Recursive Version (defun my-reverse (alist &optional (result nil)) (if (null alist) result (my-reverse (rest alist) (cons (first alist) result) )

Tail Recursive Version > (my-reverse '(1 2 3)) (3 2 1) > (trace my-reverse) > (my-reverse ‘(1 2 3))

Tail Recursive Version 1. Trace: (MY-REVERSE '(1 2 3)) 2. Trace: (MY-REVERSE '(2 3) '(1)) 3. Trace: (MY-REVERSE '(3) '(2 1)) 4. Trace: (MY-REVERSE 'NIL '(3 2 1)) 4. Trace: MY-REVERSE ==> (3 2 1) 3. Trace: MY-REVERSE ==> (3 2 1) 2. Trace: MY-REVERSE ==> (3 2 1) 1. Trace: MY-REVERSE ==> (3 2 1) (3 2 1)

First/Rest Recursion (defun reverse-all (alist) (cond ((null alist) nil) ((atom alist) alist) (t (append (reverse-all (rest alist)) (list (reverse-all (first alist))))) )

First/Rest Recursion > (reverse-all '(((1) (2 3) 4))) ((4 (3 2) (1)))

Question 1.Trace (reverse-all ‘(1 ((2) 3))) by hand.

Optional Parameters > (defun add (n &optional (m 1)) (+ n m)) ADD > (add 10) 11 > (add 10 2) 12

Rest Parameters (defun example (&rest elements) (length elements) ) EXAMPLE > (example) > (example ) > (example ‘(1 2))

Keyword Parameters (defun add (&key (n1 0) (n2 0)) (+ n1 n2)) ADD > (add) > (add :n1 7 :n2 3) > (add :n2 3)

Questions 1.Develop a recursive function called my- count that computes the number of times than an atomic key appears as a top- level element in a list. For example, (my- count 1 ‘(1 2 (1 3) 1)) should return a 2. Trace the function. 2.Repeat the above question but now make the function tail recursive. Trace the function.

Questions 3.Change the function my-count so that it counts all occurrences of the key. For example, (my-count 1 ‘(1 2 (1 3) 1)) should return a 3. Trace the function. 4.Define my-nthcdr, a function that uses a keyword parameter with a default value of 1. Do not use nthcdr in the function body!