Nabble has removed Mailing-list integration.
Posts created here DO NOT GET SENT TO THE MAILING LIST.
Mailing-list emails DO NOT GET POSTED TO THE FORUM.
So basically the Forum is now out of date, we are looking into migrating the history.

# My latest functional programming challenge, the cumulative sum ... Classic List Threaded 6 messages Open this post in threaded view
|

## My latest functional programming challenge, the cumulative sum ...

 I'm sort of enjoying this struggle a little. But it is hard getting the head around this functional programming paradigm. I came across a beautiful implementation of a sum: function sum(list, i = 0) =  i < len(list) - 1 ?  list[i] + sum(list, i + 1) : list[i]; works a char. I want to calculate a cumulative sum, using a generator of course such that each element in the new vector is the sum of all previous vectors and itself. But I figured a place to start is to fix the sum above which works forwards to one that works backwards. To clarify the sum above appears to me. Takes a sample vector V=[1, 2, 3]: sum(V) = 1 + 2 + 3 sum(V,1) = 2 + 3 sum(V,2) = 3 I wanted just as an exercise to reverse this logic, so that: sum(V) = 3 + 2 + 1 sum(V,1) = 2 + 1 sum(V,2) = 1 I don't NEED this, it was just an idea  as you can see a cumulative sum in that output emerging already. So I tried: function sum(list, i = 0) =  i < len(list) - 1 ?  list[len(list)-1-i] + sum(list, i + 1)  : list[len(list)-1-i]; and if I single step this in my mind it seems right: sum(V) = 3 + sum(V,1) sum(V,1) = 2 + sum(V,2) sum(V,2) = 1 but OpenSCAD returns a result of ... 8. Clearly I've misunderstood something. Of course as noted this isn't essential. It's an exercise. My goal is to get a vector of cumulative sums back. Regards, Bernd. -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: My latest functional programming challenge, the cumulative sum ...

Open this post in threaded view
|

## Re: My latest functional programming challenge, the cumulative sum ...

Open this post in threaded view
|

## Re: My latest functional programming challenge, the cumulative sum ...

 In reply to this post by RevarBat I can't see anything wrong with that code, assuming the words all mean what I think they mean, apart from it's going backwards ;) I don't know OpenSCAD very well, but I'm not too bad at programming. Here's my full program:         function sum(list, i = 0) =  i < len(list) - 1 ?  list[len(list)-1-i]  + sum(list, i + 1)  : list[len(list)-1-i];         echo( sum([1,2,3]) );         echo( sum([10, 8, 6]) ); And here's the output when I press F5:         ECHO: 6         ECHO: 24 That looks fine to me. Bit of "debugging"...         function sum(list, i=0) =             let ( result = dosum(list, i) )             echo(str("sum(", list, ", ", i, " = ", result) )             result;         function dosum(list, i) =             i < len(list) - 1 ?                 list[len(list)-1-i]  + sum(list, i + 1)                 : list[len(list)-1-i];         echo( sum([1,2,3]) );         echo( sum([10, 8, 6]) ); Result:         ECHO: "sum([1, 2, 3], 2 = 1"         ECHO: "sum([1, 2, 3], 1 = 3"         ECHO: "sum([1, 2, 3], 0 = 6"         ECHO: 6         ECHO: "sum([10, 8, 6], 2 = 10"         ECHO: "sum([10, 8, 6], 1 = 18"         ECHO: "sum([10, 8, 6], 0 = 24"         ECHO: 24 -- The University of Glasgow, charity number SC004401 On Tuesday, 28 April 2020 11:25:41 BST Revar Desmera wrote: > So I tried: > > function sum(list, i = 0) =  i < len(list) - 1 ?  list[len(list)-1-i] + > > sum(list, i + 1)  : list[len(list)-1-i]; > > > > and if I single step this in my mind it seems right: > > > > sum(V) = 3 + sum(V,1) > > sum(V,1) = 2 + sum(V,2) > > sum(V,2) = 1 > > > > but OpenSCAD returns a result of ... 8. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org