Re: Question concerning eval

2016-01-14 Thread adrian . medina
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 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.


Re: Question concerning eval

2016-01-14 Thread gianluca torta
Hi Adrian,

with your suggestion it certainly works also with avec'

however, I still don't see why the original code from Burt works with avec 
and does not work with avec'

cheers,
Gianluca

On Thursday, January 14, 2016 at 5:06:02 PM UTC+1, adrian...@mail.yu.edu 
wrote:
>
> 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 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.


Question concerning eval

2016-01-14 Thread Burt
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.


Re: Question concerning eval

2016-01-14 Thread gianluca torta
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.


Re: Question concerning eval

2016-01-14 Thread Burt
Hi Adrian, Gianluca,

I understand that quoting the  entire binding vector avec solves my 
problem. 
@Adrian Thanks for the hint.

But nethertheless, it's a riddle for me why there's a difference between my
original approaches.

cheers,
Burt

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


Re: Question concerning eval

2016-01-14 Thread gianluca torta
small update: I found that the problem happens because make-adder returns a 
closure; with a simple function it works:

(defn make-2-adder [] (fn [x] (+ 2 x)))
(def g2 (make-2-adder))
(eval `(let [~'f ~g2] (~'f 5)))
; --> 7


On Thursday, January 14, 2016 at 12:52:47 PM UTC+1, gianluca torta wrote:
>
> 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.