2 (:use [sicp [utils :only (square)]]
5 (defn square-list-1 [items]
6 (let [f (fn [things answer]
10 (cons (square (first things))
15 "This produced reversed list, as we cons into an empty list and
16 consing into a list always adds elements into the head as this
17 is the most efficient - O(1) - way to add elements into the list."
20 (deftest test-square-list
21 (is (= (square-list-1 '(1 2 3 4)) '(16 9 4 1))))
23 ;; Louis then tries to fix his bug by interchanging the arguments to cons:
25 (defn square-list-2 [items]
26 (let [f (fn [things answer]
31 (square (first things))))))]
35 "This won't work because a 'nil' in the cons cell represents the
36 end of the list and cons is used to construct a list by appending
37 elements at the head of the list and not by appending at the tail."