<div dir="ltr">I was wondering about why he calls this a "data constructor". Is this a general use of the idea of "date constructor?"</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Feb 13, 2021 at 1:50 AM Ut Primum <<a href="mailto:utprimum@gmail.com">utprimum@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Hi,<div dir="auto">the sequence defined in your example has been conjectured by Collatz to be finite for all initial n. But by now this property hasn't been proved. </div><div dir="auto">So, for what we know, there could be an integer such that the sequence starting form it is infinite (no such n has been found yet, but there could be one).</div><div dir="auto">Anyway, even if such n existed, you chose it, and wrote</div><div dir="auto"><br></div><div dir="auto">x=collatz n</div><div dir="auto"><br></div><div dir="auto">there would not be a stack overflow. This is because computations will be done "lazily", so just when really needed. So if you asked for the first, say, 10 elements of the list x, they would be computed without problems, even if the base case is never reached. Of course if you asked to print collatz n for an n that makes the sequence infinite, the printing would never end. But you could make many operations with the possibly infinite list, if they required just a finite (not necessarily a priori bounded) number of elements.</div><div dir="auto"><br></div><div dir="auto">So in Haskell you can define lists or other data structures that are infinite. Of course you must define the structure in such a way that to compute the "next" element of the list there is no need to look at "future" elements, but just at "past" ones.</div><div dir="auto"><br></div><div dir="auto">I hope this answers your question,</div><div dir="auto">Cheers,</div><div dir="auto">Ut</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il sab 13 feb 2021, 00:03 Galaxy Being <<a href="mailto:borgauf@gmail.com" target="_blank">borgauf@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I'm looking at <a href="https://stackoverflow.com/questions/23768413/building-a-list-from-left-to-right-in-haskell-without" rel="noreferrer" target="_blank">this</a> from Stackoverflow and wondering what is meant in the accepted answer when he says<div><br></div><div><i>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</i>:<br><br><font face="monospace">collatz :: Integer -> [Integer]<br>collatz n | n <= 1 = []<br>collatz n = n : collatz next where<br> next = if even n then div n 2 else n * 3 + 1</font></div><div><br></div><div>What is meant by lazy data constructor in this context?<br></div><div><br></div><div>LB</div></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" rel="noreferrer" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>