2
votes

I'm trying some lazy streams in Clojure. If I do:

(defn ints-from [n]
   (cons n (lazy-seq (ints-from (inc n)))))

and

(def nats (ints-from 0))

it's fine, I can do something like:

(take 5 nats)

Now I'm trying to encapsulate the 2 functions in 1:

(defn natz[]
( letfn   [(aux [n]((cons n (lazy-seq (aux (inc n)))))) ] (aux 0) ))

This seems to compile, but does not do what I expect.

(take 4 natz)

gives:

(user=> IllegalArgumentException Don't know how to create ISeq from: user$natz    
clojure.lang.RT.seqFrom (RT.java:494)

What am I missing?

1

1 Answers

5
votes

One parenthesis less inside letfn definition and one parenthesis more to invoke natz function

(defn natz[]
  (letfn [(aux [n] (cons n (lazy-seq (aux (inc n)))))]
    (aux 0)))

Example usage:

(take 4 (natz))
=> (0 1 2 3)