I must be misunderstanding something about how to define functions. I’m doing this SICP exercise, “Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.”
I try this in the REPL, and it seems to work:
=> (reduce + (map (fn [x] (* x x)) (rest (sort '(2 1 0)))))
5
But defining a procedure and replacing the numbers with parameters gives me an error:
=> (defn my-procedure [a b c] (reduce + (map (fn [x] (* x x)) (rest (sort '(a b c))))))
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.Number clojure.lang.Numbers.multiply (Numbers.java:146)
What am I not seeing? Thanks!
This should work for you:
here is why:
When you use a quote form, the form is not evaluated and your vars are not resolved. You are left with a list of symbols in your case.
Rather than create an un-evaluated form, you should use the
listfunction or another collection