1 / 27

Introduction to LISP

Introduction to LISP. Lisp. Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures You can write programs that write programs (macros). Prefix notation. >(+ 2 3) 5 >(+ 2 3 4 5) 14 >(/ (- 7 1) (- 4 2)) 3. Prefix notation evaluation.

sonora
Download Presentation

Introduction to 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. Introduction to LISP

  2. Lisp • Extensible: It lets you define new operators yourself • Lisp programs are expressed as lisp data structures • You can write programs that write programs (macros)

  3. Prefix notation • >(+ 2 3) • 5 • >(+ 2 3 4 5) • 14 • >(/ (- 7 1) (- 4 2)) • 3

  4. Prefix notation evaluation • >(/ (- 7 1) (- 4 2)) • 3 • Arguments from left to right • Arguments are passed to the function

  5. Data • Integer • >256 • 256 • Strings • >"hello world“ • "hello world“ • Symbols • Symbols are words • >’hello • HELLO • Usually converted to uppercases • Lisp is case insensitive • Lists

  6. Quote • >(quote (+ 3 5)) • (+ 3 5) • >(’ (+ 3 5)) • (+ 3 5) • quote is a special operator (distinct evaluation rule) • Protects expressions from evaluation

  7. Lists • Lists are zero or more elements enclosed in parentheses • >(list 3 2 1) • (3 2 1) • >’(3 2 1) • (3 2 1) • >(list '(+ 2 1) (+ 2 1)) • ((+ 2 1) 3)

  8. Lists • Lisp programs are expressed as lists • This is why we need the quote • Quoted list: returns the list itself • Unquoted list: trated as code and returns its evaluation

  9. List operations • >(cons ‘a ‘(b c d)) • (A B C D) • >(car ‘(a b c d)) • A • >(cdr ‘(a b c d)) (B C D) • >(car (cdr ‘(a b c d))) • B • second, third, nth

  10. Truth • T, NIL, null, not • >(listp ‘(a b c)) • T • (>listp 27) • NIL • >(null nil) • T • >(not nil) • T numberp, listp, stringp, …

  11. Conditionals (if (listp ‘(a b c)) (+ 1 2) (+ 5 6)) 3 (if (listp 27) (+ 2 3)) NIL

  12. AND / OR (and t NIL) NIL (and t (+ 1 3)) 3 (or t NIL) T (or t (+ 1 3)) T

  13. Functions (defun our-third (x) (car (cdr (cdr x))) >(our-third ‘(A B C D)) C

  14. Recursion (defun our-member (obj lst) (if (null lst) nil (if (eql (car lst) obj) T (our-member obj (cdr lst)))))

  15. Iteration (defun show-squares (start end) (do ((i start (+ i 1 ))) ((> I end) ‘done) (format t “~s ~s ~%” i (* i i)))) We do not always need recursion. When we want to do something repeatedly, iteration is sometimes more natural A typical case is to generate some sort of table or iterating over an array Prints out the squares of the integers from start to end

  16. Recursive show-squares • For comparison, here is a recursive version of show-squares. (defun show-squares (i end) (if (> i end) ‘done (progn (format t “~s ~s ~%” i (* i i)) (show-squares (+ i 1) end)))) Note: progn is an special operator that evaluates its arguments in order and returns the value(s) of the last.

  17. Functional programming • >(setf lst ‘(c a r a t)) • (C A R A T) • (remove ‘a lst) • (C R T) • Removes does not remove an object from a list (not literally) • The function returns a new list containing everything on the original list but the object • Use (setf x (remove ‘a x)) instead

  18. Functions as objects • In Lisp, functions are regular objects (like symbols or strings or lists) • Like any other kind of object, we can pass functions as arguments. One example of a function that takes a function as parameter is apply >(apply #’+ ‘(1 2 3)) 6 • #’ (sharp-quote) is an abbreviation for function just as ‘ is an abbreviation for quote >(apply (function +) ‘(1 2 3)) 6 • The function funcall does the same thing but does not need the arguments to be packaged in a list: >(funcall #’+ 1 2 3) 6

  19. Equality > (eql '(1 2) '(1 2)) NIL > (equal '(1 2) '(1 2)) T > (eql 1 1) T > (equal 1 1) T > (= 1 1) T > (= 1 1.0) T > (eql 1 1.0) NIL > (equal 1 1.0) NIL

  20. Mapping functions • > (mapcar #'sqrt '(4 9 16)) • (2 3 4) • Mapcar takes a function and a list and returns the result of applying the function to every element from the list. • Other map functions • maplist, mapcan, mapcon, …

  21. Some functions you might need • car • cdr • defun • if • lambda • list • listp • stringp • loop • mapcar • not • defvar • setf • setq • numberp • funcall • remove • defmacro

  22. Some useful functions for the homework • Instantiate the game class • (setq mypuzzle (make-instance 'game :board *easy-test* :name “easy”)) • Creates an instance of the class game and sets the att board to *easy-test* and the att name to “easy” • To access the atts (above we just set them when creating the instance), use the accessors defined in the defclass • (game-board mypuzzle) • (game-name mypuzzle)

  23. Some useful functions for the homework • Use the functions provided. Particularly row-groups, column-groups, and block-groups: (setq rows (ROW-GROUPS (game-board mypuzzle))) (setq cols (COLUMN-GROUPS (game-board mypuzzle))) (setq blocks (BLOCK-GROUPS (game-board mypuzzle))) game-board is the accessor to the board attribute in the game class • You can print the lists: >(print rows) ((2 4 3 1) (1 3 4 2) (4 2 1 3) (3 1 2 -))

  24. Some useful functions for the homework • (not (member t (mapcar 'duplicate-values rows))) • no duplicate values on the rows of the board (rows is a list of lists as you can see on previous slide) • (defun duplicate-values (lst) • (has-duplicates (sort (remove '- lst) #'<))) • has-duplicates should be a recursive function similar to our-member defined on previous slides. The logic is: • return NIL for lists of size 1 or less • return true if the first and second element are equal • call has-duplicates on the rest of the list

  25. Some useful functions for the homework • Removes the dashes from the list lst • (remove ‘- lst) • Member which returns NIL if the list does not contain the element • > (member 1 '(2 3 4)) • NIL • (not (member t (mapcar 'duplicate-values rows))) • no duplicate values on the rows of the board (rows is a list of lists as you can see on previous slide)

  26. Some useful functions for the homework • iota creates a list of numbers within a range • > (iota 10 1) • (1 2 3 4 5 6 7 8 9 10) • sort returns a the list sorted according to a comparison function • > (sort '(5 3 4 1 9) #'<) • (1 3 4 5 9) • To check if a list (e.g. a row, a column, a block) has all the values from 1 to MAXX (useful function for defining goalp): (defun all-values (lst) (equal (iota MAXX 1) (sort (remove '- lst) #'<) ))

  27. From the code from the book • http://aima.cs.berkeley.edu/ • Use utilities.lisp if you want to use the iota function • You might want to use queue.lisp too (includes functions for queues maniulations) • You might want to check simple.lisp for the general search (an idea of how to implement it)

More Related