AD
Ari Diacou
Fri, Feb 24, 2017 11:10 PM
I am trying to write arithmetic, geometric and harmonic mean functions in
openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is undef, if
you uncomment the "f," you get 5 errors, not 3, which means there are too
many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_
Manual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
I am trying to write arithmetic, geometric and harmonic mean functions in
openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is undef, if
you uncomment the "f," you get 5 errors, not 3, which means there are too
many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_
Manual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
NH
nop head
Fri, Feb 24, 2017 11:18 PM
When i == len(values) you return values[i] but lists are indexed from 0, so
you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean functions in
openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is undef,
if you uncomment the "f," you get 5 errors, not 3, which means there are
too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
When i == len(values) you return values[i] but lists are indexed from 0, so
you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>
>
> I am trying to write arithmetic, geometric and harmonic mean functions in
> openscad.
>
> It appears I need to use recursion (
>
> Here is my program:
>
> echo(sum([1,2,3]));
> function sum(values,i=0)=(
> (i==len(values)) ?
> values[i] :
> values[i] + sum(values, //f,
> i+1)
> );
>
> The output should be 6. with the comment in line 5, the output is undef,
> if you uncomment the "f," you get 5 errors, not 3, which means there are
> too many calls.
>
> Can anyone give me some help?
>
> I took a look at the WikiBook, but it didnt help me much:
> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>
> Or if you have already written the functions I want (product, sum,
> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
> send me those.
>
> Thanks,
> Ari M Diacou, Metis Industries
>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
AD
Ari Diacou
Fri, Feb 24, 2017 11:20 PM
When i == len(values) you return values[i] but lists are indexed from 0,
so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean functions in
openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is undef,
if you uncomment the "f," you get 5 errors, not 3, which means there are
too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Thanks a lot nophead!
On Fri, Feb 24, 2017 at 6:18 PM, nop head <nop.head@gmail.com> wrote:
> When i == len(values) you return values[i] but lists are indexed from 0,
> so you are try to access one past the end, hence the undef.
>
>
>
> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>
>>
>>
>> I am trying to write arithmetic, geometric and harmonic mean functions in
>> openscad.
>>
>> It appears I need to use recursion (
>>
>> Here is my program:
>>
>> echo(sum([1,2,3]));
>> function sum(values,i=0)=(
>> (i==len(values)) ?
>> values[i] :
>> values[i] + sum(values, //f,
>> i+1)
>> );
>>
>> The output should be 6. with the comment in line 5, the output is undef,
>> if you uncomment the "f," you get 5 errors, not 3, which means there are
>> too many calls.
>>
>> Can anyone give me some help?
>>
>> I took a look at the WikiBook, but it didnt help me much:
>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>
>> Or if you have already written the functions I want (product, sum,
>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>> send me those.
>>
>> Thanks,
>> Ari M Diacou, Metis Industries
>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> Discuss@lists.openscad.org
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
NH
nop head
Fri, Feb 24, 2017 11:21 PM
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head nop.head@gmail.com wrote:
When i == len(values) you return values[i] but lists are indexed from 0,
so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean functions in
openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is undef,
if you uncomment the "f," you get 5 errors, not 3, which means there are
too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head <nop.head@gmail.com> wrote:
> When i == len(values) you return values[i] but lists are indexed from 0,
> so you are try to access one past the end, hence the undef.
>
>
>
> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>
>>
>>
>> I am trying to write arithmetic, geometric and harmonic mean functions in
>> openscad.
>>
>> It appears I need to use recursion (
>>
>> Here is my program:
>>
>> echo(sum([1,2,3]));
>> function sum(values,i=0)=(
>> (i==len(values)) ?
>> values[i] :
>> values[i] + sum(values, //f,
>> i+1)
>> );
>>
>> The output should be 6. with the comment in line 5, the output is undef,
>> if you uncomment the "f," you get 5 errors, not 3, which means there are
>> too many calls.
>>
>> Can anyone give me some help?
>>
>> I took a look at the WikiBook, but it didnt help me much:
>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>
>> Or if you have already written the functions I want (product, sum,
>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>> send me those.
>>
>> Thanks,
>> Ari M Diacou, Metis Industries
>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> Discuss@lists.openscad.org
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>>
>
AD
Ari Diacou
Fri, Feb 24, 2017 11:23 PM
Nice, I like that format better.
On Fri, Feb 24, 2017 at 6:21 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head nop.head@gmail.com wrote:
When i == len(values) you return values[i] but lists are indexed from 0,
so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean functions
in openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is undef,
if you uncomment the "f," you get 5 errors, not 3, which means there are
too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Nice, I like that format better.
On Fri, Feb 24, 2017 at 6:21 PM, nop head <nop.head@gmail.com> wrote:
> echo(sum([1,2,3]));
> function sum(values, i = 0) =
> i == len(values) ? 0 : values[i] + sum(values, i + 1);
>
> Works.
>
> On 24 February 2017 at 23:18, nop head <nop.head@gmail.com> wrote:
>
>> When i == len(values) you return values[i] but lists are indexed from 0,
>> so you are try to access one past the end, hence the undef.
>>
>>
>>
>> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>>
>>>
>>>
>>> I am trying to write arithmetic, geometric and harmonic mean functions
>>> in openscad.
>>>
>>> It appears I need to use recursion (
>>>
>>> Here is my program:
>>>
>>> echo(sum([1,2,3]));
>>> function sum(values,i=0)=(
>>> (i==len(values)) ?
>>> values[i] :
>>> values[i] + sum(values, //f,
>>> i+1)
>>> );
>>>
>>> The output should be 6. with the comment in line 5, the output is undef,
>>> if you uncomment the "f," you get 5 errors, not 3, which means there are
>>> too many calls.
>>>
>>> Can anyone give me some help?
>>>
>>> I took a look at the WikiBook, but it didnt help me much:
>>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>>
>>> Or if you have already written the functions I want (product, sum,
>>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>>> send me those.
>>>
>>> Thanks,
>>> Ari M Diacou, Metis Industries
>>>
>>>
>>> _______________________________________________
>>> OpenSCAD mailing list
>>> Discuss@lists.openscad.org
>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>
>>>
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
NH
nop head
Fri, Feb 24, 2017 11:25 PM
echo(sum([1,2,3]));
function sum(values, i = 0, sum = 0) =
i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
Is a better way to do it because it uses tail recursion. I.e. it ends by
calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
out of stack.
On 24 February 2017 at 23:23, Ari Diacou ari.diacou@gmail.com wrote:
Nice, I like that format better.
On Fri, Feb 24, 2017 at 6:21 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head nop.head@gmail.com wrote:
When i == len(values) you return values[i] but lists are indexed from 0,
so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean functions
in openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is
undef, if you uncomment the "f," you get 5 errors, not 3, which means there
are too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
echo(sum([1,2,3]));
function sum(values, i = 0, sum = 0) =
i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
Is a better way to do it because it uses tail recursion. I.e. it ends by
calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
out of stack.
On 24 February 2017 at 23:23, Ari Diacou <ari.diacou@gmail.com> wrote:
> Nice, I like that format better.
>
> On Fri, Feb 24, 2017 at 6:21 PM, nop head <nop.head@gmail.com> wrote:
>
>> echo(sum([1,2,3]));
>> function sum(values, i = 0) =
>> i == len(values) ? 0 : values[i] + sum(values, i + 1);
>>
>> Works.
>>
>> On 24 February 2017 at 23:18, nop head <nop.head@gmail.com> wrote:
>>
>>> When i == len(values) you return values[i] but lists are indexed from 0,
>>> so you are try to access one past the end, hence the undef.
>>>
>>>
>>>
>>> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>>>
>>>>
>>>>
>>>> I am trying to write arithmetic, geometric and harmonic mean functions
>>>> in openscad.
>>>>
>>>> It appears I need to use recursion (
>>>>
>>>> Here is my program:
>>>>
>>>> echo(sum([1,2,3]));
>>>> function sum(values,i=0)=(
>>>> (i==len(values)) ?
>>>> values[i] :
>>>> values[i] + sum(values, //f,
>>>> i+1)
>>>> );
>>>>
>>>> The output should be 6. with the comment in line 5, the output is
>>>> undef, if you uncomment the "f," you get 5 errors, not 3, which means there
>>>> are too many calls.
>>>>
>>>> Can anyone give me some help?
>>>>
>>>> I took a look at the WikiBook, but it didnt help me much:
>>>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>>>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>>>
>>>> Or if you have already written the functions I want (product, sum,
>>>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>>>> send me those.
>>>>
>>>> Thanks,
>>>> Ari M Diacou, Metis Industries
>>>>
>>>>
>>>> _______________________________________________
>>>> OpenSCAD mailing list
>>>> Discuss@lists.openscad.org
>>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>>
>>>>
>>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> Discuss@lists.openscad.org
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
AD
Ari Diacou
Fri, Feb 24, 2017 11:28 PM
That was the part of the wikibook i didn't understand, although I at least
updated the link to the wikipedia article.
On Fri, Feb 24, 2017 at 6:25 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0, sum = 0) =
i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
Is a better way to do it because it uses tail recursion. I.e. it ends by
calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
out of stack.
On 24 February 2017 at 23:23, Ari Diacou ari.diacou@gmail.com wrote:
Nice, I like that format better.
On Fri, Feb 24, 2017 at 6:21 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head nop.head@gmail.com wrote:
When i == len(values) you return values[i] but lists are indexed from
0, so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean functions
in openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is
undef, if you uncomment the "f," you get 5 errors, not 3, which means there
are too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
That was the part of the wikibook i didn't understand, although I at least
updated the link to the wikipedia article.
On Fri, Feb 24, 2017 at 6:25 PM, nop head <nop.head@gmail.com> wrote:
> echo(sum([1,2,3]));
>
> function sum(values, i = 0, sum = 0) =
> i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
>
> Is a better way to do it because it uses tail recursion. I.e. it ends by
> calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
> out of stack.
>
> On 24 February 2017 at 23:23, Ari Diacou <ari.diacou@gmail.com> wrote:
>
>> Nice, I like that format better.
>>
>> On Fri, Feb 24, 2017 at 6:21 PM, nop head <nop.head@gmail.com> wrote:
>>
>>> echo(sum([1,2,3]));
>>> function sum(values, i = 0) =
>>> i == len(values) ? 0 : values[i] + sum(values, i + 1);
>>>
>>> Works.
>>>
>>> On 24 February 2017 at 23:18, nop head <nop.head@gmail.com> wrote:
>>>
>>>> When i == len(values) you return values[i] but lists are indexed from
>>>> 0, so you are try to access one past the end, hence the undef.
>>>>
>>>>
>>>>
>>>> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>>>>
>>>>>
>>>>>
>>>>> I am trying to write arithmetic, geometric and harmonic mean functions
>>>>> in openscad.
>>>>>
>>>>> It appears I need to use recursion (
>>>>>
>>>>> Here is my program:
>>>>>
>>>>> echo(sum([1,2,3]));
>>>>> function sum(values,i=0)=(
>>>>> (i==len(values)) ?
>>>>> values[i] :
>>>>> values[i] + sum(values, //f,
>>>>> i+1)
>>>>> );
>>>>>
>>>>> The output should be 6. with the comment in line 5, the output is
>>>>> undef, if you uncomment the "f," you get 5 errors, not 3, which means there
>>>>> are too many calls.
>>>>>
>>>>> Can anyone give me some help?
>>>>>
>>>>> I took a look at the WikiBook, but it didnt help me much:
>>>>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>>>>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>>>>
>>>>> Or if you have already written the functions I want (product, sum,
>>>>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>>>>> send me those.
>>>>>
>>>>> Thanks,
>>>>> Ari M Diacou, Metis Industries
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> OpenSCAD mailing list
>>>>> Discuss@lists.openscad.org
>>>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>>>
>>>>>
>>>>
>>>
>>> _______________________________________________
>>> OpenSCAD mailing list
>>> Discuss@lists.openscad.org
>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>
>>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> Discuss@lists.openscad.org
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
NH
nop head
Fri, Feb 24, 2017 11:33 PM
The difference is the previous version has to add values[i] after it calls
itself, so it has to actually recurse. When the tail is just a call to
itself with nothing to do afterwards it can be made into a loop.
On 24 February 2017 at 23:28, Ari Diacou ari.diacou@gmail.com wrote:
That was the part of the wikibook i didn't understand, although I at least
updated the link to the wikipedia article.
On Fri, Feb 24, 2017 at 6:25 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0, sum = 0) =
i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
Is a better way to do it because it uses tail recursion. I.e. it ends by
calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
out of stack.
On 24 February 2017 at 23:23, Ari Diacou ari.diacou@gmail.com wrote:
Nice, I like that format better.
On Fri, Feb 24, 2017 at 6:21 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head nop.head@gmail.com wrote:
When i == len(values) you return values[i] but lists are indexed from
0, so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean
functions in openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is
undef, if you uncomment the "f," you get 5 errors, not 3, which means there
are too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
The difference is the previous version has to add values[i] after it calls
itself, so it has to actually recurse. When the tail is just a call to
itself with nothing to do afterwards it can be made into a loop.
On 24 February 2017 at 23:28, Ari Diacou <ari.diacou@gmail.com> wrote:
> That was the part of the wikibook i didn't understand, although I at least
> updated the link to the wikipedia article.
>
> On Fri, Feb 24, 2017 at 6:25 PM, nop head <nop.head@gmail.com> wrote:
>
>> echo(sum([1,2,3]));
>>
>> function sum(values, i = 0, sum = 0) =
>> i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
>>
>> Is a better way to do it because it uses tail recursion. I.e. it ends by
>> calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
>> out of stack.
>>
>> On 24 February 2017 at 23:23, Ari Diacou <ari.diacou@gmail.com> wrote:
>>
>>> Nice, I like that format better.
>>>
>>> On Fri, Feb 24, 2017 at 6:21 PM, nop head <nop.head@gmail.com> wrote:
>>>
>>>> echo(sum([1,2,3]));
>>>> function sum(values, i = 0) =
>>>> i == len(values) ? 0 : values[i] + sum(values, i + 1);
>>>>
>>>> Works.
>>>>
>>>> On 24 February 2017 at 23:18, nop head <nop.head@gmail.com> wrote:
>>>>
>>>>> When i == len(values) you return values[i] but lists are indexed from
>>>>> 0, so you are try to access one past the end, hence the undef.
>>>>>
>>>>>
>>>>>
>>>>> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>>>>>
>>>>>>
>>>>>>
>>>>>> I am trying to write arithmetic, geometric and harmonic mean
>>>>>> functions in openscad.
>>>>>>
>>>>>> It appears I need to use recursion (
>>>>>>
>>>>>> Here is my program:
>>>>>>
>>>>>> echo(sum([1,2,3]));
>>>>>> function sum(values,i=0)=(
>>>>>> (i==len(values)) ?
>>>>>> values[i] :
>>>>>> values[i] + sum(values, //f,
>>>>>> i+1)
>>>>>> );
>>>>>>
>>>>>> The output should be 6. with the comment in line 5, the output is
>>>>>> undef, if you uncomment the "f," you get 5 errors, not 3, which means there
>>>>>> are too many calls.
>>>>>>
>>>>>> Can anyone give me some help?
>>>>>>
>>>>>> I took a look at the WikiBook, but it didnt help me much:
>>>>>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>>>>>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>>>>>
>>>>>> Or if you have already written the functions I want (product, sum,
>>>>>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>>>>>> send me those.
>>>>>>
>>>>>> Thanks,
>>>>>> Ari M Diacou, Metis Industries
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> OpenSCAD mailing list
>>>>>> Discuss@lists.openscad.org
>>>>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>>>>
>>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> OpenSCAD mailing list
>>>> Discuss@lists.openscad.org
>>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>>
>>>>
>>>
>>> _______________________________________________
>>> OpenSCAD mailing list
>>> Discuss@lists.openscad.org
>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>
>>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> Discuss@lists.openscad.org
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
RP
Ronaldo Persiano
Sat, Feb 25, 2017 12:24 AM
There is some code about similar functions in the Tips and Tricks
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Add_all_values_in_a_list
section of the Manual. The simpler and non-recursive solution I know for
your case is:
function sum(values) = [for(p=values) 1]*values;
It works with list of numbers and list of vectors.
There is some code about similar functions in the Tips and Tricks
<https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks#Add_all_values_in_a_list>
section of the Manual. The simpler and non-recursive solution I know for
your case is:
function sum(values) = [for(p=values) 1]*values;
It works with list of numbers and list of vectors.
AD
Ari Diacou
Sat, Feb 25, 2017 2:33 AM
I think it works now:
A=[2,3,4];
m=-2;
echo(str("A=",A));
echo(str("power mean(A,",m,")=",power_mean(A,m)));
echo(str("arithmetic mean=",mean(A)));
echo(str("harmonic mean=",harmonic_mean(A)));
echo(str("sum=",sum(A)));
echo(str("product=",product(A)));
echo(str("invert(A)=",invert(A)));
echo(str("power_each(A,",m,")=",power_each(A,m)));
function sum(values,i=0)=(
//outputs the sum of a list of values
//uses recursion, but takes advantage of OpenSCAD's automatic looping
of certain recursive function formats.
i==len(values) ? 0 :
values[i] + sum(values, i+1)
);
function product(values,i=0)=(
//outputs the product of a list of values
i==len(values) ? 1 :
values[i]*product(values, i+1)
);
function mean(values,i=0)=(
//outputs the arithmetic mean of a list of values
//uses recursion, but takes advantage of OpenSCAD's automatic looping
of certain recursive function formats.
//See https://en.wikipedia.org/wiki/Mean#Arithmetic_mean
i==len(values) ? 0 :
values[i] + sum(values, i+1)
)/len(values);
function geometric_mean(values)=pow(product(values),1/len(values));
//outputs the geometric mean of a list of values //uses recursion, but
takes advantage of OpenSCAD's automatic looping of certain recursive
function formats. See https://en.wikipedia.org/wiki/Mean#Geometric_mean
function harmonic_mean(values,i=0)=len(values)/sum(invert(values));
//outputs the harmonic mean of a list of values //uses recursion, but takes
advantage of OpenSCAD's automatic looping of certain recursive function
formats. //See https://en.wikipedia.org/wiki/Mean#Harmonic_mean
function invert(values)=[for(i=[0:len(values)-1]) 1/values[i]]; //inverts
each value in a list
function power_each(values,n)=[for(i=[0:len(values)-1]) pow(values[i],n)];
//returns a list of each value in a list raised to the power of n. n can be
any value.
function
power_mean(values,m)=pow(sum(power_each(values,m))/len(values),1/m); //See
https://en.wikipedia.org/wiki/Mean#Generalized_means
On Fri, Feb 24, 2017 at 6:28 PM, Ari Diacou ari.diacou@gmail.com wrote:
That was the part of the wikibook i didn't understand, although I at least
updated the link to the wikipedia article.
On Fri, Feb 24, 2017 at 6:25 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0, sum = 0) =
i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
Is a better way to do it because it uses tail recursion. I.e. it ends by
calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
out of stack.
On 24 February 2017 at 23:23, Ari Diacou ari.diacou@gmail.com wrote:
Nice, I like that format better.
On Fri, Feb 24, 2017 at 6:21 PM, nop head nop.head@gmail.com wrote:
echo(sum([1,2,3]));
function sum(values, i = 0) =
i == len(values) ? 0 : values[i] + sum(values, i + 1);
Works.
On 24 February 2017 at 23:18, nop head nop.head@gmail.com wrote:
When i == len(values) you return values[i] but lists are indexed from
0, so you are try to access one past the end, hence the undef.
On 24 February 2017 at 23:10, Ari Diacou ari.diacou@gmail.com wrote:
I am trying to write arithmetic, geometric and harmonic mean
functions in openscad.
It appears I need to use recursion (
Here is my program:
echo(sum([1,2,3]));
function sum(values,i=0)=(
(i==len(values)) ?
values[i] :
values[i] + sum(values, //f,
i+1)
);
The output should be 6. with the comment in line 5, the output is
undef, if you uncomment the "f," you get 5 errors, not 3, which means there
are too many calls.
Can anyone give me some help?
I took a look at the WikiBook, but it didnt help me much:
https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
Or if you have already written the functions I want (product, sum,
aritmentic mean, harmonic mean, and geometric mean) of a list of values,
send me those.
Thanks,
Ari M Diacou, Metis Industries
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
I think it works now:
A=[2,3,4];
m=-2;
echo(str("A=",A));
echo(str("power mean(A,",m,")=",power_mean(A,m)));
echo(str("arithmetic mean=",mean(A)));
echo(str("harmonic mean=",harmonic_mean(A)));
echo(str("sum=",sum(A)));
echo(str("product=",product(A)));
echo(str("invert(A)=",invert(A)));
echo(str("power_each(A,",m,")=",power_each(A,m)));
function sum(values,i=0)=(
//outputs the sum of a list of values
//uses recursion, but takes advantage of OpenSCAD's automatic looping
of certain recursive function formats.
i==len(values) ? 0 :
values[i] + sum(values, i+1)
);
function product(values,i=0)=(
//outputs the product of a list of values
i==len(values) ? 1 :
values[i]*product(values, i+1)
);
function mean(values,i=0)=(
//outputs the arithmetic mean of a list of values
//uses recursion, but takes advantage of OpenSCAD's automatic looping
of certain recursive function formats.
//See https://en.wikipedia.org/wiki/Mean#Arithmetic_mean
i==len(values) ? 0 :
values[i] + sum(values, i+1)
)/len(values);
function geometric_mean(values)=pow(product(values),1/len(values));
//outputs the geometric mean of a list of values //uses recursion, but
takes advantage of OpenSCAD's automatic looping of certain recursive
function formats. See https://en.wikipedia.org/wiki/Mean#Geometric_mean
function harmonic_mean(values,i=0)=len(values)/sum(invert(values));
//outputs the harmonic mean of a list of values //uses recursion, but takes
advantage of OpenSCAD's automatic looping of certain recursive function
formats. //See https://en.wikipedia.org/wiki/Mean#Harmonic_mean
function invert(values)=[for(i=[0:len(values)-1]) 1/values[i]]; //inverts
each value in a list
function power_each(values,n)=[for(i=[0:len(values)-1]) pow(values[i],n)];
//returns a list of each value in a list raised to the power of n. n can be
any value.
function
power_mean(values,m)=pow(sum(power_each(values,m))/len(values),1/m); //See
https://en.wikipedia.org/wiki/Mean#Generalized_means
On Fri, Feb 24, 2017 at 6:28 PM, Ari Diacou <ari.diacou@gmail.com> wrote:
> That was the part of the wikibook i didn't understand, although I at least
> updated the link to the wikipedia article.
>
> On Fri, Feb 24, 2017 at 6:25 PM, nop head <nop.head@gmail.com> wrote:
>
>> echo(sum([1,2,3]));
>>
>> function sum(values, i = 0, sum = 0) =
>> i == len(values) ? sum : sum(values, i + 1, sum + values[i]);
>>
>> Is a better way to do it because it uses tail recursion. I.e. it ends by
>> calling itself. That is optimised to a loop by OpenSCAD, so it doesn't run
>> out of stack.
>>
>> On 24 February 2017 at 23:23, Ari Diacou <ari.diacou@gmail.com> wrote:
>>
>>> Nice, I like that format better.
>>>
>>> On Fri, Feb 24, 2017 at 6:21 PM, nop head <nop.head@gmail.com> wrote:
>>>
>>>> echo(sum([1,2,3]));
>>>> function sum(values, i = 0) =
>>>> i == len(values) ? 0 : values[i] + sum(values, i + 1);
>>>>
>>>> Works.
>>>>
>>>> On 24 February 2017 at 23:18, nop head <nop.head@gmail.com> wrote:
>>>>
>>>>> When i == len(values) you return values[i] but lists are indexed from
>>>>> 0, so you are try to access one past the end, hence the undef.
>>>>>
>>>>>
>>>>>
>>>>> On 24 February 2017 at 23:10, Ari Diacou <ari.diacou@gmail.com> wrote:
>>>>>
>>>>>>
>>>>>>
>>>>>> I am trying to write arithmetic, geometric and harmonic mean
>>>>>> functions in openscad.
>>>>>>
>>>>>> It appears I need to use recursion (
>>>>>>
>>>>>> Here is my program:
>>>>>>
>>>>>> echo(sum([1,2,3]));
>>>>>> function sum(values,i=0)=(
>>>>>> (i==len(values)) ?
>>>>>> values[i] :
>>>>>> values[i] + sum(values, //f,
>>>>>> i+1)
>>>>>> );
>>>>>>
>>>>>> The output should be 6. with the comment in line 5, the output is
>>>>>> undef, if you uncomment the "f," you get 5 errors, not 3, which means there
>>>>>> are too many calls.
>>>>>>
>>>>>> Can anyone give me some help?
>>>>>>
>>>>>> I took a look at the WikiBook, but it didnt help me much:
>>>>>> https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Man
>>>>>> ual/User-Defined_Functions_and_Modules&stable=0#Recursive_functions
>>>>>>
>>>>>> Or if you have already written the functions I want (product, sum,
>>>>>> aritmentic mean, harmonic mean, and geometric mean) of a list of values,
>>>>>> send me those.
>>>>>>
>>>>>> Thanks,
>>>>>> Ari M Diacou, Metis Industries
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> OpenSCAD mailing list
>>>>>> Discuss@lists.openscad.org
>>>>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>>>>
>>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> OpenSCAD mailing list
>>>> Discuss@lists.openscad.org
>>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>>
>>>>
>>>
>>> _______________________________________________
>>> OpenSCAD mailing list
>>> Discuss@lists.openscad.org
>>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>>
>>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> Discuss@lists.openscad.org
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>>
>