# for loop in a function Classic List Threaded 7 messages Open this post in threaded view
|

## for loop in a function

 Hi I haven't been able to get a for loop working in a function. I'm trying to add the values in a vector from a start point to an end point. Something like this: AVector = [2,5,3,11,4]; start = 1; end = 3; //addVector would return 19 function addVector (aVector,start,end) Any help would be great. Thanks! -- 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: for loop in a function

 From the manual: ``` // recursion - find the sum of the values in a vector (array) by calling itself // from the start (or s'th element) to the i'th element - remember elements are zero based function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s)); vec=[ 10, 20, 30, 40 ]; echo("sum vec=", sumv(vec,2,1)); // calculates 20+30=50 ``` On Wed, Apr 11, 2018 at 11:34 AM, Mint86 wrote:Hi I haven't been able to get a for loop working in a function. I'm trying to add the values in a vector from a start point to an end point. Something like this: AVector = [2,5,3,11,4]; start = 1; end = 3; //addVector would return 19 function addVector (aVector,start,end) Any help would be great. Thanks! -- Sent from: http://forum.openscad.org/ _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: for loop in a function

 And just for fun, a non-recursive version using the c-style for loop (needs to be enabled as experimental feature in the dev snapshots)... function addVector(aVector, start, end) = [ for (i = end, a = aVector[i];i >= start;i = i - 1, a = a + aVector[i]) a ][end - start]; I guess it's more a puzzle kind of solution as I think the recursive one is more obvious and due to the tail-recursion elimination maybe even faster than the one above :). ciao,    Torsten. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org -- Torsten
Open this post in threaded view
|

## Re: for loop in a function

 In reply to this post by frankv > function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s)); Depending on the application, you might want something like: ```function sumv(list,start,end) = start>=end? 0 : list[start]+sumv(list,start+1,end) ; ``` so that it terminates on bad input. Of course that version doesn't take advantage of the overloading on + the way that the one above does. Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: for loop in a function

 Awesome! Thanks for the quick replies. They both are what I was looking for. Good tip NateTG. -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org