280 likes | 323 Views
Learn Common Lisp, a functional programming language, using provided URLs for tutorials. Understand expressions, functions, control structures, and more in Lisp. Examples and syntax explanations included.
E N D
Common lisp A functional programming language. Useful URL: http://www.cs.sfu.ca/CC/310/pwfong/Lisp/ http://www.cs.sfu.ca/CC/310/pwfong/Lisp/1/tutorial1.html In Unix: type lisp How to quit: (quit) Lisp’s working environment: loop read in an expression from the console; evaluate the expression; print the result of evaluation to the console; end loop.
Examples: Note: the prompt of lisp in my system is “*”. 1. Simple test * 1 //my input 1 // lisp output 2. Compute (2+4) you type in: (+ 2 4) * (+ 2 4) //my input 6 // lisp output 3. Compute (2*3 *5) You type in: (* 2 3 5) *(* 2 3 5) //my input 30 // lisp output 4. Compute (2*5+4) * (+(* 2 5) 4) //my input 14 // lisp output 5. Compute (2+4*5-4) * (- (+ 2 (* 4 5)) 4) //my input 18 // lisp output 6a. (- (+ 2 (* 4 )) 4) 6b. (- 2), (- 2 5) 6c. (* 4) 6d. (/ 2)
Common lisp • Expressions: composed of forms. • a function call f(x): (f x). For example, sin(0) is written as (sin 0). • Expressions : case-insensitive. (cos 0)and(COS 0)are interpreted in the same way. • "+" is the name of the addition function that returns the sum of its arguments. • Some functions, like “+” and “*”, could take an arbitrary number of arguments. • A function application form looks like (functionargument1argument2 ... argumentn).
Common lisp • LISP evaluates function calls in applicative order, • -> means that all the argument forms are evaluated before the function is invoked. • e.g. Given ( + (sin 0) (+ 1 5)), • the argument forms (sin 0) and (+ 15) are respectively evaluated to the values 0 and 6 before they are passed as arguments to “+” function. • Numeric values are called self-evaluating forms: they evaluate to themselves. • Some other forms, e.g. conditionals, are not evaluated in applicative order.
Some basic functions + : summation - : subtraction / : division * : multiplication abs : absolute value, e.g. (abs -2) returns 2; (abs 2) returns 2 rem : remainder; e.g. (rem 3 5) returns 3; (rem 7 5) returns 2 min :minimum max :maximum cos :cosine sin :sine
Definition of a function Use defun to define a new function. Examples: 1. Define a function as double(x) = 2*x Input: (defun double (x) (* x 2)) Lisp output: DOUBLE 2. Inline comments Input: (defun triple (x) ‘’compute x times 3 ’’ (* x 3) ) Lisp output: TRIPLE We can use ; then followed with a documentation string. (defun triple (x) ‘’compute x times 3 ’’ ; compute x multiplied by 3 (* x 3) )
Save/Load lisp programs -Edit a lisp program: Use a text editor to edit a lisp program and save it as, for example, helloLisp.lisp -Load a lisp program: (load ‘’helloLisp.lisp’’) -Compile a lisp program: (compile-file ‘’helloLisp.lisp’’) -Load a compileed lisp program (load ‘’helloLisp’’)
Control structures: Recursions and Conditionals (defun factorial ( n ) ‘’compute the factorial of a non-negative integer’’ ( IF (= n 1) 1 ( * n factorial( - n 1) ) ) ) What is the problem? Ternary operator?
Control structures: Recursions and Conditionals • Strict function: evaluate their arguments in applicative order • If is not a strict function. • The if form evaluates the condition (= N 1): • If the condition evaluates to true, then only the second argument is evaluated, and its value is returned as the value of the if form. • If the condition evaluates to false, the third argument is evaluated, and its value is returned. • - short-circuit? • Special forms: Forms that are not strict functions. • The function is recursive. • It involves invocation of itself. • recursion: loop • Linear recursion: may make at most one recursive call from any level of invocation.
Multiple Recursions • Fibonacci numbers: 1, 1, 2, 3, 5, 8, … • ( • defun fibonacci (N) • "Compute the N'th Fibonacci number." • (if (or (zerop N) (= N 1)) 1 • (+ (fibonacci (- N 1)) • (fibonacci (- N 2)) • ) • ) • ) • the function call (zerop N) tests if N is zero. • a shorthand for (= N 0). (zerop returns either T or NIL) • predicate: a boolean function, as indicated by the suffix p. • or: the form is a logical operator. • It evaluates its arguments from left to right, • - returning non-NIL if it encounters an argument • that evaluates to non-NIL. • - It evaluates to NIL if all tests fail. • - For example, in the expression (or t (= 1 1)), • the second argument (= 1 1) will not be evaluated.
Binomial Coefficient The Binomial Coefficient B(n, r) is the coefficient of the term xr in the binormial expansion of (1 + x)n. For example, B(4, 2) = 6 because (1+x)4 = 1 + 4x + 6x2 + 4x3 + x4. The Binomial Coefficient can be computed using the Pascal Triangle formula: Implement a doubly recursive function (binomial NR) that computes the binomial coefficient B(N, R).
Local variable declaration: Let ( let ( (x 1 ) (y 4 ) ) (+ x y) ) That is: (let ( (x 1) (y 4)) (+ x y)) Contrast: let* (let* ( (x 1) (y (* x 2)) ) (+ x y) )
Lists • Lists: containers; supports sequential traversal. • List is also a recursive data structure: its definition is recursive. • Data type: constructors, selectors and recognizers. • Constructors: create new instances of a data type • A list is obtained by evaluating one of the following constructors: • nil: Evaluating nil creates an empty list; • (cons xL): Given a LISP object x and a list L, • evaluating (cons xL) creates a list containing x followed by the elements in L. • Recursive definition: • Example: create a list containing 1 followed by 2. • *(cons 1 (cons 2 nil)) • *(1 2)
Define a list: quote or ` *(quote (2 3 5 7 11 13 17 19)) *(2 3 5 7 11 13 17 19) Or *`(2 3 5 7 11 13 17 19)) *(2 3 5 7 11 13 17 19))
Selectors First:(first L1) returns the first literal in L1 Rest: (rest L1)return L1 without the first literal Last: (last L1) return the last cons structure in L1 Examples: *(first '(2 4 8)) * 2 *(rest (rest (rest '(8)))) * NIL
Recognizers Given a list L - (null L) returns t iff L is nil, - (consp L)returns t iff L is constructed from cons. Examples: *(null nil) *T (null '(1 2 3)) *NIL *(consp nil) *NIL *(consp '(1 2 3)) *T
(defun recursive-list-length (L) "A recursive implementation of list-length.“ ( if (null L) 0 ( 1+ (recursive-list-length (rest L)) ) ) )
What is the purpose of the following function? ( defun list-nth (N L) (if (null L) nil ( if (zerop N) (first L) (list-nth (1- N) (rest L)) ) ) )
If-then-else-if (defun list-nth (n L) "Return the n'th member of a list L." (cond ((null L) nil) ((zerop n) (first L)) (t (list-nth (1- n) (rest L))) ) ) 1. The condition (null L) is evaluated first. If true, then nil is returned. 2. Otherwise, the condition (zerop n) is evaluated. If true, then the value of (first L) is returned. 3. In case neither of the conditions holds, the value of (list-nth (1- n) (rest L)) is returned.
What does the following function do? (defun list-member (E L) "Test if E is a member of L." (cond ((null L) nil) ((eq E (first L)) t) (t (list-member E (rest L))) ) ) Modify the code in order to use “if” instead of cond. Note: member is a built-in function of lisp
In the implementation of list-member, the function call (eq xy) tests if two symbols are the same. (list-member '(a b) '((a a) (a b) (a c))) 0: (LIST-MEMBER (A B) ((A A) (A B) (A C))) 1: (LIST-MEMBER (A B) ((A B) (A C))) 2: (LIST-MEMBER (A B) ((A C))) 3: (LIST-MEMBER (A B) NIL) 3: returned NIL 2: returned NIL 1: returned NIL 0: returned NIL NIL (defun list-member (E L) "Test if E is a member of L." (cond ((null L) nil) ((eq E (first L)) t) (t (list-member E (rest L))) ) )
Example Member: continue… • we would have expected a result of t. • '(a b) does not eq another copy of '(a b) (they are not the same symbol), list-member returns nil. • account for list equivalence, • Use equalfor the list test:
What does the following function do? (defun list-append (L1 L2) "Append L1 by L2." ( if (null L1) L2 (cons (first L1) (list-append (rest L1) L2) ) ) )
Exercises • Member function. • member(e L) checks whether e in a list L or not. Return t if true; otherwise return nil. • Compute x^n, n is a positive integer. • pow( x n ) • Compute the summation of 1^1 + 2^m+3^m+…+n^m, where n and m are positive integers. • sum( n m ) • Counting function • Count the number of times a cons structure e appearing in a cons list L • count ( e L )
Exercises • deletion function. • delete(e L) removes all the cons structure e appearing in a cons list L. • Interleaving function • interlv( L1 L2) creates a new list by arranging the cons structures in L1 and L2 in a interleaving pattern and the first cons structure in the new list is from L1. • For example • interlv( `(1 2 3) `(8 9 7)) • (1 8 2 9 3 7) • interlv( `(1 ) `(8 9 7)) • (1 8 9 7)
Exercises • Set operations • - union • - intersection • - difference • - two sets are equal? • - a member function is required…
Some interesting questions • What is the difference between (1 2 3) and `(1 2 3)? • (1- 5) • (- 1 5) • (1+ 6) • Do we have (1/ 5)?