Professional Documents
Culture Documents
Lecture 21 LISP
Lecture 21 LISP
Programming
Languages
Lecture 21
Fakhar Lodhi
• Iteration: dotimes and dolist
– (dotimes (count n result) body)
• do body n times. count starts with 0, ends with n-1
> (product-as-sum 3 4)
> 12
Property Lists
1) Property lists:
Assign/access properties (attribute-value pairs) of a symbol
To assign a property: (setf (get object attribute) value)
To obtain a property: (get object attribute)
To see all the properties; (symbol-plist object)
Example:
>(setf (get 'a 'height) 8) ; cannot use "setq" here
8
>(symbol-plist ‘a)
(WEIGHT 20 HEIGHT 8)
Property Lists
> (remprop object attribute)
>(symbol-plist ‘a)
(HEIGHT 8)
>(symbol-plist ‘a)
NIL
Property lists
• Assume that if the name of a person’s father is
known, the father’s name is given as the value of
the father property. Define a function
GRANDFATHER that returns the name of a
person’s paternal grandfather, if known, or NIL
otherwise.
(aref chess-board 0 2)
WHITE-BISHOP
(aref chess-board 7 2)
BLACK-BISHOP
• Other functions in LISP library
1) Predicates:zerop, plusp, evenp, oddp, integerp, floatp
2) Logical connector: and, or, not
3) Rounding: floor, ceiling, truncate, round
4) Others:
max, min, abs, sqrt, 1+ (add 1), 1- (minus 1)