lisp: exampleslisp: examples • eval • the lisp interpreter is a loop read‐eval‐print • the...

Post on 24-Mar-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lisp:Examplesref:Lisp(3rdEd)Winston&Horn• Primitive functions

• (+ 5 2 ) yields 7• (+ 5 2 3 11) yields 21• (+) yields 0

07/0

3/20

16P

S -

Lisp

Intro

1

Lisp:Examples• List access

• (first ‘( A B C )) yields A• (first ‘((A B) C D)) yields (A B)• (first ‘( )) yields NIL (empty list)• (first ‘A) yields an error

A is not a list

07/0

3/20

16P

S -

Lisp

Intro

2

Lisp:Examples• List constructors:  cons, append, list, last, rest

(cons ‘A ‘()) (A)(cons ‘A ‘(B C)) (A B C)(cons ‘( ) ‘(A B)) (Nil A B)(cons ‘(A B) ‘( D E F))  ((A B) D E F)

(append ‘(A B) ‘((C D) E F))  (A B (C D) E F)(list ‘A ‘B ‘C ‘D) (A B C D)(last ‘(A B C D)) (D)(rest ‘(A B C D)) (B C D)

07/0

3/20

16P

S -

Lisp

Intro

3

Lisp:Examples• Predicate functions

• listp – is the argument a list?• (listp ‘(A B)) T• (listp nil) T (empty list)• (listp ‘A) nil (not a list)

• null – is the list empty?• (null ‘(A)) Nil• (null nil) T

07/0

3/20

16P

S -

Lisp

Intro

4

Lisp:Examples (HicsuntDracones!)

• Predicate functions – equality

• EQ – T if both arguments are atoms and the same

• (eq ‘A ‘A) T• (eq ‘A ‘(A B)) Nil• (eq ‘(A B) ‘(A B)) Nil (not atoms!)• (equal ‘(A B) ‘(A B)) T

• (eq 1.5 1.5) Nil• (= 1.5 1.5) T

• TAKE CARE WITH TESTS FOR EQUALITY!

07/0

3/20

16P

S -

Lisp

Intro

5

Lisp:Examples– Specialforms• set; setq (set quote); setf (set field) (structures)

• set / setq / setf• (set  ‘pi 3.141539) ;; pi is quoted• (setq pi 3.141539) ;; pi is not quoted• (setf pi 3.141593)  ;; pi is not quoted

• defun ;; define a function• (defun square (x) (* x x))• (square 4)  16

07/0

3/20

16P

S -

Lisp

Intro

6

Lisp:Examples• Control flow:  if, when, unless

• (if <predicate> <then_exp> <else_exp>)• (defun myabs (x) (if (< x 0) (‐ x) x))

• if with a NIL else clause (if <test> <then form> nil ) • (when <test> <then form>)

• if with a NIL then clause (if <test> nil <else form>)• (unless <test> <else form>)

07/0

3/20

16P

S -

Lisp

Intro

7

Lisp:Examples

• Control flow ‐ Cond

(cond(test_1 (expr))(test_2 (expr))…(test_n (expr))(t (expr)))

NB: otherwise == t (true)

(defun my_abs (x)

(cond( (= x 0) 0)( (< x 0) (‐ x))( (> x 0) x))

)

07/0

3/20

16P

S -

Lisp

Intro

8

Lisp:Examples

• Control flow ‐ Cond

(cond(test_1 (expr))(test_2 (expr))…(test_n (expr))(t (expr)))

NB: otherwise == t (true)

(defun member (atm lis)

(cond( (null lis) NIL)( (eq atm (first lis))    T)( T (member atm (rest lis))))

)

07/0

3/20

16P

S -

Lisp

Intro

9

Lisp:Examples(defun equalsimp (lis1 lis2)(cond((null lis1) (null lis2)) ; 1((null lis2) nil) ; 2((eq (first lis1)  (first lis2)) ; 3

(equalsimp (rest lis1) (rest lis2)))(t nil) ; 4

))1 – both lists = empty  T; lis1 empty & lis2 not empty  false2 – lis1 is NOT empty & lis2 is empty  nil (false)3 – the first elements of each list are equal  test the rest of the list4 – default  nil (false)

07/0

3/20

16P

S -

Lisp

Intro

10

Lisp:Examples• equalsimp takes 2 simple lists as arguments 

• (i.e. no nested lists)

• (equalsimp ‘(A B) ‘(A B)) T

• (equalsimp ‘(A B) ‘(A B C))  nil

• (equalsimp ‘A ‘A) nil

07/0

3/20

16P

S -

Lisp

Intro

11

Lisp:Examples– equalsimp ‐ trace

EQUALSIMP;; Tracing function EQUALSIMP.(EQUALSIMP)1. Trace: (EQUALSIMP '(A B) '(A B))2. Trace: (EQUALSIMP '(B) '(B))3. Trace: (EQUALSIMP 'NIL 'NIL)3. Trace: EQUALSIMP ==> T2. Trace: EQUALSIMP ==> T1. Trace: EQUALSIMP ==> TT(EQUALSIMP)Bye.

07/0

3/20

16P

S -

Lisp

Intro

12

Lisp:Examples– equalsimp ‐ trace

EQUALSIMP;; Tracing function EQUALSIMP.(EQUALSIMP)1. Trace: (EQUALSIMP '(A B) '(A B C))2. Trace: (EQUALSIMP '(B) '(B C))3. Trace: (EQUALSIMP 'NIL '(C))3. Trace: EQUALSIMP ==> NIL2. Trace: EQUALSIMP ==> NIL1. Trace: EQUALSIMP ==> NILNIL(EQUALSIMP)Bye.

07/0

3/20

16P

S -

Lisp

Intro

13

Lisp:Examples– equalsimp ‐ trace

EQUALSIMP;; Tracing function EQUALSIMP.(EQUALSIMP)1. Trace: (EQUALSIMP 'A 'A)*** ‐ FIRST: A is not a list(EQUALSIMP)Bye.

07/0

3/20

16P

S -

Lisp

Intro

14

Lisp:Examples‐myequal(defun myequal (lis1 lis2)(cond((not (listp lis1)) (eq lis1 lis2))((not (listp lis2)) nil)((null lis1) (null lis2))((null lis2) nil)((myequal (first lis1) (first lis2))

(myequal (rest lis1) (rest lis2)))(t nil)

))(trace myequal)(myequal '(A B) '(A B))(untrace)

NB: test for list (listp)

07/0

3/20

16P

S -

Lisp

Intro

15

Lisp:Examples– myequal +traceMYEQUAL;; Tracing function MYEQUAL.(MYEQUAL)1. Trace: (MYEQUAL '(A B) '(A B))2. Trace: (MYEQUAL 'A 'A)2. Trace: MYEQUAL ==> T2. Trace: (MYEQUAL '(B) '(B))3. Trace: (MYEQUAL 'B 'B)3. Trace: MYEQUAL ==> T3. Trace: (MYEQUAL 'NIL 'NIL)3. Trace: MYEQUAL ==> T2. Trace: MYEQUAL ==> T1. Trace: MYEQUAL ==> TT(MYEQUAL)Bye.

07/0

3/20

16P

S -

Lisp

Intro

16

Lisp:Examples– myequal +traceMYEQUAL;; Tracing function MYEQUAL.(MYEQUAL)1. Trace: (MYEQUAL '(A B) '(A C))2. Trace: (MYEQUAL 'A 'A)2. Trace: MYEQUAL ==> T2. Trace: (MYEQUAL '(B) '(C))3. Trace: (MYEQUAL 'B 'C)3. Trace: MYEQUAL ==> NIL2. Trace: MYEQUAL ==> NIL1. Trace: MYEQUAL ==> NILNIL(MYEQUAL)Bye.

07/0

3/20

16P

S -

Lisp

Intro

17

Lisp:Examples– myequal +traceMYEQUAL;; Tracing function MYEQUAL.(MYEQUAL)1. Trace: (MYEQUAL '(A B) '(A B C))2. Trace: (MYEQUAL 'A 'A)2. Trace: MYEQUAL ==> T2. Trace: (MYEQUAL '(B) '(B C))3. Trace: (MYEQUAL 'B 'B)3. Trace: MYEQUAL ==> T3. Trace: (MYEQUAL 'NIL '(C))3. Trace: MYEQUAL ==> NIL2. Trace: MYEQUAL ==> NIL1. Trace: MYEQUAL ==> NILNIL(MYEQUAL)Bye.

07/0

3/20

16P

S -

Lisp

Intro

18

Lisp:Examples– myequal +trace

MYEQUAL;; Tracing function MYEQUAL.(MYEQUAL)1. Trace: (MYEQUAL 'A 'A)1. Trace: MYEQUAL ==> TT1. Trace: (MYEQUAL 'A 'B)1. Trace: MYEQUAL ==> NILNIL(MYEQUAL)Bye.

07/0

3/20

16P

S -

Lisp

Intro

19

Lisp:Examples– myequal +traceMYEQUAL;; Tracing function MYEQUAL.(MYEQUAL)1. Trace: (MYEQUAL '((A B) C (D E)) 

'((A B) C (D E)))2. Trace: (MYEQUAL '(A B) '(A B))3. Trace: (MYEQUAL 'A 'A)3. Trace: MYEQUAL ==> T3. Trace: (MYEQUAL '(B) '(B))4. Trace: (MYEQUAL 'B 'B)4. Trace: MYEQUAL ==> T4. Trace: (MYEQUAL 'NIL 'NIL)4. Trace: MYEQUAL ==> T3. Trace: MYEQUAL ==> T2. Trace: MYEQUAL ==> T2. Trace: (MYEQUAL '(C (D E)) '(C (D E)))3. Trace: (MYEQUAL 'C 'C)3. Trace: MYEQUAL ==> T

3. Trace: (MYEQUAL '((D E)) '((D E)))4. Trace: (MYEQUAL '(D E) '(D E))5. Trace: (MYEQUAL 'D 'D)5. Trace: MYEQUAL ==> T5. Trace: (MYEQUAL '(E) '(E))6. Trace: (MYEQUAL 'E 'E)6. Trace: MYEQUAL ==> T6. Trace: (MYEQUAL 'NIL 'NIL)6. Trace: MYEQUAL ==> T5. Trace: MYEQUAL ==> T4. Trace: MYEQUAL ==> T4. Trace: (MYEQUAL 'NIL 'NIL)4. Trace: MYEQUAL ==> T3. Trace: MYEQUAL ==> T2. Trace: MYEQUAL ==> T1. Trace: MYEQUAL ==> TT(MYEQUAL)Bye.

07/0

3/20

16P

S -

Lisp

Intro

20

Lisp:Examples

• Scope & fences – let>(setf x 'outside) Warning: Declaring X special.

OUTSIDE 

>(let ((x 'inside) (y x))(list x y)) 

(INSIDE OUTSIDE)

Evaluation order NOT sequential

• Scope & fences – let*>(setf x 'outside) Warning: Declaring X special.

OUTSIDE 

>(let* ((x 'inside) (y x))(list x y)) 

(INSIDE INSIDE)

Evaluation order sequential

07/0

3/20

16P

S -

Lisp

Intro

21

Lisp:Examples• let

• binds values to arguments – IN PARALLEL !!!• creates a local environment (block)• may have a “body” which is evaluated

• let*• Shorthand for nested let’s• binds values to arguments – IN SERIAL• may have a “body” which is evaluated

07/0

3/20

16P

S -

Lisp

Intro

22

Lisp:Examples• Scope & fences – let*>(setf x 'outside) Warning: Declaring X special.

OUTSIDE 

>(let* ((x 'inside) (y x))(list x y)) 

(INSIDE INSIDE)

• Scope & fences – let*>(setf x 'outside) Warning: Declaring X special.

OUTSIDE 

>(let ((x 'inside))(let (y x))(list x y)))

(INSIDE INSIDE)

07/0

3/20

16P

S -

Lisp

Intro

23

Lisp:Examples• Lambda expressions & defun

• (defun second (L) (first (rest L)))• (lambda (L) (first (rest L)))

• Application• (second ‘(A B C)) B• ((lambda (L) (first (rest L))) ‘(A B C))  B

• defun can be thought of as binding a name to a lambda expression

07/0

3/20

16P

S -

Lisp

Intro

24

Lisp:ExamplesSum of squares>(defun square (x) (* x x))

>(defun sum‐square (index n)(if (> index n)0(+ (square index)(sum‐square (+ 1 index) n))

))>(sum‐square 1 3)14

Sum of sinessin is defined in Lisp

>(defun sum‐sin (index n)(if (> index n)0(+ (sin index)(sum‐sin (+ 1 index) n))

))>(sum‐sin 1 3)1.8918884

07/0

3/20

16P

S -

Lisp

Intro

25

Lisp:Examples• function as argument

>(defun square (x) (* x x))

>(defun sum (fn index n)(if (> index n)0(+ (funcall fn index)

(sum fn (+ 1 index) n) )))

>( sum ( function square) 1 3)14NB: sum has 3 arguments

• function• denotes that argument is a function (cf quote)

• creates a function object (lexical closure)

• Abbreviated as #’

> (sum #’square 1 3)14

> (sum #’sin 1 3)1.8918884

07/0

3/20

16P

S -

Lisp

Intro

26

Lisp:Examples• Functions as return values

> (defun create‐add (const)(function (lambda (x)

(+ x const))))

> (funcall (create‐add 17) 3)20NB: argument to create‐add is 17The argument to the functionreturned by create‐add is 3

• Functions as return values

> (setf add‐17 (create‐add 17))> (setf add‐97 (create‐add 97))

> (funcall add‐17 3)20> (funcall add‐97 13)110> ; create a list of functions> (list (create‐add 10)

(create‐add 20)(create‐add 30))

07/0

3/20

16P

S -

Lisp

Intro

27

Lisp:Examples• Functional forms

• Composition• (defun compose (f g)

(lambda (x) (funcall f (funcall g x)))

• Apply to all: mapcar ‐ apply a function to list elements> (mapcar #’ (lambda (n) (+ n 2)) ‘(5 6 7 8))> (7 8 9 10)> (mapcar #’ square ‘(5 6 7 8))> (25 36 49 64)

07/0

3/20

16P

S -

Lisp

Intro

28

Lisp:Examples(defunmapcar (fun lis)(cond( (null lis)   ‘())( T   (cons (funcall fun (first lis))

(mapcar fun (rest lis)))))

)

NB: if lis is empty return the empty listotherwise create a list (cons) with the function applied to the first element of the list and mapcar fun on the tail of the list

07/0

3/20

16P

S -

Lisp

Intro

29

Lisp:Examples• Eval

• the Lisp interpreter is a loop read‐eval‐print• the expression (text) is first read• then the expression is evaluated • finally the result of the expression is printed

> (setf form ‘(+ 2 2)) ; form is a list object with 3 elements(+ 2 2)

> form ; the value of form(+ 2 2)

> (eval form) ; the evaluation of form4 ; the first element (+) is a function

07/0

3/20

16P

S -

Lisp

Intro

30

Lisp:Examples• EVAL example >(defun adder (lis)

(cond( (null lis) 0)( T (eval (cons ‘+ lis)))

))>(adder ‘(1 2 3 4))10

07/0

3/20

16P

S -

Lisp

Intro

31

Lisp:Examples

• Primitive functions +• List functions

• first, rest, cons, list, append, last• listp, null

• Predicates: eq, =• Special forms

• set / setq / setf, defun

• Control flow• if, (when, unless) cond

• Scope (environment)• let, let*

• Function related• lambda expressions• defun• lambda / let• function (#’)• funcall• mapcar• eval

07/0

3/20

16P

S -

Lisp

Intro

32

top related