1 / 49

LISP

LISP. COMP 205 – Week 11 Dr. Chunbo Chu. Intro. Lisp stands for “ LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy use of recursion Interpretive language Functional language Variations Frantz Lisp (80’s)

berny
Télécharger la présentation

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 COMP 205 – Week 11 Dr. Chunbo Chu

  2. Intro • Lisp stands for “LISt Process” • Invented by John McCarthy (1958) • Simple data structure (atoms and lists) • Heavy use of recursion • Interpretive language • Functional language • Variations • Frantz Lisp (80’s) • Common Lisp (de facto industrial standard)

  3. Why Lisp? • Because it’s the most widely used AI programming language • Because AI researchers and theoreticians like using it • Because it’s good for writing production software (Graham article) • Because it’s got lots of features other languages don’t • Because you can write new programs and extend old programs really, really quickly in Lisp

  4. The IDE • Install Lisp in a Box • loop • read in an expression from the console; • evaluate the expression; • print the result of evaluation to the console; end loop. • REPL

  5. Valid objects (S-expressions) • Atoms: • numbers: (real 1.0, integer 1) • symbols: a consecutive sequence of characters (no space) • e.g., a, x, price-of-beef. • two special symbols: T and NIL for logical true and false. • strings: a sequence of characters bounded by double quotes • e.g., "this is red". • (Note: LISP is case insensitive) • Lists: a list of atoms and/or lists, bounded by "(" and ")“, • e.g., (a b c), (a (b c)) • top elements of a list • example: top elements of list (a b c) are a, b, and c • top elements of list (a (b c)) are a and (b c) • nil: empty list, same as ().

  6. 2. Function calls • also a list • use prefix notation: (function-name arg1 ... argn) • returns function value for the given list of arguments • functions are either provided by Lisp function library or defined by the user. • Examples: • >(+ 1 3 5) • 9 • >(/ 3 5) • 3/5 • >(/ 3.0 5) • 0.59999999999999998 • >(sqrt 4) • 2

  7. Sqrt • + • *

  8. First Lisp program ( defun hello () (format t “Hello World!”)) • Use Emacs to create a new file (C-x f)containing the above code. • Save the file (C-x C-s) under name hello.lisp • Switch back the SLIME buffer and load the program by: (load “hello.lisp) • Run your program: (hello)

  9. exit • quote = `

  10. load

  11. Atoms • numeric • fractions • floating point • literal atoms • Boolean values • other symbols • strings

  12. Lists • NIL = ()

  13. Function calls • evaluation of functions

  14. setf more general than setq • binding

  15. 3. Evaluation of S-expression • 1) Evaluate an atom. • numerical and string atoms evaluate to themselves; • symbols evaluate to their values if they are assigned values, • return Error, otherwise; • the values of T and NIL are themselves. • 2) Evaluate a list - evaluate every top element of the list as follows, • unless explicitly forbidden: • the first element is always a function name; • evaluating it means to call the function body; • each of the rest elements will then be evaluated, and their values • returned as the arguments for the function. • Examples >(sqrt x) Error: The variable X is unbound. >(+ (/ 3 5) 4) 23/5 >(+ (sqrt 4) 4.0) 6.0

  16. 3) To assign a value to a symbol (setq, set, setf) • setq is a special form of function (with two arguments); • the first argument is a symbol which will not be evaluated; • the second argument is a S-expression, which will be evaluated; • the value of the second argument is assigned to be the value of • the first argument • to forbid evaluation of a symbol (quote or ‘) >(setq x 3.0) 3.0 >x 3.0 >(setq y x) 3.0 ; the value of x is assigned as the value of y >y 3.0 >(+ x y) 6.0

  17. >(quote x) x >'x x >(setq z 'x) x • to force an evaluation, using function "eval" • Two more assignment functions: • (set x y) ; assign the value of y to the value of x. x is evaluated • ; first and whose value must be a symbol • ; "setq" is a combination of "set" and "quote" • (setf x y) ; similar to but more general than "setq" in that x can be • ; something other than a symbol. >(+ x z) Error: X is not of type NUMBER ... >(+ x (eval z)) 6.0 eval

  18. first • rest • function nesting

  19. car • cdr • cadr • caddr • nthcdr • butlast • cons • append

  20. Compositions of car and cdr

  21. length • reverse • last • list

  22. Activity • Construct a list of four birds by evaluating several expressions with cons. Find out what happens when you cons a list onto itself. Replace the first element of the list of four birds with a fish. Replace the rest of that list with a list of other fish.

  23. Basic expression evaluation

  24. 2) Predicates (a special function which returns NIL if the predicate is false, T or anything other than NIL, otherwise) =, >, <, >=, <= for numerical values; equal, eq, for others (symbols, lists, etc.) tests if x is a atom tests if x is a list also numberp, symbolp, null predicates >(equal ‘a (car L)) T >(< x y) NIL >(= x y) T >(equal ‘x ‘y) NIL >(atom x) T >(atom L) NIL >(atom (car L)) T >(listp x) NIL >(listp L) T >(numberp ‘x) NIL >(numberpx) T >(symbolp ‘x) T >(symbolpx) NIL

  25. Basic storage handling

  26. >(null NIL) T >(null L) NIL >(null x) NIL 3) Set operations ( a list can be viewed as a set whose members are the top elements of the list) >(member 'b L) ; test if symbol b is a member (a top element) of L (B C) ; if yes, returns the sublist of L starting at the ; first occurrence of symbol b >(member ‘b (cons 'b L)) (B A B C) >(member x L) NIL ; if no, returns NIL >(union L1 L2) ; returns the union of the two lists >(intersection L1 L2) ; returns the intersection of the two lists >(set-difference L1 L2) ; returns the difference of the two lists Set operations

  27. defun

  28. Defining New Functions (defun name (parameter*) "Optional documentation string." body) • Convention: you construct compound names with hyphens rather than underscores or inner caps. • Thus, frob-widget is better Lisp style than either frob_widget or frobWidget. • When a parameter list is a simple list of variable names, the parameters are called required parameters

  29. Optional Parameters • Place the symbol &optional followed by the names of the optional parameters. (defunfoo (a b &optional c d) (list a b c d)) • When the function is called, arguments are first bound to the required parameters. (foo 1 2) (1 2 NIL NIL) (foo 1 2 3)  (1 2 3 NIL) (foo 1 2 3 4)  (1 2 3 4)

  30. Non-NIL default value • Replace the parameter name with a list containing a name and an expression. • The expression will be evaluated only if the caller doesn't pass enough arguments to provide a value for the optional parameter. (defunfoo (a &optional (b 10)) (list a b)) (foo 1 2)  (1 2) (foo 1)  (1 10)

  31. More flexibility: • (defun make-rectangle (width &optional (height width)) ...)

  32. Rest Parameters • Functions need to take a variable number of arguments. • E.g. (+), (+ 1), (+ 1 2), (+ 1 2 3), … • A catchall parameter after the symbol &rest. • Any arguments remaining after values have been doled out to all the required and optional parameters are gathered up into a list that becomes the value of the &rest parameter. (defun format (stream string &rest values) ...) (defun + (&rest numbers) ...)

  33. Keyword Parameters • Suppose you have a function that takes four optional parameters. • Now suppose that most of the places the function is called, the caller wants to provide a value for only one of the four parameters. • After any required, &optional, and &rest parameters you include the symbol &key and then any number of keyword parameter specifiers.

  34. (defunfoo (&key a b c) (list a b c)) • (foo) (NIL NILNIL) • (foo :a 1)  (1 NIL NIL) • (foo :b 1)  (NIL 1 NIL) • (foo :c 1)  (NIL NIL 1) • (foo :a 1 :c 3) (1 NIL 3) • (foo :a 1 :b 2 :c 3)  (1 2 3) • (foo :a 1 :c 3 :b 2)  (1 2 3)

  35. (defunfoo (&key (a 0) (b 0 b-supplied-p) (c (+ a b))) • Mixing Different Parameter Types • Whenever more than one flavor of parameter is used, they must be declared in order : first the names of the required parameters, then the optional parameters, then the rest parameter, and finally the keyword parameters.

  36. Function Return Values • The default behavior: return the value of the last expression evaluated as the function’s own return value. • The RETURN-FROM special operator to immediately return any value from the function • The first "argument" is the name of the block from which to return. (defunfoo (n) (dotimes (i 10) (dotimes (j 10) (when (> (* i j) n) (return-from foo (list i j))))))

  37. Data structures • assoc

  38. make-array • aref • defstruct

  39. Dotted pairs

  40. Dotted pairs

  41. 4) Conditional • >(cond (<test-1> <action-1>) • . • . • . • (<test-k> <action-k>)) • each (<test-i> <action-i>) is called a clause; • if test-i (start with i=1) returns T (or anything other than NIL), • this function returns the value of action-i; • else, go to the next clause; • usually, the last test is T, which always holds, meaning otherwise. • cond can be nested (action-i may contain (cond ...)) conditional

  42. Now, having basic functions, defun and cond we can define any Lisp function. Examples. 5. Define functions (heavy use of recursive definitions) (defun func-name (arg-1 ... Arg-n) func-body) examples: (defun member (x L) (cond ((null L) nil) ; base case 1: L is empty ((equal x (car L)) L) ; base case 2: x=first(L) (t (member x (cdr L))) ; recursion: test if x is in rest(L) )) (defun intersection (L1 L2) (cond ((null L1) nil) ((null L2) nil) ((member (car L1) L2) (cons (car L1) (intersection (cdr L1) L2))) (t (intersection (cdr L1) L2)) )) Example: (intersection '(a b c) '(b a b c)) returns (a b c) (intersection '(b a b c) '(a b c)) returns (b a b c) member intersection

  43. (defun set-difference (L1 L2) • (cond ((null L1) nil) • ((null L2) L1) • ((not (member (car L1) L2)) • (cons (car L1) (set-difference (cdr L1) L2))) • (t (set-difference (cdr L1) L2)) • )) • Define functions iteratively. • (dolist (x L result) body) • for each top level element x in L, do body(x); • x is not equal to an element of L in each iteration, but rather x takes an element of L as its value; • (dotimes (count n result) body) • ; do body n times. count starts with 0, ends with n-1 • Note: result is optional, to be used to hold the computing result. • If result is given, the function will return the value of result, • returns NIL, otherwise. (may change global variables as side effects.) dolist dotimes

  44. Activity • Write a function sum to calculate the sum of all numbers in a list

  45. Various definitions of SUM dolist (defun sum1 (L) (setq y 0) (dolist (x L y) (setq y (+ y x)))) (defun sum2 (L) (setq y 0) (dolist (x L y) (setq y (+ y (eval x))))) (defun sum3 (L) (setq y 0) (dotimes (count (length L) y) (setq y (+ y (nth count L))) )) defun sum4 (L) (setq y 0) (dotimes (count (length L) y) (setq y (+ y (eval (nth count L)))) )) >(setq L1 '(1 2 3)) (1 2 3) dotimes >(sum1 L1) 6

  46. >(setq L1 '(1 2 3)) (1 2 3) >(setq L2 '(a b c)) (A B C) >(dotimes (count 3) (set (nth count L2) (nth count L1))) NIL >a 1 >(sum1 L1) 6 >(sum1 L2) Error: … >(sum2 L2) 6 >(sum3 L1) 6 >(sum3 L2) Error: … >(sum4 L2) 6

More Related