[Nickle] Fwd: nickle slow for large factorials

Michel Alexandre Salim michael.silvanus at gmail.com
Mon Jul 5 01:01:21 PDT 2010


On Mon, Jul 5, 2010 at 6:23 AM, Keith Packard <keithp at keithp.com> wrote:
> On Mon, 5 Jul 2010 02:48:37 +0200, Michel Alexandre Salim <michael.silvanus at gmail.com> wrote:
>
>> Ah, thanks. Any reason why the memory consumption is so high even the
>> tail-recursive implementation? Memory usage should be not much worse
>> than whatever it takes to represent 666666! in memory.
>
> You can't do trivial tail recursion in the factorial function -- you
> have to perform additional computation after each recursive function:
> The killer here is the n* -- without that, you have a simple tail
> recursive function. Refactor this into a pure tail call:
>
>     int fact2(n,m) {
>         if (n <= 1)
>            return m;
>         return fact2(n-1,n*m);
>     }
>     int fact(n) { return fact2(n,1); }
>
> and now you get the tail call behaviour.
>
I forgot to mention that this is in fact what I implemented, and htop
still reports memory consumption in the hundreds of megabytes, and !
does so too. I wonder if nickle's numerical system is leaking memory?

Thanks,

-- 
Michel


More information about the Nickle mailing list