(de natsum (N) (if (=0 N) 0 ( + N ( natsum ( dec N)))))
Like that? Am 10.02.2017 15:28 schrieb "Mike Pechkin" <mike.pech...@gmail.com>: > hi, > > On Fri, Feb 10, 2017 at 3:07 PM, Christopher Howard < > christopher.how...@qlfiles.net> wrote: > > > Hi list. When I try to do > > > > (apply '+ (range 1 1000000) > > > > > List of millions of items is not a problem. > Problem how you use it. > (apply) is not for free, It *creates* a function call with a million of > arguments. > Stack size make sense. > > > > I get segfault. I thought maybe this was some kind of internal > > limitation of the apply function, so I defined a foldl: > > > > (de foldl (Fn Acc Lst) > > (if (== () Lst) Acc > > (let Acc2 (Fn Acc (car Lst)) > > (foldl Fn Acc2 (cdr Lst)) ) ) ) > > > > : (foldl '+ 0 (range 1 1000)) > > (foldl '+ 0 (range 1 1000)) > > -> 500500 > > : (foldl '+ 0 (range 1 1000000)) > > (foldl '+ 0 (range 1 1000000)) > > > > > > Stack size makes sense too. > I like recursions this way: > > (de rec1 (F A L) > (if L > (rec1 F (inc 'A (++ L)) L) > A ) ) > > recursion with hidden accumulator from outer call and car-cdr: > (de rec2 (F L A) > (default A 0) > (if L > (rec2 F (cdr L) (inc 'A (car L))) > A ) ) > The call will be: (rec2 '+ (range 1 1000000))) > > > As recursion just loop you can implement it without big stack too: > (de sum1 (L) > (let N 0 > (for I L > (inc 'N I) ) ) ) > > even without list creation: > (de sum2 (X) > (let N 0 > (for I X > (inc 'N I) ) ) ) > > > And finally, that's why (sum) was implemented: > (sum prog (range 1 1000000))) > > > Bonus: for practice write recursion function to sum numbers without (range)