The code is TCO'ed but look at the data structure:  the code is 
successively calling the `Succ` function to build a successively nested 
record structure, which overflows the stack at about 5000 recursive nests 
in the case of the OP.

On Saturday, 21 January 2017 01:36:36 UTC+7, OvermindDL1 wrote:
>
> On Friday, January 20, 2017 at 11:17:32 AM UTC-7, mrbackend (Roy Brokvam) 
> wrote:
>>
>> Consider:
>> type Nat
>>     = Zero
>>     | Succ Nat
>>
>>
>> nat : Int -> Nat
>> nat n =
>>     nat_ n Zero
>>
>>
>> nat_ : Int -> Nat -> Nat
>> nat_ n curr =
>>     if n <= 0 then
>>         curr
>>     else
>>         nat_ (n - 1) (Succ curr)
>>
>> Using tail recursion, this should be stack safe in Elm (as far as I 
>> understand). But I get a stack overflow for large values of n (>= 5000). Is 
>> my function really tail recursive, or might there be a bug in Elm?
>>
>  
> It compiles into this:
> ```elm
> var _user$project$Temp1484937158714761$Succ = function (a) {
> return {ctor: 'Succ', _0: a};
> };
> var _user$project$Temp1484937158714761$nat_ = F2(
> function (n, curr) {
> nat_:
> while (true) {
> if (_elm_lang$core$Native_Utils.cmp(n, 0) < 1) {
> return curr;
> } else {
> var _v0 = n - 1,
> _v1 = _user$project$Temp1484937158714761$Succ(curr);
> n = _v0;
> curr = _v1;
> continue nat_;
> }
> }
> });
> var _user$project$Temp1484937158714761$Zero = {ctor: 'Zero'};
> var _user$project$Temp1484937158714761$nat = function (n) {
> return A2(_user$project$Temp1484937158714761$nat_, n, 
> _user$project$Temp1484937158714761$Zero);
> };
> ```
> So it does look TCO'd, what is the exact stack trace that you got?
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to