Download presentation
Presentation is loading. Please wait.
Published byWendy Roberts Modified over 9 years ago
1
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition (loops) do –Sequence prog
2
2 The CONS Cell – List Building Cons –A pair of pointers: the first is the car, and the second is the cdr Atom –Basic lisp entity a symbol, a number (real, rational (ratio integer), float, complex), a vector, an array, a character, a string Everything that is not a cons (defun our-atomp (x) (not (consp x))) List –An ordered collection of atoms or lists (the elements of the list) –A list is either nil or a cons (defun our-listp (x) (or (null x) (consp x))) ab
3
3 Basic List Processing Functions list –takes any number args, returns a list: – (list 'x 'y 'z) => (X Y Z) – (list (list 'x 'y) (list 'x 'y)) => ((X Y) (X Y)) car (or first ) –returns the first element of a list – (car (list 'x 'y)) => X cdr (or rest ) –everything but first element: – (cdr '(a b c)) => (B C) cons –prepends a symbol to a list – (cons (list 'x 'y) (list 'x 'y)) => ((X Y) X Y)
4
4 cons and car (setf x (cons 'a nil)) (A) (setf x (cons (car x) '(b c))) (A B C) a x nil a x bc
5
5 cdr and list (setf y (list 'a (list 'b 'c) 'd)) (A (B C) D) (setf z (car (cdr y))) (B C) (eql z (cdr x)) NIL (equal z (cdr x)) T (eql z (car (cdr y))) T y d nil a z bc
6
6 Predicate Functions listp and null listp –takes one parameter –it returns T if the parameter is a list NIL otherwise null –takes one parameter –it returns T if the parameter is the empty list NIL otherwise –Note that null returns T if the parameter is () ! What is this equivalent to?
7
7 Examples - cons, car, cdr, consp (setf x (cons 'a 'b)) (A. B) (car x) A (cdr x) B (setf y (cons 'a (cons (cons 'b 'c) (cons 'd 'e)))) (A (B. C) D. E) (setf z (car (cdr y))) (B. C) (consp (cdr y)) T (consp (cdr z)) NIL y a b y ad e b c
8
8 List Processing Functions, cont. append –takes any number of lists as arguments –returns them appended together (append '(a b c) '(d e f)) => (A B C D E F) equal –takes two arguments –returns T if they are structurally equal or of equal value
9
9 Sublists (list (list 'blue 'sky) (list 'green 'grass) (list 'brown 'earth)) ((blue sky) (green grass) (brown earth)) sky nil blue grass nil green earth nil brown nil
10
10 Cons Cells and Lists as Trees Binary tree –car as the left subtree, –cdr as the right subtree (setf x '(((a) ((b) (c))) ((d (e)) f))) x c nil b a f e d
11
11 SDRAW program See the structure of anything – sdraw.lisp –available at www2.hawaii.edu/~janst/313/lisp/sdraw.lisp –(from Common Lisp: A Gentle Introduction to Symbolic Computation by David S. Touretzky Benjamin/Cummings Publishing Co., 1990.) Save a copy to your account and try it out
12
12 (sdraw:sdraw '(2 (a b) 3)) > (sdraw:sdraw '(2 (a b) 3)) [*|*]--->[*|*]------------------>[*|*]--->NIL | | | v v v 2 [*|*]--->[*|*]--->NIL 3 | | v v A B
13
13 Predicate Functions – usually end in P Return NIL or something else (True) What type is it? (typep … ) Is it a list? (listp … ) Is it a number? (numberp … ) Is it an integer? (integerp … ) Is it a string? (stringp … ) Is it an atom? (atom … ) Is it nil? (null … ) etc.
14
14 Predicate Functions eq and equal eq takes two symbolic parameters; –returns T if both parameters are atoms and the two are the same e.g., (eq 'a 'a) yields T (eq 'a 'b) yields NIL –Note that if eq is called with list parameters, the result is not reliable –Also eq does not work for numeric atoms equal takes two parameters –Returns T if both parameters “look/print the same” –Works on lists, structures, etc. –Try equal first
15
15 Functions Return a Value Is arg a list? (listp ) – (listp "foo") => NIL Is arg the empty list? (null ) – (null nil) => T Return new list with all args (list * ) – (list 4 5 6) => (4 5 6) Return first item (car ) – (car (list 4 5 6)) => 4 Return new list with everything except the first item (rest) (cdr ) – (cdr (list 4 5 6)) => (5 6) Return new list with arg1 1st, then everything in arg2 (cons ) – (cons 5 (list 4 5 6) ) => (5 4 5 6) These functions do not have side effects
16
16 Function Definition To create a named function, use defun : (defun ( ) ) >(defun sumsq (x y) "Returns sum of X and Y squared." (+ (* x x) (* y y))) SUMSQ The documentation string is saved in the environment and can be recalled with (documentation (quote ) 'function) >(documentation 'sumsq 'function) "Returns sum of X and Y squared."
17
17 Special forms Already used one – defun ! Syntax is the same as function calls ( … ) Special word is one of: -defun, if, let, function, quote, setq, setf, etc. Not evaluated in the same way as functions (i.e. lazy - not eager evaluation) Quiz - What would happen with eager evaluation here: (if (> x 0) (/ 10 x) x) Why can’t eager evaluation be used for if ?
18
18 Special Form - quote quote takes one parameter and the entire expression evaluates to the parameter – (quote (a b c)) => (A B C) – '(a b c) => (A B C) ; alternate syntax quote can’t work under eager evaluation. Why? quote allows us to represent functions as data – (+ 1 2) is a program (computes 3 when evaled) – '(+ 1 2) is data (the list of three elements) – (+ 2 (+ 1 2)) is 5, but – (+ 2 '(+ 1 2)) is an error (why?)
19
19 More Lisp Features Execution flow control – order of code execution Sequence (statement by statement) Selection (conditionals) Iteration (repetition, loops) Declaration –create new variables, functions Assignment –assign values to variables Input/Output –read from the keyboard/files –write to the screen/files
20
20 Iteration Examples (do ((x 0) (y 99 -1)) (= x 100) (setf (aref a x) (aref b y)) (when (= (aref a x) 15) (return))) (dotimes (x 100) (format t "~%Number: ~a" x)) (loop do (setq x (next-leaf my-tree)) (format t "~%This one? ~a" x) while x) (loop while (/= x 3) do (terpri) (princ "Guess Again!") (setq x (read)))
21
21 (defun compute-pi (hex-digit) (print (bbb-pi hex-digit) ) ) (defun bbb-pi (n) (let ((result 0.0)) (dotimes (x n) (incf result (* (expt (/ 1.0 16) x) (- (/ 4.0 (+ 1 (* 8 x))) (/ 2.0 (+ 4 (* 8 x))) (/ 1.0 (+ 5 (* 8 x))) (/ 1.0 (+ 6 (* 8 x))))))) result)) (compute-pi 9) The “Miraculous” Bailey-Borwein-Plouffe (BBP) Pi Algorithm Can find the nth hexadecimal digit of π without knowing digits 0...n-1! Recently computed 10 billionth hexadecimal digit of π (it’s 9) dotimes Example
22
22 Conditional statements - if if special form: – (if ) – (if (= x 0) 0 (/ 10 x)) – Evaluates if true, evaluate if false, evaluate
23
23 Conditional statements - cond cond special form syntax: (cond * ) where is ( * ) Example (cond ((= x 1) (print "x is a small number")) ((>= x 2) (print "x is a larger number")))
24
24 Evaluation of cond (cond ((= x 1) (print "x is a small number")) ((>= x 2) (print "x is a larger number"))) Evaluate test in first clause (= x 1) –If true, execute all other statements in the same clause, –Returns the result of last statement in that clause Else, evaluate the test in the next clause (= x 2) –If true, execute all other statements in the same clause, –Returns result of last statement in that clause Repeat until the first true test or until out of clauses
25
25 Function eval Mostly used in Lisp’s REPL loop eval can be called separately – quote prevents the evaluation > (setf s1 '(cadr '(one two three))) (CADR '(ONE TWO THREE)) > (eval s1) TWO > (eval (list 'cdr (car '((quote (a. b)) c)))) B What is the result of – (eval (quote (list list)))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.