Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.

Similar presentations


Presentation on theme: "Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has."— Presentation transcript:

1 Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has higher “seniority” LISP: List Processing language LISP: List Processing language (i) Symbolic language: its execution effects can be represented by processing symbols or strings of data (i) Symbolic language: its execution effects can be represented by processing symbols or strings of data (ii) Functional language: its basic components are denotable by mathematical functions (ii) Functional language: its basic components are denotable by mathematical functions (iii) general purpose language: it has evolved to incorporate many “real- world” features (iii) general purpose language: it has evolved to incorporate many “real- world” features (iv) a language for Artificial Intelligence: symbol concept, word (iv) a language for Artificial Intelligence: symbol concept, word 1COSC 2P93 Prolog: Lisp

2 Common Lisp Many Lisp dialects exist (50+ years old!) Many Lisp dialects exist (50+ years old!) Common Lisp is a modern standardized one Common Lisp is a modern standardized one 2COSC 2P93 Prolog: Lisp (from “Common Lisp & Artificial Intelligence P. Harrison, Prentice Hall, 1990)

3 Lisp: history Lisp design borrows from: Lisp design borrows from: Programming languages: Some Fortran, IPL syntax Programming languages: Some Fortran, IPL syntax Math: recursive function theory, lambda calculus Math: recursive function theory, lambda calculus McCarthy’s list representation of programs, data McCarthy’s list representation of programs, data Some implementation characteristics: Some implementation characteristics: often interpreted, which makes it slow (compilers exist) often interpreted, which makes it slow (compilers exist) memory intensive: extensive dynamic allocation/deallocation of memory memory intensive: extensive dynamic allocation/deallocation of memory lots of research in “garbage collection” techniques lots of research in “garbage collection” techniques there existed special Lisp computers (Lisp Machine); however, RISC architecture replaced them, and now CISC is more common there existed special Lisp computers (Lisp Machine); however, RISC architecture replaced them, and now CISC is more common Program characteristics: Program characteristics: symbolic nature encourages experimentation, fast prototyping symbolic nature encourages experimentation, fast prototyping a major reason it’s popular in AI a major reason it’s popular in AI very recursive very recursive very easy to write “read-once” programs very easy to write “read-once” programs must be doubly diligent to write clean, politically correct programs! must be doubly diligent to write clean, politically correct programs! 3COSC 2P93 Prolog: Lisp

4 Getting started with Clisp clisp: www.clisp.org clisp: www.clisp.orgwww.clisp.org a Common Lisp (“CL”) a Common Lisp (“CL”) Starting on sandcastle (Linux): Starting on sandcastle (Linux): 1. put /usr/local/clisp/bin in your path 2. type: clisp lisp program files have “.lsp” or “.lisp” extension lisp program files have “.lsp” or “.lisp” extension Because its interpreted, you can create, edit, and debug programs during execution. Because its interpreted, you can create, edit, and debug programs during execution. recommended that you use a text editor to enter programs into files explicitly recommended that you use a text editor to enter programs into files explicitly To load your file: To load your file: > (load “/usr/people/mccarthy/myfile.lsp”) > (load “/usr/people/mccarthy/myfile.lsp”) To save a copy of your interactive session: To save a copy of your interactive session: > (dribble “session_file_name”) > (dribble “session_file_name”) To exit Xlisp: To exit Xlisp: > (exit) > (exit) 4COSC 2P93 Prolog: Lisp

5 Lisp syntax: List LISP: “Lost in Stupid Parentheses” ( + 9 3 8 ) ( + 9 3 8 ) function arguments you can replace function or argument by another list; arbitrarily deep you can replace function or argument by another list; arbitrarily deep examples examples (5) (dog (cat mouse)) (5) (dog (cat mouse)) (4 7 3 5) (+ 3 (* 3 6) (/ 3 5)) (4 7 3 5) (+ 3 (* 3 6) (/ 3 5)) You will spend much time balancing parentheses You will spend much time balancing parentheses try vi’s “%” key try vi’s “%” key 5COSC 2P93 Prolog: Lisp

6 Lisp functions, Atoms Basic rule of Lisp: Basic rule of Lisp: when combining Lisp functions, the embedded list arguments are always executed before the function is applied to them (“eager evaluation”) when combining Lisp functions, the embedded list arguments are always executed before the function is applied to them (“eager evaluation”) compare to: lazy evaluation - evaluate only when needed compare to: lazy evaluation - evaluate only when needed Atom: a basic data value (vs. lists, which are compound values) Atom: a basic data value (vs. lists, which are compound values) eg. george 3.14159 56 56 + two-words two-words Special list: NIL or () Special list: NIL or () considered to be both a list and an atom considered to be both a list and an atom 6COSC 2P93 Prolog: Lisp

7 Simple function calls > (+ 8 12 3) 23 > (* 5 6 7) 210 > (+ (- 4 5) (* 2 2)) 3 > (/ 2 3 4) 1/6 > (/ (/ 2 3) 4)) > (/ 2 (/ 3 4)) 1/68/3 > (/ 2.0 3 4) 0.1666667 7COSC 2P93 Prolog: Lisp

8 Lisp There are lots of built-in Lisp functions (see Xlisp doc.) There are lots of built-in Lisp functions (see Xlisp doc.) There are built-in data types as well (float, bool, string,...) There are built-in data types as well (float, bool, string,...) One key point: program code and data are both symbolic expressions in Lisp (same as Prolog) One key point: program code and data are both symbolic expressions in Lisp (same as Prolog) the interpreter doesn’t distinguish between them the interpreter doesn’t distinguish between them Lisp interpreter: Lisp interpreter: 1. evaluate each argument --> obtains a value 1. evaluate each argument --> obtains a value 2. evaluate the function applied to the argument values 2. evaluate the function applied to the argument values sometimes we don’t want a list to be evaluated: use the quote function to create a literal expression sometimes we don’t want a list to be evaluated: use the quote function to create a literal expression ‘ (a b c) ‘ (a b c) differs from: (a b c) <-- expects a function called “a” differs from: (a b c) <-- expects a function called “a” 8COSC 2P93 Prolog: Lisp

9 List functions: CAR, CDR CAR: returns first element in a list CAR: returns first element in a list result may be an atom or list result may be an atom or list CDR: returns tail of the list (rest of list minus first element) CDR: returns tail of the list (rest of list minus first element) - result is always a list - result is always a list eg. (car ‘(a b c d)) --> returns atom “a” (car ‘((a b) (c d) (e (f g)))) --> returns list (a b) (car ‘((a b) (c d) (e (f g)))) --> returns list (a b) note: (car (a b c d)) will try to evaluate (a b c d) first! (cdr ‘(a b c d)) --> returns (b c d) (cdr ‘(a b c d)) --> returns (b c d) (cdr ‘(a)) --> returns NIL or () (cdr ‘(a)) --> returns NIL or () (car (cdr ‘((a b) (c d) (e (f g))))) --> returns (c d) (car (cdr ‘((a b) (c d) (e (f g))))) --> returns (c d) (car ‘(cdr ‘((a b) (c d) (e (f g))))) --> returns CDR ! (programs are data) (car ‘(cdr ‘((a b) (c d) (e (f g))))) --> returns CDR ! (programs are data) 9COSC 2P93 Prolog: Lisp

10 List functions: CONS, LIST cons: inserts an item in front of a list cons: inserts an item in front of a list cons takes elem1 (atom or list), and stuffs it into first position of 2nd argument, which must be a list cons takes elem1 (atom or list), and stuffs it into first position of 2nd argument, which must be a list ( cons ‘a ‘( b c d) ) ( cons ‘a ‘( b c d) ) ( a b c d ) ( a b c d ) list: creates a list from a set of arguments list: creates a list from a set of arguments list takes arguments, and wraps parentheses around them list takes arguments, and wraps parentheses around them ( list ‘a ‘b ‘c ‘d ) ( list ‘a ‘b ‘c ‘d ) ( a b c d ) ( a b c d ) 10COSC 2P93 Prolog: Lisp

11 List functions more examples more examples (list ‘a ‘(b c) ) --> returns (a (b c) ) (list ‘(a b) ‘(c d)) --> returns ((a b) (c d)) (cons ‘a ‘(b c)) --> returns (a b c) (cons ‘(a b) ‘(c d)) --> returns ((a b) c d) (list (cons ‘a ‘(b c)) (cdr ‘(d e f))) = (list (a b c) (e f) ) -->returns ((a b c) (e f)) (cons ‘(* 3 4) ‘(5 6)) --> returns ((* 3 4) 5 6) (cons (* 3 4) ‘(5 6)) --> returns (12 5 6) 11COSC 2P93 Prolog: Lisp

12 Global variables setq takes a variable name (same rules as Java, C) and a value, and assigns variable that value setq takes a variable name (same rules as Java, C) and a value, and assigns variable that value > (setq f 25) 25 > (setq g ‘(a b c)) (a b c) > (cons f g) (25 a b c) > (cons ‘f g) (f a b c) > (list (cdr g) (list (car (cdr g)) (car g))) (( b c ) (b a) ) > (setq g ‘dog) dog 12COSC 2P93 Prolog: Lisp

13 Miscellaneous Important reminder: Important reminder: You must put the quote ‘ before any constants (atoms) in your program. You must put the quote ‘ before any constants (atoms) in your program. eg. ( list ‘cat ‘dog ‘bat ) Otherwise, if the quote is missing, Lisp thinks you are referring to a variable: > ( setq cat 5 ) > ( setq cat 5 ) > ( list cat ‘dog ‘bat) > ( list cat ‘dog ‘bat) ( 5 dog bat ) ( 5 dog bat ) 13COSC 2P93 Prolog: Lisp

14 More List primitive functions append: merges two or more lists into a single list append: merges two or more lists into a single list all arguments must be lists all arguments must be lists eg.( append ‘(a b) ‘(c)) --> returns (a b c) ( append ‘(a (b c)) ‘(d e) ‘((f)) ) --> returns ( a (b c) d e (f) ) note: unlike list, append presumes one level of list structure, and puts all the list arguments into this list note: unlike list, append presumes one level of list structure, and puts all the list arguments into this list reverse: reverse order of elements in the single argument reverse: reverse order of elements in the single argument note: does not reverse argument sublists! note: does not reverse argument sublists! eg.( reverse (cdr ‘(a b c d) ) ) --> returns (d c b) last: returns a list consisting of last element in its argument list last: returns a list consisting of last element in its argument list eg. (last ‘( a (b c) d e (f g) ) ) --> returns ((f g)) 14COSC 2P93 Prolog: Lisp

15 CONS, LIST, APPEND... These functions are “similar”, but shouldn’t be confused. These functions are “similar”, but shouldn’t be confused. ( cons ‘(a) ‘(b c d e) ) --> ( (a) b c d e ) only two arguments; second must be a list only two arguments; second must be a list inserts 1st arg as 1st element in 2nd list inserts 1st arg as 1st element in 2nd list ( list ‘(a) ‘b ‘(c(d)) ‘e ‘f) --> ( (a) b (c(d)) e f ) any number of arguments any number of arguments wraps parentheses around them, creating a list wraps parentheses around them, creating a list ( append ‘(a) ‘((b)) ‘(c(d)) ) --> (a (b) c (d) ) any number of args, all must be lists any number of args, all must be lists takes paretheses off of each argument, and then wraps parentheses around stripped results takes paretheses off of each argument, and then wraps parentheses around stripped results 15COSC 2P93 Prolog: Lisp

16 Conditional processing Predicate: a function that returns information about its arguments Predicate: a function that returns information about its arguments usually interpreted by program as “true” or “false” usually interpreted by program as “true” or “false” “false”: usually NIL “false”: usually NIL “true”: either the constant ‘t’ or any value other than NIL “true”: either the constant ‘t’ or any value other than NIL atom: ‘t’ if arg is an atom; otherwise NIL atom: ‘t’ if arg is an atom; otherwise NIL ( atom ‘dog ) --> t ( atom ‘(dog) ) --> NIL ( atom NIL ) --> t (NIL is both an atom and a list) listp: ‘t’ if arg is a list; otherwise NIL listp: ‘t’ if arg is a list; otherwise NIL ( listp ‘dog ) --> NIL ( listp ‘(dog)) --> t ( listp NIL ) --> t 16COSC 2P93 Prolog: Lisp

17 Conditional processing numberp: ‘t’ if arg is a number (integer, float), otherwise NIL numberp: ‘t’ if arg is a number (integer, float), otherwise NIL ( numberp 10 ) --> t ( numberp 10.5 ) --> t ( numberp ‘chris ) --> NIL zerop: ‘t’ if arg evaluates to 0 zerop: ‘t’ if arg evaluates to 0 ( zerop (/ 0 5) ) --> t ( zerop (/ 1 5)) --> NIL null: ‘t’ if argument is NIL, otherwise NIL null: ‘t’ if argument is NIL, otherwise NIL ( null nil ) --> t ( null ‘(a b) ) --> NIL equal: ‘t’ if its 2 args are equal, otherwise NIL equal: ‘t’ if its 2 args are equal, otherwise NIL ( equal 2 4 ) --> NIL ( equal ‘(b c) (cdr ‘ (a b c)) ) --> t 17COSC 2P93 Prolog: Lisp

18 Conditional processing member: if first arg is found as element in 2nd list arg. it returns tail of list starting with that first arg value; otherwise NIL member: if first arg is found as element in 2nd list arg. it returns tail of list starting with that first arg value; otherwise NIL ( member ‘a ‘(b c a e f) ) --> (a e f) ( member ‘a ‘( b (a e) d) ) --> NIL logical functions: logical functions: OR: evaluates args from left to right; returns first non-NIL value found OR: evaluates args from left to right; returns first non-NIL value found NOT: if arg evaluates to NIL, returns t; otherwise, non-NIL args return NIL NOT: if arg evaluates to NIL, returns t; otherwise, non-NIL args return NIL AND: evaluates left-to right, and returns NIL at first arg that evals to NIL; AND: evaluates left-to right, and returns NIL at first arg that evals to NIL; if all args are non-NIL, returns value of last argument if all args are non-NIL, returns value of last argument eg. ( seq x 25 ) ( or (> x 0 ) ( t ( or (> x 0 ) ( t ( not (> x 0) ) --> NIL ( not (> x 0) ) --> NIL ( and (> x 0) ( t ( and (> x 0) ( t ( and ‘a ‘b ‘c ) --> c ( and ‘a ‘b ‘c ) --> c There are many other predicates: There are many other predicates: relational tests: \= relational tests: \= precise tests on lists, constructs... see documentation for more precise tests on lists, constructs... see documentation for more 18COSC 2P93 Prolog: Lisp

19 Defining Lisp Functions > ( defun incr (num) > ( defun incr (num) (+ num 1) ) (+ num 1) ) incr incr Functor name must be an atom Functor name must be an atom Parameters are call by value: changes to them affect local memory Parameters are call by value: changes to them affect local memory think of them as local variables into which the argument values are copied; they disappear after function is executed think of them as local variables into which the argument values are copied; they disappear after function is executed function body is also a list function body is also a list 1. parameter values are substituted into all parameter references 2. body is executed 3. final value is returned as value for function function namearg list function body 19COSC 2P93 Prolog: Lisp

20 Function definition example (from “Essential Lisp”, J.R. Anderson et al., Addison-Wesley, 1987) (defun double (num) (* num 2)) Call: double (+ 5 10)) Call: double (+ 5 10)) Steps: Steps: 1. Argument of function call, (+ 5 10), evaluated  15 2. Function double is applied to 15: a) Value of argument 15 is assigned to parameter num. b) Body of function, (* num 2), evaluated: i. Variable num is evaluated  value 15 ii. Constant 2 evaluates to itself  2 iii. Function * is applied to args 15 and 2  30 iv. Function double returns value 30. 20COSC 2P93 Prolog: Lisp

21 Conditional processing cond: Lisp’s if-then-else statement cond: Lisp’s if-then-else statement evaluates argument lists one after another evaluates argument lists one after another the first list with a non-NIL test (“true”) has its body evaluated and returned as answer the first list with a non-NIL test (“true”) has its body evaluated and returned as answer common to put a default “otherwise” case at the end; otherwise NIL is returned if no cases are true common to put a default “otherwise” case at the end; otherwise NIL is returned if no cases are true ( defun abc (val) ( cond ( ( equal val ‘a) ‘first ) ( ( equal val ‘b) ‘second ) ( t ‘rest) ) ) optional ‘default’ case 21COSC 2P93 Prolog: Lisp

22 Example write a function that takes two values and returns the greater value, presuming they are numeric. Do error recovery. write a function that takes two values and returns the greater value, presuming they are numeric. Do error recovery. Version 1: (defun bigger ( a b ) ( cond ((not (numberp a)) NIL) ( cond ((not (numberp a)) NIL) ((not (numberp b)) NIL) ((not (numberp b)) NIL) ((> a b) a) ((> a b) a) ((> b a) b) ((> b a) b) (t a) ) ) (t a) ) ) (bigger 1 2) --> 2 (bigger 2 ‘c) --> NIL 22COSC 2P93 Prolog: Lisp

23 Example Version 2 (defun bigger ( a b ) ( cond ((not (numberp a)) NIL) ( cond ((not (numberp a)) NIL) ((not (numberp b)) NIL) ((not (numberp b)) NIL) ((> a b) a) ((> a b) a) (t b) ) ) (t b) ) ) Version 3 (defun bigger ( a b ) ( and (numberp a) ( and (numberp a) (numberp b) (numberp b) (or (and (> a b) a) (or (and (> a b) a) b) ) ) b) ) ) : very cryptic function! 23COSC 2P93 Prolog: Lisp

24 Example safediv - divides 2 numbers, but does error checking for proper data types, div by zero... safediv - divides 2 numbers, but does error checking for proper data types, div by zero... eg. ( safediv 6 3 ) --> 2 ( safediv 6 0 ) --> NIL ( safediv 6 0 ) --> NIL ( safediv ‘w 4 ) --> NIL ( safediv ‘w 4 ) --> NIL ( defun safediv ( num1 num2 ) ( and ( numberp num1) ( and ( numberp num1) ( numberp num2) ( numberp num2) ( not (zerop num2 )) ( not (zerop num2 )) (/ num1 num2) ) ) (/ num1 num2) ) ) 24COSC 2P93 Prolog: Lisp

25 Miscellaneous Comments in programs: start line with ; Comments in programs: start line with ; functions with no arguments: functions with no arguments: either: (defun f ( )..... either: (defun f ( )..... or (defun f NIL.... or (defun f NIL.... Interpreter: evaluates multiple expressions from left to right Interpreter: evaluates multiple expressions from left to right eg. (list (setq x 1) (setq x 2)) --> returns (1 2), but variable x = 2 eg. (list (setq x 1) (setq x 2)) --> returns (1 2), but variable x = 2 eg. (defun f (a b) eg. (defun f (a b) (action 1) (action 1) (action 2) (action 2)...... (action k)) (more later) (action k)) (more later) returning a constant value: returning a constant value: (defun five() (defun five() 5) 5) 25COSC 2P93 Prolog: Lisp

26 eg. Removeall ; (removeall a s) removes all instances of element a from s (defun removeall (a s) (cond (cond ((null s) nil) ((null s) nil) ((equal a (car s)) (removeall a (cdr s))) ((equal a (car s)) (removeall a (cdr s))) ('t (cons (car s) (removeall a (cdr s)))) ('t (cons (car s) (removeall a (cdr s)))) )) 26COSC 2P93 Prolog: Lisp

27 eg. Setdiff ; (setdiff a b) removes all elements in b from a (defun setdiff (a b) (cond (cond ((null b) a) ((null b) a) ((member (car b) a) ((member (car b) a) (setdiff (removeall (car b) a) (cdr b))) (setdiff (removeall (car b) a) (cdr b))) ('t (setdiff a (cdr b))) ('t (setdiff a (cdr b))) )) 27COSC 2P93 Prolog: Lisp

28 eg. Quicksort (defun quicksort (l) (cond ((null l) nil) (cond ((null l) nil) (t (append (quicksort (splitless (car l) (cdr l))) (t (append (quicksort (splitless (car l) (cdr l))) (list (car l)) (list (car l)) (quicksort (splitgeq (car l) (cdr l))))) (quicksort (splitgeq (car l) (cdr l))))) )) 28COSC 2P93 Prolog: Lisp

29 quicksort (cont) (defun splitless (n l) (cond ((null l) NIL) (cond ((null l) NIL) ((< (car l) n) (cons (car l) (splitless n (cdr l) ))) ((< (car l) n) (cons (car l) (splitless n (cdr l) ))) (t (splitless n (cdr l))) (t (splitless n (cdr l))) )) (defun splitgeq (n l) (cond ((null l) NIL) (cond ((null l) NIL) ((>= (car l) n) (cons (car l) (splitgeq n (cdr l) ))) ((>= (car l) n) (cons (car l) (splitgeq n (cdr l) ))) (t (splitgeq n (cdr l))) (t (splitgeq n (cdr l))) )) 29COSC 2P93 Prolog: Lisp


Download ppt "Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has."

Similar presentations


Ads by Google