Hi Burt,

I have done some investigation, further reducing your issue to the 
following:

(def g (make-adder 2))
(eval `(let [~'f ~g] (~'f 5)))
; --> error IllegalArgumentException No matching ctor found for class 
user$make_adder$fn__7609 etc.

(defn h [x] (+ x 2))
(eval `(let [~'f ~h] (~'f 5)))
; --> 7

but it is still not completely clear to me why they behave differently

hth,
Gianluca



On Thursday, January 14, 2016 at 11:05:55 AM UTC+1, Burt wrote:
>
> Hi, 
>
> I got an exception using eval and I do not understand why!
> Can anybody explain what happens?
>
> Here is a small piece of code showing the problem:
>
> (defn eval-phi [avec phi]
>   (eval `(let ~avec ~phi)))
>
> ; The idea of eval-phi is the evaluation of
> ; arbitrary lists where the symbols are
> ; defined in avec
>
> ; example:
> ; in avec the symbol p is defined as true
> ; and the symbol foo as a function adding 2
>
> (def avec
>   ['p   true
>   'foo (fn [x] (+ 2 x))])
>
> ; here are two lists
> (def phi '(and p (= 7 (foo 5))))
> (def psi '(and (not p) (= 7 (foo 5))))
>
> ; and here their evaluation
> (eval-phi avec phi)
> ; => true
> (eval-phi avec psi)
> ; => false
>
> ; second approach:
> ; the attempt to give the definition of a
> ; function in avec by a function which
> ; returns a function leads to an exception:
>
> (defn make-adder [n]
>   (fn [x] (+ n x)))
>
> ((make-adder 2) 5)
> ; => 7
>
> ; here the second definition of a binding vector:
> (def avec'
>   ['p true
>    'foo (make-adder 2)])
>
> (eval-phi avec' phi)
> ; CompilerException java.lang.ExceptionInInitializerError
>
> ; in my mental model of the substitution of symbols by 
> ; their value there should be no difference between the
> ; first approach and the second.
>
> ; Actually avec' contains the function returned by make-adder
> ((nth avec' 3) 5)
> ; => 7
>
> ; Why does avec' leads to an exception?
>
>
> *Kind regards,*
>
> *Burkhardt Renz*
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to