sicpclojure icon indicating copy to clipboard operation
sicpclojure copied to clipboard

Wrong rewrite of a function in clojure

Open adomas-sk opened this issue 5 years ago • 0 comments

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

adomas-sk avatar Jan 20 '20 08:01 adomas-sk