# max() of more than 2 values

 Classic List Threaded
10 messages
Reply | Threaded
Open this post in threaded view
|

## max() of more than 2 values

 Hi,I would like to get the biggest element of a vector.I tried this: a=[1,3,5,8,1,2]; function maximum(a,i)=(i
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 You have a missing closing bracket. Should be: function maximum(a,i)=(i
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 Also should be i+1 I think, plus the 0 would cause problems with negative numbers and does look at the last element. function maximum(a, i = 0) = (i < len(a) - 1) ? max(a[i], maximum(a, i +1)) : a[i]; Works and doesn't require the zero to be passed explicitly. On 22 April 2013 15:02, nop head wrote: You have a missing closing bracket. Should be: function maximum(a,i)=(i
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 Thanks nop head.Using function with recursion is so cool. But thinking about it still ties my head in a knot.Greetings,TakeItAndRun On 22 April 2013 22:12, nop head wrote: Also should be i+1 I think, plus the 0 would cause problems with negative numbers and does look at the last element. function maximum(a, i = 0) = (i < len(a) - 1) ? max(a[i], maximum(a, i +1)) : a[i]; Works and doesn't require the zero to be passed explicitly. On 22 April 2013 15:02, nop head wrote: You have a missing closing bracket. Should be: function maximum(a,i)=(i
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 It would be a bit easier if OpenScad had list slicingfunction maximum(a) =(len(a) > 1) ? max(a[i], maximum(a[1:])) : a[0]; On 22 April 2013 21:51, Peter Falke wrote: Thanks nop head.Using function with recursion is so cool. But thinking about it still ties my head in a knot. Greetings,TakeItAndRun On 22 April 2013 22:12, nop head wrote: Also should be i+1 I think, plus the 0 would cause problems with negative numbers and does look at the last element. function maximum(a, i = 0) = (i < len(a) - 1) ? max(a[i], maximum(a, i +1)) : a[i]; Works and doesn't require the zero to be passed explicitly. On 22 April 2013 15:02, nop head wrote: You have a missing closing bracket. Should be: function maximum(a,i)=(i
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 Administrator On 2013-04-22, at 17:03 , nop head wrote: > It would be a bit easier if OpenScad had list slicing > > function maximum(a) =(len(a) > 1) ? max(a[i], maximum(a[1:])) : a[0]; > I think this list slicing syntax fits well into the OpenSCAD language. Perhaps we should clarify the whole concept of ranges and generalize it to have meaning for e.g. lists and strings? Any opinions on that? See this page for ideas: https://github.com/openscad/openscad/wiki/List-and-Array-operations   -Marius _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 I would do the slicing exactly the same as Python, including negative indices to denote indexing from the end. I don't think lists should be mutable in keeping with all variables, so list = [0,1,2,3]; list[1] = 4; should not be allowed. list2 = list[:1] ~ [4] ~ list[2:]; On 22 April 2013 22:16, Marius Kintel wrote: On 2013-04-22, at 17:03 , nop head wrote: > It would be a bit easier if OpenScad had list slicing > > function maximum(a) =(len(a) > 1) ? max(a[i], maximum(a[1:])) : a[0]; > I think this list slicing syntax fits well into the OpenSCAD language. Perhaps we should clarify the whole concept of ranges and generalize it to have meaning for e.g. lists and strings? Any opinions on that? See this page for ideas: https://github.com/openscad/openscad/wiki/List-and-Array-operations  -Marius _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad http://openscad.org - https://flattr.com/thing/121566 _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 On Mon, 22 Apr 2013 22:39:42 +0100 nop head <[hidden email]> wrote: > I would do the slicing exactly the same as Python, including negative > indices to denote indexing from the end. > > I don't think lists should be mutable in keeping with all variables, so list > = [0,1,2,3]; list[1] = 4; should not be allowed. > > list2 = list[:1] ~ [4] ~ list[2:]; I do wonder if for non programmers having the built in functions do sensible things with vectors where it makes sense would also be helpful ? ie max([1,2,3,4,5,6]);  [1,2,3,4,5,6] * 1.5  etc _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 Yes extending max and min would make things easier. The second example already works.On 22 April 2013 22:52, Alan Cox wrote: On Mon, 22 Apr 2013 22:39:42 +0100 nop head <[hidden email]> wrote: > I would do the slicing exactly the same as Python, including negative > indices to denote indexing from the end. > > I don't think lists should be mutable in keeping with all variables, so list > = [0,1,2,3]; list[1] = 4; should not be allowed. > > list2 = list[:1] ~ [4] ~ list[2:]; I do wonder if for non programmers having the built in functions do sensible things with vectors where it makes sense would also be helpful ? ie max([1,2,3,4,5,6]);  [1,2,3,4,5,6] * 1.5  etc _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad http://openscad.org - https://flattr.com/thing/121566 _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

## Re: max() of more than 2 values

 OpenSCAD is linked with https://www.bountysource.com if you want to start a bounty. The bounties are linked to issues on github. On Mon, Apr 22, 2013 at 4:59 PM, nop head wrote: Yes extending max and min would make things easier. The second example already works.On 22 April 2013 22:52, Alan Cox wrote: On Mon, 22 Apr 2013 22:39:42 +0100 nop head <[hidden email]> wrote: > I would do the slicing exactly the same as Python, including negative > indices to denote indexing from the end. > > I don't think lists should be mutable in keeping with all variables, so list > = [0,1,2,3]; list[1] = 4; should not be allowed. > > list2 = list[:1] ~ [4] ~ list[2:]; I do wonder if for non programmers having the built in functions do sensible things with vectors where it makes sense would also be helpful ? ie max([1,2,3,4,5,6]);  [1,2,3,4,5,6] * 1.5  etc _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad http://openscad.org - https://flattr.com/thing/121566 _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad http://openscad.org - https://flattr.com/thing/121566 _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566