this is because in order to use ~ to evaluate the fn, you need to use the
backtick ` instead of the single quote '
Il giorno 13/lug/2011 00.34, "Paul Meehan" <paulm...@gmail.com> ha scritto:
>
> Sorry to repoen the discussion, but I got this:
>
>
> => (def alist '(1 ~(fn [x y](+ x y))))
> #'user/alist
>
> => (apply (second alist) '(1 3))
>
> #<CompilerException java.lang.ClassCastException: clojure.lang.Cons cannot
be cast to clojure.lang.IFn (NO_SOURCE_FILE:0)>
>
>
> On 07/12/2011 23:13, Benny Tsai wrote:
>>
>> Nice, yes, that works just as well :)
>>
>> On Tuesday, July 12, 2011 4:09:09 PM UTC-6, LaPingvino wrote:
>>>
>>> Didn't try, but shoudn't (def alist `(1 ~(fn [x y] (+ x y)))) be better?
Then you don't need the eval :)
>>>
>>> 2011/7/12 Benny Tsai <benny...@gmail.com>
>>>>
>>>> alist is missing parens around the fn, so it's really a list of 4
elements like so:
>>>>
>>>>
>>>>   (1, fn, [x y], (+ x y))
>>>>
>>>> So (second alist) returns just the symbol 'fn.  And when you apply a
Symbol to a list, the result is the last item in the list (not sure why).
>>>>
>>>> To do what you want, alist should be defined like this:
>>>>
>>>>   (def alist '(1 (fn [x y] (+ x y))))
>>>>
>>>> Also, it seems that the definition of the function needs to be eval'ed
first to become an apply-able function.  The following returns 4 in my REPL:
>>>>
>>>>   (apply (eval (second alist)) '(1 3))
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To post to this group, send email to clo...@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+u...@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 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 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 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

Reply via email to