Section 15.4, 15.6 plus other materials Lisp Lisp = List Processing Developed by John McCarthy in the late 1950’s Oriented toward symbolic (rather than numeric) manipulation Automatic facilities for associating information with symbols Section 15.4, 15.6 plus other materials
How is Lisp different from other languages? Lisp is interpreted (Lisp compilers do exist, but are generally used after the application has been developed) Everything is viewed as a list Programs and data look the same Language provides functions for dealing with lists Functions to put lists together, take lists apart, walk through lists, etc. Section 15.4, 15.6 plus other materials
How we’ll tackle the language Basics Getting into and out of lisp Loading functions Comments Whitespace Etc. Functions provided by the language Writing lisp functions Lisp style Lisp functions that have functions as arguments Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Getting into lisp At student machine prompt type: gcl * Full path is: /usr/local/bin/gcl Lisp is sitting in a read-eval-print loop – lisp reads whatever you type, evaluates it and prints the result Examples Numbers evaluate to themselves Function calls Function for defining a function Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Getting out of lisp At the lisp prompt, type: (quit) or type: (bye) or type: (by) or type: ^D Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Loading code into lisp Use an editor (vi, emacs, pico, etc) to type in your lisp code; file needs to end with a .lsp suffix Get into lisp Load function (load “fact”) Note that .lsp suffix is not specified If there are no syntax errors, lisp responds with T; otherwise NIL Section 15.4, 15.6 plus other materials
Adding comments to your lisp code Single line comments – anything after a semicolon Multiple line comments – anything within double quotes (defun factorial(n) “this function computes the factorial of n” (cond ((zerop n) 1) ; if n is zero, return 1 (t (* n (factorial (- n 1))))) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Lisp “programs” Unlike C++ and Java, there is no main program that acts as the driver Any function can be called at the lisp prompt; any function can be the driver Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Lisp spacing Extra white space is ignored * ( + 3 5 ) * (+ 3 5) * ( + 3 5 ) these all evaluate to 8 Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Lisp data structures Everything is an s-expression (S = symbolic) Two types of s-expressions Atoms – symbols or numeric constants a 3 24 abc Lists – elements in parens (abc e f g) ((abc) c (d e)) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Storing lists Lists in Lisp are stored as a linked list ((a b) c (d e)) c d e a b Functions are also stored as linked lists Section 15.4, 15.6 plus other materials
Lisp case insensitivity These would invoke the same function (factorial 3) (Factorial 3) In addition, the atom a and the atom A are the same (equal ‘a ‘A) returns T Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Evaluation rules Atoms Number – evaluates to itself T or NIL – evaluate to themselves List First element of the list denotes a function name, the remaining elements are its arguments Arguments of the function are usually evaluated in the left to right order and then the function is applied to those values Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Lisp functions Lisp provides a bunch of built-in (primitive) functions Two categories Normal – evaluated as described on previous slide Special – not evaluated the normal way We’ll go through a bunch, but not all, of the Lisp functions Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Quote Function Special function (not a normal function) Don’t evaluate argument Usually abbreviated with a ‘ Examples (quote a) - evaluates to a ((quote (quote a)) – evaluates to (quote a) ‘a – evaluates to a ‘(+ 3 4) – evaluates to (+ 3 4) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Cons function (cons arg1 arg2) Construct a new list whose first element is the value of arg1 and whose remainder is the value of arg2 (puts arg1 as the first element of arg2) Examples (cons ‘a ‘(a b c)) - evaluates to (a a b c) (cons ‘(a b) ‘() ) – evaluates to ((a b)) (cons ‘(a b) NIL) – evaluates to ((a b)) (cons ‘a ‘((b c))) – evaluates to (a (b c)) (cons ‘() ‘(b c)) - evaluates to (NIL b c) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Car function (car list) Returns the first element of the list Examples (car ‘(a b c)) – evaluates to a (car ‘((a b) c)) – evaluates to (a b) (car ‘() ) – evaluates to NIL (car ‘(((a b) c) d)) – evaluates to ((a b) c) (car ‘a) – causes interpreter error Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Cdr function (cdr list) Returns the list with all but the first element Examples (cdr ‘(a b c)) – evaluates to (b c) (cdr ‘((a b) c)) – evaluates to (c) (cdr ‘((a (b c)) (c) (d)) – evaluates to ((c) (d)) Section 15.4, 15.6 plus other materials
Car and cdr compositions (cadr list) (car (cdr list)) (caar list) (car (car list)) (caddr list) (car (cdr (cdr list))) General forms where x is a or d: cxxr cxxxr Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Equivalent functions (first list) (car list) (second list) (cadr list) (third list) (caddr list) (fourth list) (cadddr list) (rest list) (cdr list) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Last function (last list) Returns the last element in a list Returns a list Examples (last ‘((a b) c d e f) – evaluates to (f) (last ‘(a)) - evaluates to (a) (last ‘((a))) – evaluates to ((a)) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Nth function (nth n list) returns the nth element in a list First element in the list is the 0th Examples (nth 0 ‘(a b c)) – evaluates to a (nth 2 ‘(a b c)) – evaluates to c (nth 0 ‘()) – evaluates to NIL (nth 0 ‘(())) – evaluates to NIL (nth 0 ‘((()))) – evaluates to (NIL) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Append function (append arg1 arg2) Combines the elements of arg1 and arg2 into a single list Examples (cons ‘(a b) ‘(c d)) – evaluates to ((a b) c d) (append ‘(a b) ‘(c d)) – evaluates to (a b c d) (cons ‘((a b) c) ‘(d e)) – evaluates to (((a b) c) d e) (append ‘((a b) c) ‘(d e)) –evaluates to ((a b) c d e) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials List function (list arg1 arg2 arg3 … argn) Makes a list with each of its args as elements Examples (append ‘(a b c) ‘()) – evaluates to (a b c) (cons ‘(a b c) ‘()) – evaluates to ((a b c)) (list ‘(a b c) ‘()) – evaluates to ((a b c) ()) (list ‘a ‘b ‘(c d) ‘e) – evaluates to (a b (c d) e) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Length function (length lst) Returns the length of a list Interpreter error if argument is not a list Examples (length ‘()) – evaluates to 0 (length ‘(a)) – evaluates to 1 (length ‘(a b A 1)) – evaluates to 4 (length ‘a) – error Section 15.4, 15.6 plus other materials
Evaluate (by hand) each of the following Lisp S-expressions 3 (car '(a)) (car (a)) (car '((a b) c d)) (car '(+ 3 4)) (caadr '(a (a (a b)))) (cons '(b) '((a) b)) (append '(b) '((a) b)) (list '(b) '((a) b)) (quote (a b)) (quote '(a b)) (length '(a b)) (nth 1 '(a b)) (last '(a b)) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Setf function (setf var sexp) Lisp assignment statement (use sparingly) Binds sexp to the symbol var; binding is global if the setf appears within a function (unless specified local by prog) and also global if setf appears outside a function Setfs outside functions are executed when file loaded Examples (setf num 3) – evaluates to 3 num – evaluates to 3 (setf num ‘(a b)) – evaluates to (a b) num – evaluates to (a b) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Arithmetic functions (+ arg1 arg2 …) (- arg1 arg2 …) (* arg1 arg2 …) (/ arg1 arg2 …) (min arg1 arg2 …) (max arg1 arg2 …) (abs arg) (1+ arg) (1- arg) The arguments must be Integer or Floating Point or else you’ll get an interpreter error Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Predicate functions Predicate function evaluates to either T or NIL We’ll go through some of the most important Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Atom function (atom arg) Returns T if the value of arg is an atom Examples (atom ‘a) – evaluates to T (atom ‘(a)) – evaluates to NIL (setf num 3) – evaluates to 3 (atom num) – evaluates to T (atom 4) – evaluates to T (atom ‘()) – evaluates to T (note NULL list is an atom) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Numberp function (numberp arg) Evaluates to T if the value of arg is a number Examples (numberp 3) – evaluates to T (numberp ‘a) – evaluates to NIL (setf num 3) – evaluates to 3 (numberp num) – evaluates to T Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Symbolp function (symbolp arg) Evaluates to T if the value of arg is a non-numeric atom Examples (setf num 3) – evaluates to 3 (symbolp num) – evaluates to NIL (symbolp 3) – evaluates to NIL (symbolp ‘(a)) – evaluates to NIL (symbolp ‘a) – evaluates to T Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Null function (null arg) Evaluates to T if arg is an empty list, NIL otherwise Examples (null NIL) – evaluates to T (null ‘()) – evaluates to T (null ‘a) – evaluates to NIL (null ‘(a b)) – evaluates to NIL Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Not function (not arg1) Evaluates to T if arg1 is NIL and NIL otherwise Examples (not (symbolp ‘a)) – evaluates to NIL (not (symbolp ‘(a b))) – evaluates to T (not (> 6 3)) – evaluates to NIL Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Listp function (listp arg) Evaluates to T if arg is a list Examples (listp ‘a) – evaluates to NIL (listp 3) – evaluates to NIL (listp ‘()) – evaluates to T (listp ‘(a b c)) – evaluates to T Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Zerop function (zerop arg) Evaluates to T if the value of arg is 0 arg must be an Integer or Floating Point value Examples (setf num 0) – evaluates to 0 (zerop num) – evaluates to T (zerop 0) – evaluates to T (zerop ‘a) – error, bad argument type Section 15.4, 15.6 plus other materials
More predicate functions that expect numeric arguments (plusp arg) – evaluates to t if the value of arg is a positive number (float or integer) (evenp arg) – evaluates to t if the value of arg is an even integer (oddp arg) – evaluates to T if the value of arg is an odd integer Section 15.4, 15.6 plus other materials
More predicate functions that expect numeric arguments (> arg1 arg2 …) – evaluates to T if arg1 is greater than arg2 and arg2 is greater than arg3, etc. (< arg1 arg2 …) – evaluates to T if arg1 is less than arg2 and arg2 is less than arg3, etc. (= arg1 arg2 …) – evaluates to T if arg1 is equal to arg2 and arg2 is equal to arg3, etc. Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Equal function (equal arg1 arg2) Evaluates to T if arg1 and arg2 are the same Examples (equal ‘(a b c) ‘(a b c)) – evaluates to T (equal ‘a ‘a) – evaluates to T (equal 3 3) – evaluates to T (equal 3 ‘a) – evaluates to NIL Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Eql function (eql arg1 arg2) Evaluates to T if arg1 and arg2 are the same symbol or number (atom) Examples (eql ‘a ‘a) – evaluates to T (eql 3 3) – evaluates to T (eql ‘(a b c) ‘(a b c)) – evaluates to NIL Section 15.4, 15.6 plus other materials
Evaluate (by hand) each of the following Lisp S-expressions (- 3 4 1) (1+ 14) (atom 'a) (atom '(a)) (atom '()) (null '()) (listp '()) (listp 'a) (equal 'dog 'dog) (equal '(a b) '(a b)) (eql 'dog 'dog) (eql '(dog) '(dog)) (= 'dog 'dog) (symbolp 'dog) (symbolp 200) (symbolp '(dog)) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Control constructs Allow a condition to be evaluated to decide which expressions to be evaluated Examples if when cond (my favorite) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials If function (if test expr1 expr2) If test is not NIL return the value of expr1 else return the value of expr2 Examples (if (symbolp ‘a) ‘a NIL) – evaluates to a (if (symbolp 3) 3 NIL) – evaluates to NIL Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials When function (when test expr1 expr2 …) If test is not nil, evaluate each expression and return the value of last expression; otherwise return NIL Examples (when (symbolp ‘a) ‘a) – evaluates to a (when (symbolp ‘a) (setf val1 ‘a) (setf val2 ‘b) (setf val3 ‘c)) – evaluates to c and sets all three variables Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Cond function (cond (test1 expr1) (test2 expr2) . (testn exprn)) If test1 is non-NIL, return the value of expr1, else if test2 is non-NIL return the value of expr2 … else if testn is non-NIL return the value of exprn, else return NIL Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Cond Example (defun myMax(arg1 arg2) (cond ( (> arg1 arg2) arg1) ( (> arg2 arg1) arg2) ( t arg1))) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Prog function Allows definition of local variables and sequential execution of code (defun foo(x) (prog (x1 x2 x3) exp1 exp2 . )) x1, x2, x3 are local variables that can be set with setfs; exp1, exp2 … are executed sequentially Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Go function Transfers control to a label Generally use within a prog where you’ll loop based on the value of a local variable Use sparingly (recursion is a better style) (defun fact(x) (prog (res op) (setf op x) (setf res 1) loop (cond((= op 1)(return res))) (setf res (* res op)) (setf op (- op 1)) (go loop))) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Defun function Special function for defining functions Arguments are not evaluated Name of function is returned by defun Format: (defun fun-name (arg1 arg2 .. argn) expr1 expr2 . exprn) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Factorial function (defun factorial(n) (cond ((= n 0) 1) (t (* (factorial (- n 1)) n )))) Section 15.4, 15.6 plus other materials
Evaluation of factorial function 1 The returned values propagate back up to the very first call to factorial (factorial 0) evaluates to 1 thus (factorial 1) evaluates to 1 thus (factorial 2) evaluates to 2 and finally, (factorial 3) evaluates to 6 Section 15.4, 15.6 plus other materials
Writing recursive functions Usually we’ll have a cond that: Checks for base conditions and terminate recursion Make recursive calls to function with arguments closer to the base conditions Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Debugging (trace foo) Shows every call to foo and the values returned by the calls to foo (untrace foo) Turns off tracing Note: some builtin functions can’t be traced Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Our own last Recall: (mylast ‘(a b c d)) returns (d) (mylast ‘(d)) returns (d) Use built-in length function to check for the base case Recurse on the cdr of the list (this is called tail recursion) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Merge function (merge l1 l2) – takes two ordered lists of numbers and returns the two lists merged into one order list (merge ‘(2 3) ‘(3 4)) – evaluates to (2 3 3 4) Need two base cases l1 is empty l2 is empty Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Our own append Recall: (append ‘(a b) ‘(c d)) – evaluates to (a b c d) (append ‘((a b) c) ‘(c d) – evaluates to ((a b) c c d) Note: (append ‘(a) ‘(c d)) is the same as (cons ‘a ‘(c d)) (append ‘() ‘(c d)) is ‘(c d) We can use car to grab the first element of arg1 and recurse on the cdr of arg1 Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Two more predicates (and exp1 exp2 … expn) – returns the value of expn is none of the expressions are NIL, otherwise returns NIL (does short circuit evaluation) (and ‘a ‘b) – evaluates to b (and ‘() ‘a ‘b) evaluates to NIL (or exp1 exp2 … expn) – returns the value of the first non-NIL expression; if all are NIL then it returns NIL (also does short circuit evaluation) (or ‘a ‘b) – evaluates to ‘a (or ‘() ‘a ‘b) – evaluates to ‘a Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Our own Equal function Recall eql – takes two args and returns T if they are both atoms and are equal equal – also works on lists Want to write our own equal using eql (myequal arg1 arg2) Base cases: Arguments are eql One of the args is an atom, but they aren’t eql Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Lisp Style Indent code to reflect the nesting of parens Comment code – each function needs a comment block and each file of functions need a comment block listing the functions, describing the program and how execution is started Avoid the use of local and global variables Most functions will be recursive Functions should be short (< 10 lines) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Mapcar function (mapcar fun lst) Applies fun to successive cars of the lst (one element after the next) Returns a list of the values returned Examples (mapcar ‘car ‘((a b c) (d))) – evaluates to (a d) (mapcar ‘cdr ‘((a b c) (d))) – evaluates to ((b c) ()) (mapcar ‘car ‘((a b c) (d e) (f g) (h))) – evaluates to (a d f h) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Mapcar function Function passed to mapcar can take multiple arguments in which case mapcar would be passed multiple lists (mapcar ‘nth ‘(0 1) ‘((a b) (c d))) – evaluates to (a d) (mapcar ‘append ‘((a b) (c d)) ‘((e f) (g))) – evaluates to ((a b e f) (c d g)) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Eval function (eval arg) Evaluates arg and then evaluates it again Examples (setf x ‘(cons ‘a ‘(b c))) – evaluates to (cons ‘a ‘(b c)) (eval x) – evaluates to (a b c) (eval ‘(+ 3 4)) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Apply function (apply fn arglst) Evaluates first argument and applies it to the value of the argument list Examples (apply ‘+ ‘(1 2 3 4)) – evaluates to 10 (apply ‘nth ‘(0 (a b c))) – evaluates to a (apply ‘cons ‘(a ( b c)))– evaluates to (a b c) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Funcall function (funcall fun arg1 arg2 … argn) Takes a function specification and the args to the function and applies the function to the args Unlike apply, args are not in a list Examples (funcall ‘+ 1 2 3) – evaluates to 6 (funcall ‘cons ‘a ‘(b c)) – evaluates to (a b c) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Lambda function (lambda (arg1 arg2 … argn) exp1 exp2 . expn) Special function for defining a nameless function Section 15.4, 15.6 plus other materials
Calling a lambda function Define them and call them in one statement ((lambda(a) (car a)) ‘(a b)) – evaluates to a ((lambda(a) (and (listp a) (equal (length a) 2))) ‘(a b)) – evaluates to T Embed lambda function within another function (defun list-sqr-add1(lst) (mapcar ’(lambda(num) (+ (* num num) 1)) lst)) (list-sqr-add1 ‘(1 2 3 4)) – evaluates to (2 5 10 17) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Why lambda function? Useful when a function is called only by one other function Often used with a mapcar Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Let’s write an insert Insert takes a number and a sorted list of numbers and returns the list with the number inserted in the appropriate place (insert 5 ‘(2 3 4 7 8)) – evaluates to (2 3 4 5 7 8) Section 15.4, 15.6 plus other materials
Section 15.4, 15.6 plus other materials Let’s write a sort Sort takes a list of numbers and returns that list in sorted ordered (sort ‘(14 2 4 7 3)) – evaluates to (2 3 4 7 14) Sort should use the insert function Think about how insertion sort works – use recursion to recurse down to the base case of insertion sort (an empty list) Next modify sort so that it takes a < or > operator to cause sorting in decreasing or increasing order Section 15.4, 15.6 plus other materials