25
1 Lisp • Functions Built-in functions Defining functions Function Evaluation and Special Forms defun, if Control statements – Conditional if , cond Repetition (loops) do – Sequence prog

1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

Embed Size (px)

Citation preview

Page 1: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

1

Lisp • Functions

– Built-in functions– Defining functions– Function Evaluation and Special Forms

• defun, if

• Control statements– Conditional

• if , cond

– Repetition (loops)• do

– Sequence• prog

Page 2: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

2

The CONS Cell – List Building • Cons

– A pair of pointers:• the first is the car, and

• the second is the cdr

• Atom– Basic lisp entity

• a symbol, a number (real, rational (ratio integer), float, complex), a vector, an array, a character, a string

• Everything that is not a cons

(defun our-atomp (x) (not (consp x)))

• List– An ordered collection of atoms or lists (the elements of the list)– A list is either nil or a cons

(defun our-listp (x) (or (null x) (consp x)))

a b

Page 3: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

3

Basic List Processing Functions• list

– takes any number args, returns a list:– (list 'x 'y 'z) => (X Y Z)– (list (list 'x 'y) (list 'x 'y)) => ((X Y) (X Y))

• car (or first)– returns the first element of a list– (car (list 'x 'y)) => X

• cdr (or rest) – everything but first element:– (cdr '(a b c)) => (B C)

• cons– prepends a symbol to a list– (cons (list 'x 'y) (list 'x 'y)) => ((X Y) X Y)

Page 4: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

4

cons and car• (setf x (cons 'a nil))

(A)

• (setf x (cons (car x) '(b c)))

(A B C)

a

x nil

a

x

b c

nil

Page 5: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

5

cdr and list• (setf y (list 'a (list 'b 'c) 'd))

(A (B C) D)• (setf z (car (cdr y)))

(B C)

• (eql z (cdr x))

NIL• (equal z (cdr x))

T• (eql z (car (cdr y)))

T

y

d

nil

nil

a

z

b c

Page 6: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

6Predicate Functions listp and null

• listp – takes one parameter– it returns

• T if the parameter is a list• NIL otherwise

• null– takes one parameter– it returns

• T if the parameter is the empty list• NIL otherwise

– Note that null returns T if the parameter is ()!• What is this equivalent to?

Page 7: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

7Examples - cons, car, cdr, consp

• (setf x (cons 'a 'b))

(A . B)• (car x)

A• (cdr x)

B

• (setf y (cons 'a (cons (cons 'b 'c) (cons 'd 'e))))

(A (B . C) D . E)

• (setf z (car (cdr y)))

(B . C)• (consp (cdr y))

T• (consp (cdr z))

NIL

y

a b

y

a d e

b c

Page 8: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

8

List Processing Functions, cont.• append

– takes any number of lists as arguments – returns them appended together

(append '(a b c) '(d e f)) => (A B C D E F)

• equal– takes two arguments– returns T if they are structurally equal or of

equal value

Page 9: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

9

Sublists• (list (list 'blue 'sky)

(list 'green 'grass)

(list 'brown 'earth))

((blue sky) (green grass) (brown earth))

sky

nil

blue grass

nil

green earth

nil

brown

nil

Page 10: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

10

Cons Cells and Lists as Trees• Binary tree

– car as the left subtree,– cdr as the right subtree

• (setf x

'(((a) ((b) (c)))

((d (e)) f))) x

c

nil

b

nila

nil

f

nil

e

nild

Page 11: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

11

SDRAW program• See the structure of anything

– sdraw.lisp – available at

• www2.hawaii.edu/~janst/313/lisp/sdraw.lisp

– (from

• Common Lisp: A Gentle Introduction to Symbolic Computation by David S. Touretzky

• Benjamin/Cummings Publishing Co., 1990.)

• Save a copy to your account and try it out

Page 12: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

12(sdraw:sdraw '(2 (a b) 3))

> (sdraw:sdraw '(2 (a b) 3))

[*|*]--->[*|*]------------------>[*|*]--->NIL

| | |

v v v

2 [*|*]--->[*|*]--->NIL 3

| |

v v

A B

Page 13: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

13Predicate Functions – usually end in P Return NIL or something else (True)

• What type is it? (typep … )• Is it a list? (listp … )• Is it a number? (numberp … )• Is it an integer? (integerp … )• Is it a string? (stringp … )• Is it an atom? (atom … )• Is it nil? (null … )• etc.

Page 14: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

14Predicate Functions eq and equal

• eq takes two symbolic parameters;– returns T if both parameters are atoms and the two

are the same

e.g., (eq 'a 'a) yields T

(eq 'a 'b) yields NIL– Note that if eq is called with list parameters, the result

is not reliable– Also eq does not work for numeric atoms

• equal takes two parameters– Returns T if both parameters “look/print the same”– Works on lists, structures, etc.– Try equal first

Page 15: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

15

Functions Return a Value• Is arg a list? (listp <arg> )

– (listp "foo") => NIL

• Is arg the empty list? (null <arg> )– (null nil) => T

• Return new list with all args (list <args>* )– (list 4 5 6) => (4 5 6)

• Return first item (car <arglist> )– (car (list 4 5 6)) => 4

• Return new list with everything except the first item (rest)

(cdr <arglist> )– (cdr (list 4 5 6)) => (5 6)

• Return new list with arg1 1st, then everything in arg2

(cons <item> <inlist> )– (cons 5 (list 4 5 6) ) => (5 4 5 6)

• These functions do not have side effects

Page 16: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

16

Function Definition• To create a named function, use defun:

(defun <name> (<param list>) <docu> <forms>)

>(defun sumsq (x y)

"Returns sum of X and Y squared."

(+ (* x x) (* y y)))

SUMSQ

• The documentation string is saved in the environment and can be recalled with(documentation (quote <name>) 'function)

>(documentation 'sumsq 'function)

"Returns sum of X and Y squared."

Page 17: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

17

Special forms• Already used one – defun!• Syntax is the same as function calls

(<special word> <arg1> <arg2> …)• Special word is one of:

-defun, if, let, function, quote, setq, setf, etc.

• Not evaluated in the same way as functions

(i.e. lazy - not eager evaluation)• Quiz - What would happen with eager evaluation here:

(if (> x 0)

(/ 10 x)

x)

• Why can’t eager evaluation be used for if ?

Page 18: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

18

Special Form - quote • quote takes one parameter and the entire

expression evaluates to the parameter– (quote (a b c)) => (A B C) – '(a b c) => (A B C) ; alternate syntax

• quote can’t work under eager evaluation. Why?• quote allows us to represent functions as data

– (+ 1 2) is a program (computes 3 when evaled)– '(+ 1 2) is data (the list of three elements)– (+ 2 (+ 1 2)) is 5, but– (+ 2 '(+ 1 2)) is an error (why?)

Page 19: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

19

More Lisp Features• Execution flow control

– order of code execution• Sequence (statement by statement)• Selection (conditionals)• Iteration (repetition, loops)

• Declaration – create new variables, functions

• Assignment – assign values to variables

• Input/Output – read from the keyboard/files– write to the screen/files

Page 20: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

20

Iteration Examples(do ((x 0) (y 99 -1)) (= x 100)

(setf (aref a x) (aref b y))

(when (= (aref a x) 15)

(return)))

(dotimes (x 100)

(format t "~%Number: ~a" x))

(loop do

(setq x (next-leaf my-tree))

(format t "~%This one? ~a" x)

while x)

(loop while (/= x 3) do

(terpri) (princ "Guess Again!")

(setq x (read)))

Page 21: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

21

(defun compute-pi (hex-digit)

(print (bbb-pi

hex-digit) ) )

(defun bbb-pi (n)

(let ((result 0.0))

(dotimes (x n)

(incf result (* (expt (/ 1.0 16) x)

(- (/ 4.0 (+ 1 (* 8 x)))

(/ 2.0 (+ 4 (* 8 x)))

(/ 1.0 (+ 5 (* 8 x)))

(/ 1.0 (+ 6 (* 8 x)))))))

result))

(compute-pi 9)

The “Miraculous”

Bailey-Borwein-Plouffe (BBP)

Pi Algorithm

• Can find the nth hexadecimal digit of π without knowing digits 0...n-1!

• Recently computed 10 billionth hexadecimal digit of π

(it’s 9)

dotimes Example

Page 22: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

22

Conditional statements - if• if special form:

– (if <test>

<then form>

<else form> )

– (if (= x 0)

0

(/ 10 x))

– Evaluates <test>• if true, evaluate <then form>• if false, evaluate < else form>

Page 23: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

23Conditional statements - cond

• cond special form syntax:• (cond <cond-clause>*)

where <cond-clause> is (<test form> <form>*)

• Example• (cond

((= x 1)

(print "x is a small number"))

((>= x 2)

(print "x is a larger number")))

Page 24: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

24

Evaluation of cond • (cond

((= x 1)

(print "x is a small number"))

((>= x 2)

(print "x is a larger number")))• Evaluate test in first clause (= x 1)

– If true, execute all other statements in the same clause,– Returns the result of last statement in that clause

• Else, evaluate the test in the next clause (= x 2)– If true, execute all other statements in the same clause,– Returns result of last statement in that clause

• Repeat until the first true test or until out of clauses

Page 25: 1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition

25

Function eval• Mostly used in Lisp’s REPL loop • eval can be called separately

– quote prevents the evaluation

> (setf s1 '(cadr '(one two three)))(CADR '(ONE TWO THREE))

> (eval s1)TWO

> (eval (list

'cdr (car

'((quote (a . b)) c))))B

• What is the result of– (eval (quote (list list)))