Using Clojure 1.2, this code works
(defn A [] (fn [a] (+ a 2)))
(println ((A) 0))
(println (eval (list (A) 0)))
but the following code fails on the third line
(defn A [b] (fn [a] (+ a b)))
(println ((A 3) 0))
(println (eval (list (A 3) 0)))
Why?
Calling
(list (A 3))returns a function in a list:evalexpects to get a symbol in a list and it was getting the function it’s self. if you quote the call to(A 3)then you will get the result you seekPart of this code is being evaluated before the call to
evaland thenevalis evaluating the rest. It is more common to se eval used on quoted forms with perhaps a term orto selectively unquoted (
~).or
edit: on the question of why it works with zero args and fails with one arg:
it seems that both forms work if you don’t store them in vars (ie define them with defn)
and instead manually inline them: