1 / 25

Lisp

Lisp. Functions Built-in functions Defining functions Function Evaluation and Special Forms defun , if Control statements Conditional if , cond Repetition (loops) do Sequence prog. The CONS Cell – List Building. Cons A pair of pointers: the first is the car , and

ismael
Download Presentation

Lisp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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 nilor a cons (defun our-listp (x) (or (null x) (consp x))) a b

  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. cons and car • (setf x (cons 'a nil)) (A) • (setf x (cons (car x) '(b c))) (A B C) x nil a x nil a b c

  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 nil y a d nil z b c

  6. Predicate Functions listp and null • listp • takes one parameter • it returns • Tif the parameter is a list • NILotherwise • 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. 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 b a y e a d c b

  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. Sublists • (list (list 'blue 'sky) (list 'green 'grass) (list 'brown 'earth)) ((blue sky) (green grass) (brown earth)) nil nil nil nil blue green brown sky grass earth

  10. Cons Cells and Lists as Trees • Binary tree • caras the left subtree, • cdras the right subtree • (setf x '(((a) ((b) (c))) ((d (e)) f))) x nil nil a d f nil nil nil b c e

  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. (sdraw:sdraw '(2 (a b) 3)) > (sdraw:sdraw '(2 (a b) 3)) [*|*]--->[*|*]------------------>[*|*]--->NIL | | | v vv 2 [*|*]--->[*|*]--->NIL 3 | | v v A B

  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. Predicate Functions eqandequal • eqtakes 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 eqis called with list parameters, the result is not reliable • Also eqdoes 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. Functions Return a Value • Is arg a list? (listp<arg> ) • (listp "foo") => NIL • Is arg the empty list? (null <arg> ) • (null nil) =>T • Return new list with all args(list <args>* ) • (list 4 5 6) =>(4 5 6) • Return first item (car <arglist> ) • (car (list 4 5 6)) => 4 • Return new list with everything except the first item (rest) (cdr<arglist> ) • (cdr (list 4 5 6)) => (5 6) • Return new list with arg1 1st, then everything in arg2 (cons <item> <inlist> ) • (cons 5 (list 4 5 6) ) => (5 4 5 6) • These functions do not have side effects

  16. Function Definition • To create a named function, use defun: (defun<name> (<param list>) <docu> <forms>) >(defunsumsq (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 <name>) 'function) >(documentation 'sumsq 'function) "Returns sum of X and Y squared."

  17. Special forms • Already used one – defun! • Syntax is the same as function calls (<special word> <arg1> <arg2> …) • 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. 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. 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. 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. dotimes Example 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) (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)

  22. Conditional statements - if • if special form: • (if <test> <then form> <else form> ) • (if (= x 0) 0 (/ 10 x)) • Evaluates <test> • if true, evaluate <then form> • if false, evaluate < else form>

  23. Conditional statements - cond • cond special form syntax: • (cond<cond-clause>*) where <cond-clause> is (<test form> <form>*) • Example • (cond ((= x 1) (print "x is a small number")) ((>= x 2) (print "x is a larger number")))

  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. Function eval • Mostly used in Lisp’s REPL loop • evalcan 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)))

More Related