Top Banner
Introduction to LISP
27

Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Dec 13, 2015

Download

Documents

Adam Davis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Introduction to LISP

Page 2: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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)

Page 3: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Prefix notation

• >(+ 2 3)• 5

• >(+ 2 3 4 5)• 14

• >(/ (- 7 1) (- 4 2))• 3

Page 4: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Prefix notation evaluation

• >(/ (- 7 1) (- 4 2))

• 3

1. Arguments from left to right

2. Arguments are passed to the function

Page 5: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Data• Integer• >256• 256

• Strings• >"hello world“• "hello world“

• Symbols• Symbols are words • >’hello• HELLO

– Usually converted to uppercases– Lisp is case insensitive

• Lists

Page 6: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Quote

• >(quote (+ 3 5))• (+ 3 5)

• >(’ (+ 3 5))• (+ 3 5)

• quote is a special operator (distinct evaluation rule)

• Protects expressions from evaluation

Page 7: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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)

Page 8: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 9: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 10: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Truth• T, NIL, null, not

• >(listp ‘(a b c))• T

• (>listp 27)• NIL

• >(null nil)• T

• >(not nil)• T

numberp, listp, stringp, …

Page 11: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Conditionals

(if (listp ‘(a b c))(+ 1 2)

(+ 5 6))

3

(if (listp 27)

(+ 2 3))

NIL

Page 12: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

AND / OR

(and t NIL)NIL

(and t (+ 1 3))3

(or t NIL)T

(or t (+ 1 3))T

Page 13: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Functions

(defun our-third (x)

(car (cdr (cdr x)))

>(our-third ‘(A B C D))

C

Page 14: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Recursion

(defun our-member (obj lst)

(if (null lst)

nil

(if (eql (car lst) obj)

T

(our-member obj (cdr lst)))))

Page 15: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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 naturalA typical case is to generate some sort of table or iterating

over an arrayPrints out the squares of the integers from start to end

Page 16: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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.

Page 17: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 18: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 19: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 20: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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, …

Page 21: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 22: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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)

Page 23: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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 -))

Page 24: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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

Page 25: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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)

Page 26: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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) #'<) ))

Page 27: Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

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)