(declaim (inline single?))
(defun single? (lst)
(and (consp lst) (null (cdr lst))))
(defun foo (x)
(single? (bar x)))
(macroexpand-1 'foo)
2.Rewrite the following function to be tail-recursive. How much faster is it when compiled?
(defun foo (x)
(if (zerop x)
0
(+ 1 (foo (1- x)))))
(defun new-foo (x)
(labels ((tail (l a)
(if (zerop l)
a
(tail (1- l) (1+ a)))))
(tail x 0)))
(time (new-foo 10000))
(time (foo 10000))
3.Add declarations to the following programs. How much faster can you make them?
a)The date arithmetic code in Section 5.7.(section-5-7 Example: Date Arithmetic)
()
b)The ray-tracer in Section 9.8.(section-9-8 Example: Ray-Tracing)
()
4.Rewrite the breadth-first search code in Section 3.15 so that it conses as little as possible.
section-4-7 Example: Binary Search Trees
()