I knew I was missing something...!! Thanks all for your help! :)

On Aug 27, 12:19 am, Scott Moonen <smoo...@andstuff.org> wrote:
> Hi Soura.  I think you have an extra set of parentheses in your second
> example, on the line with dosync.  It should read:
>
> (defn foo2 [n]
>  (let [r (ref n)]
>    #(dosync
>        (alter r + %) @r)))
>
>   -- Scott
>
>
>
>
>
> On Wed, Aug 26, 2009 at 2:40 PM, Sourav <soura.ja...@gmail.com> wrote:
>
> > Hi,
>
> > I'm new to clojure and came from a Lisp background. While learning I
> > clojure I came accross the two different ways of creating anonymous
> > functions ((fn ...) and #(...)). I tried to construct the accumulator
> > function in clojure using these forms and this is what I wrote (this
> > might seem naive but I'm just a beginner :)
>
> > 1.
>
> > (defn foo [n]
> >  (let [r (ref n)]
> >    (fn [i]
> >       (dosync
> >         (alter r + i) @r))))
> > And it works fine:
>
> > user> (def f (foo 10))
> > #'user/f
> > user> (f 1)
> > 11
> > user> (f 1)
> > 12
>
> > 2.
> > (defn foo2 [n]
> >  (let [r (ref n)]
> >    #((dosync
> >        (alter r + %) @r))))
>
> > This does not work:
> > user> (def f (foo2 10))
> > #'user/f
> > user> (f 1)
> > ; Evaluation aborted.
> > java.lang.ClassCastException: java.lang.Integer cannot be cast to
> > clojure.lang.IFn (NO_SOURCE_FILE:0)
> >  [Thrown class clojure.lang.Compiler$CompilerException]
>
> > However, another different function defined below just works:
>
> > user> (defn bar [n]
> >        #(+ n %))
> > #'user/bar
> > user> (def b (bar 10))
> > #'user/b
> > user> (b 1)
> > 11
>
> > Obviously I might be missing something... Please help.
>
> --http://scott.andstuff.org/ |  http://truthadorned.org/- Hide quoted text -
>
> - Show quoted text -

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