If I have a list
a = [1, 5, 7, 90, 10];
how would I add all the values together to get 115?
--
View this message in context: http://forum.openscad.org/How-to-add-all-the-values-in-a-list-together-tp12925.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
On 06/22/2015 09:21 PM, cn145912 wrote:
If I have a list
a = [1, 5, 7, 90, 10];
how would I add all the values together to get 115?
One option (should work even with long lists, if I did not mess up the tail recursion...):
function sum(list, idx = 0, result = 0) = idx >= len(list) ? result : sum(list, idx + 1, result + list[idx]);
a = [1, 5, 7, 90, 10];
echo(sum(a));
ECHO: 113
ciao,
Torsten.
I get 113:
a = [1, 5, 7, 90, 10];
function sum(a,i)=(i==0)?a[0]:a[i]+sum(a,i-1);
echo(sum(a,4));
echo(1+5+7+90+10);
2015-06-22 21:21 GMT+02:00 cn145912 neil.penning+openscad@gmail.com:
If I have a list
a = [1, 5, 7, 90, 10];
how would I add all the values together to get 115?
--
View this message in context:
http://forum.openscad.org/How-to-add-all-the-values-in-a-list-together-tp12925.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
--
stempeldergeschichte@googlemail.com karsten@rohrbach.de
P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu
schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.
P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.
Enjoy!
ah. but to get 115 your function needs to add +2 to whatever it finds!
ha.
Jerry
--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer
The most exciting phrase to hear in science - the one that heralds new
discoveries - is not "Eureka!" but "That's funny...".- Isaac. Asimov
I
*f you give someone a program, you will frustrate them for a day; if you
teach them how to program, you will frustrate them for a lifetime. *-
Anonymous
If writing good code requires very little comments, then writing really
excellent code requires no comments at all!- Ken Thompson
On Mon, Jun 22, 2015 at 12:55 PM, Peter Falke <
stempeldergeschichte@googlemail.com> wrote:
I get 113:
a = [1, 5, 7, 90, 10];
function sum(a,i)=(i==0)?a[0]:a[i]+sum(a,i-1);
echo(sum(a,4));
echo(1+5+7+90+10);
2015-06-22 21:21 GMT+02:00 cn145912 neil.penning+openscad@gmail.com:
If I have a list
a = [1, 5, 7, 90, 10];
how would I add all the values together to get 115?
--
View this message in context:
http://forum.openscad.org/How-to-add-all-the-values-in-a-list-together-tp12925.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
--
stempeldergeschichte@googlemail.com karsten@rohrbach.de
P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu
schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.
P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.
Enjoy!
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org