Hm... this way you have to setup a ref and a transaction, but its local:

user=> (let [a (ref 0)] (dosync (ref-set a 1)))
1
user=> a
java.lang.Exception: Unable to resolve symbol: a in this context
(NO_SOURCE_FILE:0)
-Ralf
On Mon, Dec 1, 2008 at 10:07 PM, jim <[EMAIL PROTECTED]> wrote:

>
> The rule of thumb I used when I started with Clojure was if I'm
> tempted to use a def inside a function, I'm doing something wrong.
> Now, I'm not even tempted.
>
> (def mut-local)
> (defn f []
>   (binding [mut-local 0]
>      (set! mut-local 1)
>      mut-local))
>
> Is the same as what you wrote.
>
> Functional programming is a totally different mind set than imperative
> programming.  You might pick up "The Little Schemer" and "The Seasoned
> Schemer".  They do a good job of changing your perspective.
>
> Jim
>
> On Dec 1, 2:55 pm, puzzler <[EMAIL PROTECTED]> wrote:
> > I'm often copying "classic algorithms" from various sources, and it is
> > true that it can take some thought to do a functional conversion.
> > Sometimes, it's easier to just go ahead and use mutable local
> > variables.  Fortunately, Clojure lets you do mutability if needed.
> >
> > But I'm still unclear on the best way to do that.  To simulate mutable
> > locals, would you use refs or vars?  My own instinct would be to use
> > vars for a temporary mutable local, and refs for something that the
> > closure has to remember (like memoization).
> >
> > Here's how I think you could do a mutable local:
> > (defn f []
> >   (def mut-local)
> >   (binding [mut-local 0]
> >      (set! mut-local 1)
> >      mut-local))
> >
> > (f) should yield 1
> >
> > The flaw with this approach is that def actually creates a global
> > var.  It remains unbound, but the fact that the function ends up
> > having a global effect is less than satisfying.
> >
> > Better ways?
> >
>

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