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/
Here’s one implementation of a cumulative sum: https://github.com/revarbat/BOSL2/blob/e32735296cc173890a549f73d8506224af2eef8e/math.scad#L458
-Revar
On Apr 28, 2020, at 1:16 AM, ThumbOne via Discuss discuss@lists.openscad.org wrote:
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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Your question made me remember my own steps years ago. Those challenges are
a right track to master the unique nature of OpenSCAD.
I see no trouble with your solution. After all, the following:
echo(sum([1,2,3]));
echo(sum([1,2,3],1));
echo(sum([1,2,3],2));
results in:
ECHO: 6
ECHO: 3
ECHO: 1
for your later version of sum(). How have you gotten 8?
Em ter., 28 de abr. de 2020 às 09:16, ThumbOne via Discuss <
discuss@lists.openscad.org> escreveu:
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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
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.
OK, so I left the ')' out of the echo, but you get the gist. Do I get a B? ;)
echo(str("sum(", list, ", ", i, " ) = ", result) )
On Wednesday, 29 April 2020 13:14:08 BST Dr Nicholas Bailey wrote:
echo(str("sum(", list, ", ", i, " = ", result) )
You may want to browse
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks
& examine some of the
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Libraries#Other_Libraries
Admin - email* me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
--
Sent from: http://forum.openscad.org/