lazy sequence question

1 view
Skip to first unread message

Mike K

unread,
Dec 10, 2009, 12:35:11 AM12/10/09
to Clojure
I'm working my way through "Programming Clojure" and got an unexpected
result with sequences:

user> (take 10 (filter even? (iterate inc 1)))
(2 4 6 8 10 12 14 16 18 20)
user> (take-while #(< % 10) (iterate inc 1))
(1 2 3 4 5 6 7 8 9)
user> (take 10 (filter #(< % 10) (iterate inc 1)))
; Evaluation aborted.

The first two work but the third one hangs. Why?

Thanks,
Mike

Mike K

unread,
Dec 10, 2009, 12:41:43 AM12/10/09
to Clojure
On Dec 9, 10:35 pm, Mike K <mbk.li...@gmail.com> wrote:

> The first two work but the third one hangs.  Why?

user> (take 5 (filter #(< % 10) (iterate inc 1)))
(1 2 3 4 5)

OK, I figured out that it won't hang with taking <= 9 elements, which
is the total that pass the filter.

But shouldn't it give me 9 items without hanging when I ask for 10 or
more as in the first case?

Mike

Mark Engelberg

unread,
Dec 10, 2009, 12:46:50 AM12/10/09
to clo...@googlegroups.com
There are only 9 items that satisfy your predicate. (take 10 ...)
demands a 10th, and it keeps searching the (iterate inc 1) stream
forever, endlessly searching for that 10th item it will never find.
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Richard Newman

unread,
Dec 10, 2009, 12:46:54 AM12/10/09
to clo...@googlegroups.com
> But shouldn't it give me 9 items without hanging when I ask for 10 or
> more as in the first case?

No.

take returns a lazy sequence. The printer is trying to realize it in
order to print it. It can't be completely realized until it's taken
ten elements (at which point it's done, by definition).

In realizing the sequence, the first nine items are collected. take
wants one more. It tries to fetch it from the filtered lazy-seq.

The filtered seq gets 10 from the iterate lazy-seq, which doesn't pass
the filter. It gets 11, which doesn't pass the filter...

Neither filter nor take know to abandon their attempt. That's how this
works. Imagine if you wrote:

user=> (take 10 (filter #(or (= 1000 %) (< % 10)) (iterate inc 1)))
(1 2 3 4 5 6 7 8 9 1000)


It just keeps on going until it's found ten elements.

Mike K

unread,
Dec 10, 2009, 1:07:18 AM12/10/09
to Clojure
> Neither filter nor take know to abandon their attempt. That's how this  
> works.

Ah, of course. Thanks Mark and Richard!

Mike

ataggart

unread,
Dec 10, 2009, 2:48:36 AM12/10/09
to Clojure
On Dec 9, 9:46 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> There are only 9 items that satisfy your predicate.  (take 10 ...)
> demands a 10th, and it keeps searching the (iterate inc 1) stream
> forever, endlessly searching for that 10th item it will never find.


Aww. You make it sound so sad.
Reply all
Reply to author
Forward
0 new messages