On Wed, Aug 26, 2009 at 6:40 PM, Sourav <[email protected]> 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))))
>
(defn foo2 [n]
(let [r (ref n)]
#(dosync
(alter r + %) @r))))
Try the above. The difference between #() and (fn ...) was not the root of
your problem. You had an extra paren before dosync.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---