I'm new to Clojure, just thought I would share this.
I was playing around, trying to understand Atoms and I devised a
function that generates the next value in the Fibonacci sequence each
time it is called.

(def fib-gen-val (atom [1 1]))

(defn fib-gen []
  (let [[a b] @fib-gen-val]
   (swap! fib-gen-val #(let [[x y] %] (vector y (+ x y))))
    a))

Repeated calls to fib-gen return 1 1 2 3 5 ...etc

I could not figure out how to make a proper generator which somehow
initializes its own secret internal state (atom). In particular, I
couldn't find a function (exists? x) which would return true if the
thing x already exists, so that it would be created just once. Then,
how would the particular generator know which such secret atom
belonged to it?

[This is the sort of thing that Icon does (much more generally) with
its generators.
I think it's an interesting exercise to try to implement some sort of
general-purpose Icon-like generator control structure.
Also interesting would be something like the string-scanning control
structure that Icon has.]


--~--~---------~--~----~------------~-------~--~----~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to