Hi,..
Having just completed reading "Purely Functional Data Structures"
(Chris Okasaki), I'm keen to try things out in Clojure..  Something
used fairly often in the book is the notion of a suspension.  It's
effectively a way to mark an expression as being lazy and memoized.

I've not seen anything equivalent in Clojure, and I wonder if someone
knows how I can do it.  Effectively I'd like to write code as follows:

=> (var t (suspend (* 42 42)))  ; 42 * 42 will not be evaluated yet..

=> (force t)  ; Only now will 42 * 42 be evaluated..
1764

=> (force t)  ; Returns the memoized value, (* 42 42) is *not* re-evaluated
1764


In practice this would be used for heavier expressions than these.. :)

Also, it must act a bit like a closure in that it should capture the
context at creation time, making it possible to write a function which
returns a suspension.

=> (defn create-suspension [x] (suspend (* x x)))

=> (var t (create-suspension 3))

=> (var u (create-suspension 6))

=> (force t)
9

=> (let [x 100] (force u))  ; Uses previously captured value of 'x'..
36


Finally, I would not expect identical suspensions to automagically
find each other.

=> (var t (suspend (* 42 42))

=> (var u (suspend (* 42 42))

=> (force t)  ; (* 42 42) will be evaluated and memoized
1764

=> (force u)  ; (* 42 42) will be evaluated again and memoized separately
1764


Any hints would be appreciated, I suspect there may be some way to
hack this using lazy-seq.


-- 
Paul Richards
@pauldoo

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

Reply via email to