On Wed, Nov 26, 2008 at 4:40 PM, André Thieme
<[EMAIL PROTECTED]> wrote:
>
> On 26 Nov., 21:26, Chouser <[EMAIL PROTECTED]> wrote:
>> On Wed, Nov 26, 2008 at 3:22 PM, Mark Volkmann
>>
>> <[EMAIL PROTECTED]> wrote:
>>
>> > I'm curious about this syntax. I thought if #(function-name args)
>> > creates a closure then I can put one in a variable and execute it
>> > laterI entered this in a REPL.
>>
>> > (def myClosure #(prn "Hello"))
>>
>> > How can I execute the closure in myClosure now?
>
> Hello Mark, maybe something got mixed up here:
> #(prn "Hello") looks like an ordinary anonymous function to me,
> and not like a closure.
> Well, it depends on how we want to define "closure".
> I think the definition should include that it must be a function
> (be it a named or anon one) that closes over some lexical
> environment. In my opinion this environment should not be the
> trivial one: the empty one.
> We could do this, but in that case every function would be a
> closure. So from that I would say that the given example was
> not for a closure.
>
> Maybe a simple accumulator could work here:
> user> (let [acc (ref 0)]
>        (defn make-accumulator [n]
>          (dosync (ref-set acc n))
>          (fn [i]
>            (dosync (alter acc + i)))))
> #'user/make-accumulator
>
> So, here make-accumulator is a closure.
> It closes over acc.
> And the anon function that is returned by
> make-accumulator is also a closure, as it
> also closes over acc.

Thanks! In the meantime I came up with an even simpler example that
demonstrates capturing a parameter and a local variable in a closure.

(defn greet-closure [name]
  (let [salutation "Hello"]
    #(println salutation name)))
(def my-closure (greet-closure "Mark"))
(my-closure)

outputs "Hello Mark"

-- 
R. Mark Volkmann
Object Computing, Inc.

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

Reply via email to