sicpclojure
sicpclojure copied to clipboard
Wrong rewrite of a function in clojure
In Chapter 1 exercise 1.6 the function
(defn new-if [predicate then-clause else-clause]
(cond (predicate) then-clause
:else else-clause))
is incorrect, because when one tries to use it
(new-if (= 2 3) 0 5)
it throws an error because the predicate is evaluated before the new-if function is called and then the function tries to evaluate the predicate again (predicate)
. The correct function should be
(defn new-if [predicate then-clause else-clause]
(cond predicate then-clause
:else else-clause))