33
Control in LISP Control in LISP More on Predicates & More on Predicates & Conditionals Conditionals

Control in LISP More on Predicates & Conditionals

Embed Size (px)

Citation preview

Page 1: Control in LISP More on Predicates & Conditionals

Control in LISPControl in LISP

More on Predicates & ConditionalsMore on Predicates & Conditionals

Page 2: Control in LISP More on Predicates & Conditionals

Equality TestsEquality Tests

LISP provides multiple equality testsLISP provides multiple equality tests– EQUAL and =EQUAL and =– EQL and EQEQL and EQ

Serve different purposesServe different purposes– they give different answers in some casesthey give different answers in some cases

Page 3: Control in LISP More on Predicates & Conditionals

Equal and =Equal and =

EQUAL is for general comparisonsEQUAL is for general comparisons = is used for numeric comparisons= is used for numeric comparisons

– an error to use = with non-numbersan error to use = with non-numbers = does type conversion; EQUAL does not= does type conversion; EQUAL does not

– (= 15 15.0) returns T(= 15 15.0) returns T– (equal 15 15.0) returns NIL(equal 15 15.0) returns NIL

Page 4: Control in LISP More on Predicates & Conditionals

EqEq

EQ is “pointer” equalityEQ is “pointer” equality– are these “two” objects pointing to the same are these “two” objects pointing to the same

hunk of memoryhunk of memory T if same atom or same variableT if same atom or same variable MaybeMaybe T if same number & type T if same number & type> > (list (eq ‘a ‘a) (eq ‘(a) ‘(a)))(list (eq ‘a ‘a) (eq ‘(a) ‘(a)))

(T NIL)(T NIL)

Page 5: Control in LISP More on Predicates & Conditionals

Eq and SetFEq and SetF

> > (setf a ‘(1 2 3))(setf a ‘(1 2 3))

> > (setf b ‘(1 2 3))(setf b ‘(1 2 3))

> > (setf c a)(setf c a)

> > (list (eq a ‘(1 2 3)) (eq a a) (eq a b) (eq a c))(list (eq a ‘(1 2 3)) (eq a a) (eq a b) (eq a c))

(NIL T NIL T)(NIL T NIL T) A and B are different, but A and C are sameA and B are different, but A and C are same

– even tho’ they all look exactly the sameeven tho’ they all look exactly the same

Page 6: Control in LISP More on Predicates & Conditionals

Eql and EqEql and Eq

EQL is just like EQ EQL is just like EQ except for numbersexcept for numbers A number is always EQL to itselfA number is always EQL to itself

– it it may not bemay not be EQ to itself EQ to itself– neither EQ nor EQL if different typesneither EQ nor EQL if different types

> > (list (eql 100 100) (eql 100 100.0) (eq 100 100))(list (eql 100 100) (eql 100 100.0) (eq 100 100))

(T NIL T)(T NIL T) oror (T NIL NIL)(T NIL NIL)

Page 7: Control in LISP More on Predicates & Conditionals

EqualEqual

EQUAL is general purposeEQUAL is general purpose– except for numbers it’s what you’d expectexcept for numbers it’s what you’d expect

> > (setf a ‘(1 2 3))(setf a ‘(1 2 3))

> > (setf b ‘(1 2 3))(setf b ‘(1 2 3))

> > (list (equal a ‘(1 2 3)) (equal a a) (equal a b))(list (equal a ‘(1 2 3)) (equal a a) (equal a b))

(T T T)(T T T)

> > (list (equal 1 1.0) (equal a ‘(1.0 2.0 3.0)))(list (equal 1 1.0) (equal a ‘(1.0 2.0 3.0)))

(NIL NIL)(NIL NIL)

Page 8: Control in LISP More on Predicates & Conditionals

Equal is EqualEqual is Equal

EQUAL goes all the way downEQUAL goes all the way down– lists with lists treated properlylists with lists treated properly

> > (equal ‘(1 (2 (3 4) (5 6))) ‘(1 (2 (3 4) (5 6))))(equal ‘(1 (2 (3 4) (5 6))) ‘(1 (2 (3 4) (5 6))))

TT

> > (equal ‘(1 2 (3 4)) ‘(1 2 3 4))(equal ‘(1 2 (3 4)) ‘(1 2 3 4))

NILNIL

Page 9: Control in LISP More on Predicates & Conditionals

Testing for EqualityTesting for Equality

Use EQUAL if need to compare listsUse EQUAL if need to compare lists Use EQ or EQL for atoms/integersUse EQ or EQL for atoms/integers

– more efficient than equalmore efficient than equal Use = for general numbersUse = for general numbers

– only one that equates integers with floatsonly one that equates integers with floats

Page 10: Control in LISP More on Predicates & Conditionals

ExerciseExercise

True, false, either or error:True, false, either or error:(eq ‘a ‘a)(eq ‘a ‘a)

(eq 10 10)(eq 10 10)

(eq 10 10.0)(eq 10 10.0)

(eq ‘(10) ‘(10.0))(eq ‘(10) ‘(10.0))

(equal 10 10.0)(equal 10 10.0)

(equal ‘(10) ‘(10.0))(equal ‘(10) ‘(10.0))

(eql ‘a ‘a)(eql ‘a ‘a)

(eql 10 10)(eql 10 10)

(eql 10 10.0)(eql 10 10.0)

(eql ‘(10) ‘(10.0))(eql ‘(10) ‘(10.0))

(= 10 10.0)(= 10 10.0)

(= ‘(10) ‘(10.0))(= ‘(10) ‘(10.0))

Page 11: Control in LISP More on Predicates & Conditionals

Data Type PredicatesData Type Predicates

Can test an object to see whether it’s a Can test an object to see whether it’s a particular typeparticular type– ATOM, NUMBERP, SYMBOLP, LISTPATOM, NUMBERP, SYMBOLP, LISTP– (no P at the end of ATOM)(no P at the end of ATOM)

Numbers and symbols are also atomsNumbers and symbols are also atoms NIL is an atom and a listNIL is an atom and a list

Page 12: Control in LISP More on Predicates & Conditionals

Testing for TypeTesting for Type

> > (list (atom ‘a) (numberp ‘a) (symbolp ‘a) (listp ‘a))(list (atom ‘a) (numberp ‘a) (symbolp ‘a) (listp ‘a))(T NIL T NIL)(T NIL T NIL)

> > (list (atom 5) (numberp 5) (symbolp 5) (listp 5))(list (atom 5) (numberp 5) (symbolp 5) (listp 5))(T T NIL NIL)(T T NIL NIL)

> > (list (atom ()) (numberp ()) (symbolp ()) (listp ()))(list (atom ()) (numberp ()) (symbolp ()) (listp ()))(T NIL T T)(T NIL T T)

> > (list (atom ‘(a 1)) (numberp ‘(a 1))(list (atom ‘(a 1)) (numberp ‘(a 1))(symbolp ‘(a 1)) (listp ‘(a 1)))(symbolp ‘(a 1)) (listp ‘(a 1)))

(NIL NIL NIL T)(NIL NIL NIL T)

Page 13: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Evaluate:Evaluate:> > (setf a ‘(+ 2 5))(setf a ‘(+ 2 5))

> > (list (atom ‘a) (numberp ‘a))(list (atom ‘a) (numberp ‘a))

> > (list (atom a) (numberp a))(list (atom a) (numberp a))

> > (list (atom (+ 2 5)) (numberp (+ 2 5)))(list (atom (+ 2 5)) (numberp (+ 2 5)))

> > (list (listp ‘a) (listp a) (listp (+ 2 5)))(list (listp ‘a) (listp a) (listp (+ 2 5)))

> > (list (symbolp ‘a) (symbolp a) (symbolp (+ 2 5)))(list (symbolp ‘a) (symbolp a) (symbolp (+ 2 5)))

Page 14: Control in LISP More on Predicates & Conditionals

Number TestsNumber Tests

Simple tests on numbersSimple tests on numbers– ZEROP, PLUSP, MINUSP, EVENP, ODDPZEROP, PLUSP, MINUSP, EVENP, ODDP

All do what you’d expect, butAll do what you’d expect, but– errors if not given numberserrors if not given numbers– errors if given more than one argumenterrors if given more than one argument

> > (list (zerop 0) (plusp 1) (minusp 2) (evenp 3))(list (zerop 0) (plusp 1) (minusp 2) (evenp 3))

(T T NIL NIL)(T T NIL NIL)

Page 15: Control in LISP More on Predicates & Conditionals

Compound ConditionsCompound Conditions

LISP provides functions for logical LISP provides functions for logical combination of conditionscombination of conditions– AND, OR, NOTAND, OR, NOT

Can be used anywhere a condition is Can be used anywhere a condition is requiredrequired

Page 16: Control in LISP More on Predicates & Conditionals

Logical AndLogical And

All arguments must be true (All arguments must be true (i.e. i.e. non-NIL)non-NIL)– returns NIL if one argument is NILreturns NIL if one argument is NIL– returns last argument if all non-NILreturns last argument if all non-NIL

> > (and (member ‘a ‘(d a d)) (member ‘o ‘(m o m)))(and (member ‘a ‘(d a d)) (member ‘o ‘(m o m)))

(O M)(O M)

> > (and (member ‘a ‘(d a d)) (member ‘b ‘(m o m)))(and (member ‘a ‘(d a d)) (member ‘b ‘(m o m)))

NILNIL

Page 17: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Evaluate the following:Evaluate the following:> > (and (member ‘a ‘(d a d)) (member ‘u ‘(b u d)))(and (member ‘a ‘(d a d)) (member ‘u ‘(b u d)))

> > (and (member ‘a ‘(d a d)) (atom ‘(b u d)))(and (member ‘a ‘(d a d)) (atom ‘(b u d)))

> > (and (eq ‘a ‘a) (eql ‘a ‘a) (equal ‘a ‘a))(and (eq ‘a ‘a) (eql ‘a ‘a) (equal ‘a ‘a))

> > (and (eql 10 10.0) (= 10 10.0))(and (eql 10 10.0) (= 10 10.0))

> > (and (atom nil) (listp nil) (numberp nil))(and (atom nil) (listp nil) (numberp nil))

> > (and (numberp 10) (numberp 20) (+ 10 20))(and (numberp 10) (numberp 20) (+ 10 20))

Page 18: Control in LISP More on Predicates & Conditionals

Logical OrLogical Or

One argument must be non-NILOne argument must be non-NIL– returns NIL if all are NILreturns NIL if all are NIL– returns first non-NIL if one is non-NILreturns first non-NIL if one is non-NIL

> > (or (member ‘a ‘(d a d)) (member ‘b ‘(m o m)))(or (member ‘a ‘(d a d)) (member ‘b ‘(m o m)))

(A D)(A D)

> > (or (member ‘a ‘(d u d)) (member ‘b ‘(m i m)))(or (member ‘a ‘(d u d)) (member ‘b ‘(m i m)))

NILNIL

Page 19: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Evaluate the following:Evaluate the following:> > (or (member ‘a ‘(d a d)) (member ‘u ‘(b u d)))(or (member ‘a ‘(d a d)) (member ‘u ‘(b u d)))

> > (or (member ‘a ‘(b u d)) (atom ‘(b u d)))(or (member ‘a ‘(b u d)) (atom ‘(b u d)))

> > (or (eq ‘a ‘a) (eql ‘a ‘a) (equal ‘a ‘a))(or (eq ‘a ‘a) (eql ‘a ‘a) (equal ‘a ‘a))

> > (or (eql 10 10.0) (= 10 10.0))(or (eql 10 10.0) (= 10 10.0))

> > (or (atom ‘a) (listp ‘a) (numberp ‘a))(or (atom ‘a) (listp ‘a) (numberp ‘a))

> > (or (numberp 10) (numberp 20) (+ 10 20))(or (numberp 10) (numberp 20) (+ 10 20))

Page 20: Control in LISP More on Predicates & Conditionals

Logical NotLogical Not

Reverses its one argumentReverses its one argument– returns T if argument is NILreturns T if argument is NIL– returns NIL if argument is non-NILreturns NIL if argument is non-NIL

> > (not (member ‘a ‘(d a d)))(not (member ‘a ‘(d a d)))

NILNIL

> > (not (member ‘b ‘(m o m)))(not (member ‘b ‘(m o m)))

TT

Page 21: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Evaluate the following:Evaluate the following:> > (not (member ‘a ‘(d a d)))(not (member ‘a ‘(d a d)))

> > (not (member ‘u ‘(b a d)))(not (member ‘u ‘(b a d)))

> > (not (or (member ‘a ‘(d a)) (member ‘u ‘(u p))))(not (or (member ‘a ‘(d a)) (member ‘u ‘(u p))))

> > (not (and (member ‘a ‘(d a)) (member ‘u ‘(u p))))(not (and (member ‘a ‘(d a)) (member ‘u ‘(u p))))

> > (or (not (atom ‘a)) (not (listp ‘a)))(or (not (atom ‘a)) (not (listp ‘a)))

> > (and (not (evenp 3)) (or (oddp 8) (zerop 0)))(and (not (evenp 3)) (or (oddp 8) (zerop 0)))

Page 22: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Write a function that takes an atom and two Write a function that takes an atom and two lists, and says how many of the lists the lists, and says how many of the lists the atom is in: ‘none, ‘one, or ‘bothatom is in: ‘none, ‘one, or ‘both

> > (two-member ‘a ‘(s a d) ‘(d a d))(two-member ‘a ‘(s a d) ‘(d a d))

BOTHBOTH

> > (two-member ‘o ‘(c a l m) ‘(m o m))(two-member ‘o ‘(c a l m) ‘(m o m))

ONEONE

Page 23: Control in LISP More on Predicates & Conditionals

““Short-Circuit” EvaluationShort-Circuit” Evaluation

AND & OR are special formsAND & OR are special forms– only evaluate arguments until answer knownonly evaluate arguments until answer known– can “guard” conditionscan “guard” conditions

> > (defun eqn (M N)(defun eqn (M N)(and (numberp N) (numberp M) (= N M)))(and (numberp N) (numberp M) (= N M)))

> > (eqn 15 15.0)(eqn 15 15.0)TT> > (eqn ‘a ‘a)(eqn ‘a ‘a)NILNIL

Page 24: Control in LISP More on Predicates & Conditionals

Short CircuitsShort Circuits

> > (eqn 15 15.0)(eqn 15 15.0)

(and(and

(numberp 15) => T(numberp 15) => T

(numberp 15.0) => T(numberp 15.0) => T

(= 15 15.0) => T(= 15 15.0) => T

) => T) => T

> > (eqn ‘a ‘a)(eqn ‘a ‘a)

(and(and

(numberp ‘a) => NIL(numberp ‘a) => NIL

) => NIL) => NIL Doesn’t do:Doesn’t do:

(numberp ‘a) => NIL(numberp ‘a) => NIL

(= ‘a ‘a) => ERROR(= ‘a ‘a) => ERROR

AND stops as soon as it sees a NIL

Page 25: Control in LISP More on Predicates & Conditionals

Short CircuitsShort Circuits

OR stops as soon as it sees non-NILOR stops as soon as it sees non-NIL(setf L ‘(a))(setf L ‘(a))

(or (null L) (= (length L) 1) (eq (first L) (second L))(or (null L) (= (length L) 1) (eq (first L) (second L))

(or(or

(null ‘(a)) => NIL(null ‘(a)) => NIL

(= (length ‘(a)) 1) => (= 1 1) => T(= (length ‘(a)) 1) => (= 1 1) => T

) => T) => T Does not compare (first L) = a with (second L) = NIL

Page 26: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Write “safe” versions of EVENP & PLUSPWrite “safe” versions of EVENP & PLUSP– don’t give errors when called with non-don’t give errors when called with non-

numbersnumbers Version 1: use if, when or unlessVersion 1: use if, when or unless Version 2: use and, or or notVersion 2: use and, or or not

Page 27: Control in LISP More on Predicates & Conditionals

CaseCase

CASE special form simplifies CONDsCASE special form simplifies CONDs– all conditions (except else) involve checking all conditions (except else) involve checking

one object against othersone object against others

> > (defun fib (N)(defun fib (N)(cond ((= N 0) 1)(cond ((= N 0) 1)

((= N 1) 1)((= N 1) 1)(T (+ (fib (– N 1)) (fib (– N 2))))))(T (+ (fib (– N 1)) (fib (– N 2))))))

FIBFIB

Page 28: Control in LISP More on Predicates & Conditionals

CaseCase

CASE is a special formCASE is a special form– first argument is evaluatedfirst argument is evaluated– remaining arguments are notremaining arguments are not

> > (defun fib (N)(defun fib (N)(case N(case N

(0(0 1)1)(1(1 1)1)(T (T (+ (fib (– N 1)) (fib (– N 2))))))(+ (fib (– N 1)) (fib (– N 2))))))

Page 29: Control in LISP More on Predicates & Conditionals

Case CasesCase Cases

Cases appear a (LABEL FORM) pairsCases appear a (LABEL FORM) pairs First matching case is selectedFirst matching case is selected

– the form paired with it is evaluated & returnedthe form paired with it is evaluated & returned Matching:Matching:

– if LABEL is T or OTHERWISE, matchif LABEL is T or OTHERWISE, match– if LABEL is an atom, use EQL to compareif LABEL is an atom, use EQL to compare– if LABEL is a list, use member on that listif LABEL is a list, use member on that list

Page 30: Control in LISP More on Predicates & Conditionals

Compound CaseCompound Case

> > (defun fib2 (N)(defun fib2 (N)(case N(case N

((1 2)((1 2) 1)1)(T (T (+ (fib2 (– N 1)) (fib2 (– N (+ (fib2 (– N 1)) (fib2 (– N

2))))))2)))))) Calls (member N ‘(1 2)) for first caseCalls (member N ‘(1 2)) for first case Just succeeds for second caseJust succeeds for second case

Page 31: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Write a function to expand three-letter Write a function to expand three-letter abbreviations for days of the week to the abbreviations for days of the week to the full day name. Use a case statement.full day name. Use a case statement.– return NIL if the day is not recognizedreturn NIL if the day is not recognized

>> (day-full-name ‘wed) (day-full-name ‘wed)

WEDNESDAYWEDNESDAY

Page 32: Control in LISP More on Predicates & Conditionals

ExerciseExercise

Write a function with case statement to Write a function with case statement to return the number of days in a month (given return the number of days in a month (given as a 3-letter abbr.). Ignore leap years.as a 3-letter abbr.). Ignore leap years.– Thirty days hath September,Thirty days hath September,

April, June and November.April, June and November.All the rest have thirty-one,All the rest have thirty-one,

Save February all alone,Save February all alone,Which hath twenty eight days clear, Which hath twenty eight days clear, & so on& so on

Page 33: Control in LISP More on Predicates & Conditionals

Next TimeNext Time

More Control in LISPMore Control in LISP– Chapters 4, 5 & 7Chapters 4, 5 & 7