Lecture 6: In Class Activity
Solution Key
Problem 1
(defmacro first-item (lst)
(list 'car lst)
)
(defmacro rest-of-items (lst)
(list 'cdr lst)
)
Problem 2
(defmacro first-item2 (lst)
`(car ,lst)
)
(defmacro rest-of-items2 (lst)
`(cdr ,lst)
)
Problem 3
(defmacro for (var init final &rest body)
(let ((start-var (gensym)) ; Gensym's prevent variable capture
(count (gensym))
(fin (gensym))
)
`(do ((,start-var ,var) ; Bound only on first iteration
(b (boundp ,var)) ; -- Prevents multiple evaluation
(,fin ,final)
(,count ,init (+ ,count 1))
)
((= ,fin ,count) (if b
(setf ,var ,start-var)
(makunbound ,var)
)
)
(setf ,var ,count)
,@body
)
)
)
Back to main page...