Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

Modern

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

> (defun product-as-sum (n m)


(let ((result 0))
(dotimes (count n result)
(setf result (+ result m)))))

> (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

>(get 'a 'height)


8

>(setf (get ‘a ‘weight) 20)


20

>(symbol-plist ‘a)
(WEIGHT 20 HEIGHT 8)
Property Lists
> (remprop object attribute)

> (remprop ‘a ‘WEIGHT)


T

>(symbol-plist ‘a)
(HEIGHT 8)

> (remprop ‘a ‘HEIGHT)


T

>(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.

(defun grandfather (x)


(if (get x ‘father)
(get (get x ‘father) ‘father)
nil))
Property lists
• Make a list of the paternal line
(defun lineage (x)
(if x
(cons (x (lineage (get x ‘father))))))
Property lists
• Make a list of the paternal line
(defun ancestors (x)
(if x
(cons x (append (ancestors (get x ‘father))
(ancestors (get x ‘mother))))))
Arrays
• Data type to store expressions in places
identified by integer indexes.

• (setf linear-array (make-array ‘(4)))


• A single dimension array by the name of
linear-array with indices from 0 to 3.

• (setf linear-array (make-array 4))


• (setf chess-board (make-array ‘(8 8)))
Arrays
aref - array reference

(setf (aref chess-board 0 1) ‘WHITE-KNIGHT)


(setf (aref chess-board 7 4) ‘BLACK-KING)

(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)

(exp number) (base-e exponential)


(expt Base-number Power-Number)
(log number & Optional base-number)

(isqrt number) Returns the greater integer less than or equal to


the exact positive square-root of the number.

(signum number) Returns -1, zero, or 1 according if the


number is negative, zero, or positive.
Summary
• Atoms and lists
• Functions and function calls
setq, setf, set, quote, eval,
math functions (+, -, *, /, max, min, exp, sqrt, …)
list operations: list, cons, car, cdr, length, nth, append,
reverse
predicates (=, >, equal, eq, numberp, symbolp, …)
• Defining functions
(defun func_name (arg_list) func_body)
dolist, dotimes, cond, if, when, unless, mapcar
• Properties and associative lists: get, assoc
• Input/output: print, read, with-open-file, load
What made Lisp Different
• Conditionals: such as if-then-else construct.
• Function types: where functions are just like integers
and strings
• Recursion: first language to support it.
• Dynamic typing: all variable are pointers.
• Garbage-Collection.
• Programs composed of expressions.
• A symbol type.
• The whole program is a mathematical function

You might also like