[Haskell-beginners] Recursive calls inside lazy data constructors?

Galaxy Being borgauf at gmail.com
Fri Feb 12 23:01:44 UTC 2021


I'm looking at this
<https://stackoverflow.com/questions/23768413/building-a-list-from-left-to-right-in-haskell-without>
from Stackoverflow and wondering what is meant in the accepted answer when
he says

*In Haskell you can safely do recursive calls inside lazy data
constructors, and there will be no risk of stack overflow or divergence.
Placing the recursive call inside a constructor eliminates the need for an
accumulator, and the order of elements in the list will also correspond to
the order in which they are computed*:

collatz :: Integer -> [Integer]
collatz n | n <= 1 = []
collatz n = n : collatz next where
    next = if even n then div n 2 else n * 3 + 1

What is meant by lazy data constructor in this context?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210212/10d6d8f3/attachment.html>


More information about the Beginners mailing list