You need to quote the entire vector `avec`, because otherwise the function
you want to bind to `foo` will be evaluated before being evaluated by the
`let` special operator, which expects only standard readable object
literals to be embedded.
Try changing:
(def avec
['p true
'foo (fn [x] (+ 2 x))])
to:
(def avec
'[p true
foo (fn [x] (+ 2 x))])
On Thursday, January 14, 2016 at 5:05:55 AM UTC-5, 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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.