*sigh* Never mind, I am being dumb.

factorial : Int -> Int
factorial x =
    Trampoline.evaluate (fac 1 x)


fac : Int -> Int -> Trampoline Int
fac acc n =
    if n <= 0 then
        Trampoline.done acc
    else
        Trampoline.jump (\_ -> fac (n * acc) (n - 1))


On Tue, Jul 12, 2016 at 9:17 AM, Nick H <[email protected]> wrote:

> ... and I have no idea how this is supposed to work. Even for primitive
> recursion
>
> factorial : Int -> Int
> factorial x =
>     Trampoline.evaluate (fac x)
>
>
> fac : Int -> Trampoline Int
> fac x =
>     if x <= 0 then
>         Trampoline.done 1
>     else
>         Trampoline.jump
>             (\_ ->
>                 ???
>             )
>
>
>
> On Tue, Jul 12, 2016 at 7:25 AM, Nick H <[email protected]> wrote:
>
>> Ian, that is a great thought, but even if we split the list exactly in
>> half, the call stack fills up when the list length gets to around 4500.
>>
>> partition _ list =
>>   let
>>     n = List.length list // 2
>>   in
>>     (List.take n list, List.drop n list)
>>
>> In my specific problem, the "wrap" function finds the smallest
>> rectangular prism that encloses a set of 3D points. The "partition"
>> function splits the list of points in half, with a plane that is aligned
>> with one of the box's axes. I have a lot of tests to ensure the function
>> doesn't break in weird cases, like all of the points being identical. So I
>> am pretty sure the partition is doing a reasonable split.
>>
>> I am taking a look at the trampoline library now...
>>
>> On Tue, Jul 12, 2016 at 12:03 AM, Janis Voigtländer <
>> [email protected]> wrote:
>>
>>> Maybe the Trampoline library can help you?
>>>
>>> http://package.elm-lang.org/packages/elm-lang/trampoline/1.0.0/Trampoline
>>>
>>>
>>> Am Dienstag, 12. Juli 2016 schrieb Nick H :
>>>
>>>> Here is a puzzle for you:
>>>>
>>>> For a large enough list, the code below will fill up the call stack and
>>>> crash. It makes sense that the Elm compiler can't optimize it with tail
>>>> calls, since the recursive function gets called more than once per
>>>> iteration.
>>>>
>>>> What is the best way to rewrite this without using recursion?
>>>>
>>>>
>>>>
>>>> type Tree = Leaf Foo | Node Bar Tree Tree
>>>>>
>>>>> create : List Foo -> Tree
>>>>> create faces =
>>>>>   case faces of
>>>>>     [] ->
>>>>>       dummyFoo
>>>>>
>>>>>     foo :: [] ->
>>>>>       Leaf f
>>>>>
>>>>>     list ->
>>>>>       let
>>>>>         box = wrap list
>>>>>
>>>>>         ( left, right ) = partition box list
>>>>>       in
>>>>>         Node box (create left) (create right)
>>>>>
>>>>> wrap : List Foo -> Bar
>>>>>
>>>>> partition : Bar -> List Foo -> (List Foo, List Foo)
>>>>>
>>>>> --
>>>> 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.
>>>>
>>> --
>>> 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.
>>>
>>
>>
>

-- 
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