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