2

Is there a way to describe arbitrary lazy self-recursive data structures in Clojure? Let's say for example I wanted to do something like this:

(def inf-seq (fn rec [] (lazy-seq (cons 42 (rec)))))
(take 3 (inf-seq))

but with a map:

(def inf-map (fn rec [] (??? {:a (rec) :b 42})))
(get-in (inf-map) [:a :a :a :b])
1
  • as a completely different approach, you could use Clojure and Frege in the same project and code your lazy recursive data structures in Frege, which is almost Haskell. Commented Nov 28, 2019 at 18:51

1 Answer 1

2

Sequence laziness does not apply to deferred function evaluation in Clojure, which you would obviously need for constructing infinitely nested maps.

Try using Delays:

user=> (def inf-map (fn rec [] {:a (delay (rec)) :b 42}))
#'user/inf-map
user=> (inf-map)
{:a #<Delay@4e9f9a19: :pending>, :b 42}
user=> @(:a (inf-map))
{:a #<Delay@5afd479c: :pending>, :b 42}
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.