lazy seqs overflow the stack?

111 views
Skip to first unread message

Ben Wolfson

unread,
Mar 2, 2013, 2:38:24 PM3/2/13
to clo...@googlegroups.com
Try it and see:

(reduce (fn [acc _] (concat acc acc)) '() (range 1750))

blows up with a stack overflow error. Changing the inner expression to
(doall (concat acc acc)) avoids the issue, but (obviously) also
requires giving up laziness.

This is admittedly fairly insanely nested, but I would have expected it to work.

--
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks,
which may be sweet, aromatic, fermented or spirit-based. ... Family
and social life also offer numerous other occasions to consume drinks
for pleasure." [Larousse, "Drink" entry]

Marko Topolnik

unread,
Mar 2, 2013, 2:56:47 PM3/2/13
to clo...@googlegroups.com
It's a known issue. I guess it could be avoided if some kind of trampolining scheme was introduced instead of the current recursive one in LazySeq.java.

Stuart Sierra

unread,
Mar 12, 2013, 2:34:20 PM3/12/13
to clo...@googlegroups.com
Yes. `concat` in a loop is tricky: each additional concatenation creates a new lazy sequence object which points to the previous lazy sequence. If you get too many of those, trying to realize the lazy sequence will cause a stack overflow.

In general, I recommend using `concat` only in recursive functions that return lazy sequences, and never in a `loop` or `reduce`. If you want to accumulate a sequential result non-lazily, use `into` and a vector.

-S
Reply all
Reply to author
Forward
0 new messages