practical-language-learning

exercise-13

  1. Test whether your compiler observes inline declarations.
  2. Rewrite the following function…
  3. Add declarations to the following programs….
  4. Rewrite the breadth-first search…
  5. Modify the binary search tree code in Section 4.7 to use pools.

Test whether your compiler observes inline declarations.

(declaim (inline single?))

(defun single? (lst)
  (and (consp lst) (null (cdr lst))))

(defun foo (x)
  (single? (bar x)))

(macroexpand-1 'foo)

Rewrite the following function…

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

Add declarations to the following programs….

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.

Modify the binary search tree code in Section 4.7 to use pools.

section-4-7 Example: Binary Search Trees

()