





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























Lisp Lecture #
Logical Operators
Truth values = T (true) and NIL (false)
Everything but NIL has a truth value of T - even 0!!! (and t t) => t; (and 1 0) => t ; (or nil nil 0) => t; (xor 1 t) => nil (not t) =>nil; (not nil) => t; (not 45) => nil;
*Logical operators only evaluate as many arguments (left to right) as necessary to determine the truth value of the expression.
(and nil (* 88 99 66)) => nil ;; the * is never evaluated
(and (* 88 99) nil) => nil ;; the * IS evaluated due to L-R eval order
(not (and (or nil nil) (xor 1 0))) => t ;; the xor is never evaluated
a (^) b
Dotted Pairs
(cons โa nil) => (a) (cons โa (cons โb nil)) => (a b) (cons โa โ(b)) => (a b) (cons โa โb) => (a. b) (cons โ(a b) โc) => ((a b). c) (cons (cons โa โb) โc) => ((a. b). c)
a a b
a b
c
a (^) b
c
(car โ(a. b)) => a (cdr โ(a. b)) => b ; symbol (cdr โ(a b)) => (b) ; cons cell
arguments
Optional argument
More on Lisp Internals
collection
Efficiency Considerations
res is optional argument Argument list
Variable Assignment & Access
(setf y (+ 3 5)) => y assigned the value 8 (setf y โ((a 1) (b 2) (c 3))) => y assigned to the data list
(setf (second y) 88) => 88 y => ((a 1) 88 (c 3))
SETQ
(setf x (+ 3 5)) => x assigned the value 8
Access
Anytime a variable appears, it will be evaluated (i.e. its value will be accessed), unless the variable appears inside the scope of a single or double quote, or is an argument to a macro.
x => 8
โx => x
Evaluating LISP Expressions (2)
A single quoted list evaluates to a simple list of symbols
โ(a b c) => (a b c)
โ(+ 2 3) => (+ 2 3)
An unquoted list evaluates to a function call
(+ 2 3) => 5
(a b c) => ERROR: attempt to call an undeclared function โa
Evaluate arguments left-to-right, then call function:
(setf bob 5 bill 7 sue 9)
(+ bob bill sue) => 21 ;; (Calls + with 3 values: 5, 7, 9)
Polish notation: (operator operand1 operand2 โฆ operandn)
(+ 2 3 4 5) simpler than 2 + 3 + 4 + 5 + 6 if you can live with all the parentheses!!
Parentheses => no need for arithmetic precedence rules
(+ (* a b) (* c d)) -vs- ab + cd Docsity.com
Depth-First Evaluation Order
List Constructors
CONS (โconstructโ)
(cons โa โ(a b c)) => (a a b c); (cons โ(a b) โ(c d)) => โ((a b) c d) (cons โa nil) => (a); (cons nil โ(a b)) => (nil a b)) = (() a b)
(list โa โb) => (a b); (list โ(a b) โc โd) => ((a b) c d)
(append โ(a b c) โ(d e f)) => (a b c d e f); (append nil โ(a b)) => (a b)
These are not destructive :
(setf x โ(a b c)) (cons 62 x) => (62 a b c) x => (a b c)
cons