DS
Dan Shriver
Sat, Sep 24, 2016 5:10 PM
If I want to avoid recursion is there a syntax for obtaining a sum?
I've tried:
function srX(i,n,s) = (
for(i = [0:n]) {
(s)(1/(pow(2,n)(cos( (pow(2,n) * i);
}
);
and
function srX(i,n,s) = (
x = for(i = [0:n]) {
(s)(1/(pow(2,n)(cos( (pow(2,n) * i);
}
return x;
);
is there a valid syntax for computing a sum w/o recursion?
BTW: I didn't mean to be rude on previous questions, thanks very much for
all the tips I've recieved already....
If I want to avoid recursion is there a syntax for obtaining a sum?
I've tried:
function srX(i,n,s) = (
for(i = [0:n]) {
(s)*(1/(pow(2,n)*(cos( (pow(2,n) * i);
}
);
and
function srX(i,n,s) = (
x = for(i = [0:n]) {
(s)*(1/(pow(2,n)*(cos( (pow(2,n) * i);
}
return x;
);
is there a valid syntax for computing a sum w/o recursion?
BTW: I didn't mean to be rude on previous questions, thanks very much for
all the tips I've recieved already....
TP
Torsten Paul
Sat, Sep 24, 2016 5:14 PM
On 09/24/2016 07:10 PM, Dan Shriver wrote:
If I want to avoid recursion is there a syntax for obtaining a sum?
On 09/24/2016 07:10 PM, Dan Shriver wrote:
> If I want to avoid recursion is there a syntax for obtaining a sum?
>
See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks
Just curious, why is avoiding recursion important?
ciao,
Torsten.
NH
nop head
Sat, Sep 24, 2016 5:17 PM
No there aren't any mutable variables in OpenSCAD, so you can't accumulate
a sum in a variable, you need to use recursion. If you make it tail
recursion then it can be optimised into a loop by OpenSCAD.
On 24 September 2016 at 18:10, Dan Shriver tabbydan@gmail.com wrote:
If I want to avoid recursion is there a syntax for obtaining a sum?
I've tried:
function srX(i,n,s) = (
for(i = [0:n]) {
(s)(1/(pow(2,n)(cos( (pow(2,n) * i);
}
);
and
function srX(i,n,s) = (
x = for(i = [0:n]) {
(s)(1/(pow(2,n)(cos( (pow(2,n) * i);
}
return x;
);
is there a valid syntax for computing a sum w/o recursion?
BTW: I didn't mean to be rude on previous questions, thanks very much for
all the tips I've recieved already....
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
No there aren't any mutable variables in OpenSCAD, so you can't accumulate
a sum in a variable, you need to use recursion. If you make it tail
recursion then it can be optimised into a loop by OpenSCAD.
On 24 September 2016 at 18:10, Dan Shriver <tabbydan@gmail.com> wrote:
> If I want to avoid recursion is there a syntax for obtaining a sum?
>
> I've tried:
>
> function srX(i,n,s) = (
> for(i = [0:n]) {
> (s)*(1/(pow(2,n)*(cos( (pow(2,n) * i);
> }
> );
>
> and
>
> function srX(i,n,s) = (
> x = for(i = [0:n]) {
> (s)*(1/(pow(2,n)*(cos( (pow(2,n) * i);
> }
> return x;
> );
>
> is there a valid syntax for computing a sum w/o recursion?
>
> BTW: I didn't mean to be rude on previous questions, thanks very much for
> all the tips I've recieved already....
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
DS
Dan Shriver
Sat, Sep 24, 2016 5:22 PM
Ok, I don't know how OpenSCAD works "deep under the hood", and people often
talk about it being like a functional programming language, and in such
languages recursion isn't nearly as bad.... but
if you're using a "standard" programming language (proceedural or OOP)
recursion is evil as it involves expensive stack copying. Also recursion
can be hard to read / debug.
In general I try to avoid recursion. Though I do know that in true
functional languages the cost is lower (and also typically it is hard to
get around it)
On Sat, Sep 24, 2016 at 1:14 PM, Torsten Paul Torsten.Paul@gmx.de wrote:
On 09/24/2016 07:10 PM, Dan Shriver wrote:
If I want to avoid recursion is there a syntax for obtaining a sum?
Ok, I don't know how OpenSCAD works "deep under the hood", and people often
talk about it being like a functional programming language, and in such
languages recursion isn't nearly as bad.... but
if you're using a "standard" programming language (proceedural or OOP)
recursion is evil as it involves expensive stack copying. Also recursion
can be hard to read / debug.
In general I try to avoid recursion. Though I do know that in true
functional languages the cost is lower (and also typically it is hard to
get around it)
On Sat, Sep 24, 2016 at 1:14 PM, Torsten Paul <Torsten.Paul@gmx.de> wrote:
> On 09/24/2016 07:10 PM, Dan Shriver wrote:
> > If I want to avoid recursion is there a syntax for obtaining a sum?
> >
> See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks
>
> Just curious, why is avoiding recursion important?
>
> ciao,
> Torsten.
>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss@lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
NH
nop head
Sat, Sep 24, 2016 5:49 PM
For some of us recursions is easier to read as it's more like maths.
Tail recursion, when optimised, avoids stack copying.
On 24 September 2016 at 18:22, Dan Shriver tabbydan@gmail.com wrote:
Ok, I don't know how OpenSCAD works "deep under the hood", and people
often talk about it being like a functional programming language, and in
such languages recursion isn't nearly as bad.... but
if you're using a "standard" programming language (proceedural or OOP)
recursion is evil as it involves expensive stack copying. Also recursion
can be hard to read / debug.
In general I try to avoid recursion. Though I do know that in true
functional languages the cost is lower (and also typically it is hard to
get around it)
On Sat, Sep 24, 2016 at 1:14 PM, Torsten Paul Torsten.Paul@gmx.de wrote:
On 09/24/2016 07:10 PM, Dan Shriver wrote:
If I want to avoid recursion is there a syntax for obtaining a sum?
For some of us recursions is easier to read as it's more like maths.
Tail recursion, when optimised, avoids stack copying.
On 24 September 2016 at 18:22, Dan Shriver <tabbydan@gmail.com> wrote:
> Ok, I don't know how OpenSCAD works "deep under the hood", and people
> often talk about it being like a functional programming language, and in
> such languages recursion isn't nearly as bad.... but
>
> if you're using a "standard" programming language (proceedural or OOP)
> recursion is evil as it involves expensive stack copying. Also recursion
> can be hard to read / debug.
>
> In general I try to avoid recursion. Though I do know that in true
> functional languages the cost is lower (and also typically it is hard to
> get around it)
>
> On Sat, Sep 24, 2016 at 1:14 PM, Torsten Paul <Torsten.Paul@gmx.de> wrote:
>
>> On 09/24/2016 07:10 PM, Dan Shriver wrote:
>> > If I want to avoid recursion is there a syntax for obtaining a sum?
>> >
>> See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks
>>
>> Just curious, why is avoiding recursion important?
>>
>> ciao,
>> Torsten.
>>
>>
>> _______________________________________________
>> 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
>
>
TP
Torsten Paul
Sat, Sep 24, 2016 8:12 PM
On 09/24/2016 07:22 PM, Dan Shriver wrote:
In general I try to avoid recursion. Though I do know that
in true functional languages the cost is lower (and also
typically it is hard to get around it)
Right. OpenSCAD only has a very simple optimization for tail
recursion. This can help getting around the problem that the
stack consumption per recursion level is quite high limiting
the recursion depth.
In general the geometry calculation tends to need much more
time than the expression evaluation.
ciao,
Torsten.
On 09/24/2016 07:22 PM, Dan Shriver wrote:
> In general I try to avoid recursion. Though I do know that
> in true functional languages the cost is lower (and also
> typically it is hard to get around it)
>
Right. OpenSCAD only has a very simple optimization for tail
recursion. This can help getting around the problem that the
stack consumption per recursion level is quite high limiting
the recursion depth.
In general the geometry calculation tends to need much more
time than the expression evaluation.
ciao,
Torsten.
R
Ronaldo
Sat, Sep 24, 2016 9:09 PM
As a beginner, you should always keep opened the page of the cheat sheet
http://www.openscad.org/cheatsheet/ and from there consult the online
manual. I do that.
Both your function definitions are syntactically incorrect. See cheat sheet
-> function -> Manual.
All function definitions, like in math, return a value (float, char, list,
undef,...). The definition is just one expression. The result of the
expression evaluation is the value returned. It is not a procedure returning
a value, there is no return statement.
List comprehension constructs
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions
might be necessary to write non trivial function. It is an invaluable
resource in OpenSCAD language. Recursion is not rocket science (or, as we
say it Portuguese, a seven head monster). Tame it.
--
View this message in context: http://forum.openscad.org/another-beginner-question-syntax-for-a-sum-tp18467p18475.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
As a beginner, you should always keep opened the page of the cheat sheet
<http://www.openscad.org/cheatsheet/> and from there consult the online
manual. I do that.
Both your function definitions are syntactically incorrect. See cheat sheet
-> function -> Manual.
All function definitions, like in math, return a value (float, char, list,
undef,...). The definition is *just one* expression. The result of the
expression evaluation is the value returned. It is not a procedure returning
a value, there is no return statement.
List comprehension constructs
<https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions>
might be necessary to write non trivial function. It is an invaluable
resource in OpenSCAD language. Recursion is not rocket science (or, as we
say it Portuguese, a seven head monster). Tame it.
--
View this message in context: http://forum.openscad.org/another-beginner-question-syntax-for-a-sum-tp18467p18475.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
DS
Dan Shriver
Sun, Sep 25, 2016 4:34 AM
Recursion is not rocket science but it is expensive.
Many of the small programs I write in OpenSCAD are already taking very long
times to run and/or crashing my computer. If I can avoid the extra
overhead of copying the stack I do.
The cheat sheat and at least one person on this thread showed an example of
recursion that unrolls inside OpenSCAD into a loop (tail recursion). So
when I need to do recursion I'll try to make everything in that format to
avoid the overhead.
On Sat, Sep 24, 2016 at 5:09 PM, Ronaldo rcmpersiano@gmail.com wrote:
As a beginner, you should always keep opened the page of the cheat sheet
http://www.openscad.org/cheatsheet/ and from there consult the online
manual. I do that.
Both your function definitions are syntactically incorrect. See cheat sheet
-> function -> Manual.
All function definitions, like in math, return a value (float, char, list,
undef,...). The definition is just one expression. The result of the
expression evaluation is the value returned. It is not a procedure
returning
a value, there is no return statement.
List comprehension constructs
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions
might be necessary to write non trivial function. It is an invaluable
resource in OpenSCAD language. Recursion is not rocket science (or, as we
say it Portuguese, a seven head monster). Tame it.
--
View this message in context: http://forum.openscad.org/
another-beginner-question-syntax-for-a-sum-tp18467p18475.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
Recursion is not rocket science but it is expensive.
Many of the small programs I write in OpenSCAD are already taking very long
times to run and/or crashing my computer. If I can avoid the extra
overhead of copying the stack I do.
The cheat sheat and at least one person on this thread showed an example of
recursion that unrolls inside OpenSCAD into a loop (tail recursion). So
when I need to do recursion I'll try to make everything in that format to
avoid the overhead.
On Sat, Sep 24, 2016 at 5:09 PM, Ronaldo <rcmpersiano@gmail.com> wrote:
> As a beginner, you should always keep opened the page of the cheat sheet
> <http://www.openscad.org/cheatsheet/> and from there consult the online
> manual. I do that.
>
> Both your function definitions are syntactically incorrect. See cheat sheet
> -> function -> Manual.
> All function definitions, like in math, return a value (float, char, list,
> undef,...). The definition is *just one* expression. The result of the
> expression evaluation is the value returned. It is not a procedure
> returning
> a value, there is no return statement.
>
> List comprehension constructs
> <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions>
> might be necessary to write non trivial function. It is an invaluable
> resource in OpenSCAD language. Recursion is not rocket science (or, as we
> say it Portuguese, a seven head monster). Tame it.
>
>
>
> --
> View this message in context: http://forum.openscad.org/
> another-beginner-question-syntax-for-a-sum-tp18467p18475.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
>
TP
Torsten Paul
Sun, Sep 25, 2016 5:08 PM
On 09/25/2016 06:34 AM, Dan Shriver wrote:
Many of the small programs I write in OpenSCAD are already
taking very long times to run and/or crashing my computer.
If I cannavoid the extra overhead of copying the stack I do.
It's hard to tell without knowing the code, but very long
time or crashes do sound much more like geometry calculation
issues not expression evaluation.
The expression evaluation normally should error out with a
stack overflow error but this is not really handled very well,
so there might be cases where it crashes (Please report
those, we can't fix issues we don't know about).
The cheat sheat and at least one person on this thread showed
an example of recursion that unrolls inside OpenSCAD into a
loop (tail recursion). So when I need to do recursion I'll
try to make everything in that format to avoid the overhead.
I'm still trying to dodge around officially documenting the
existing tail recursion as it's both just a simple ad-hoc
implementation and quite limited.
But maybe we just need to accept it's useful enough to cover
possible regressions of this feature.
ciao,
Torsten.
On 09/25/2016 06:34 AM, Dan Shriver wrote:
> Many of the small programs I write in OpenSCAD are already
> taking very long times to run and/or crashing my computer.
> If I cannavoid the extra overhead of copying the stack I do.
>
It's hard to tell without knowing the code, but very long
time or crashes do sound much more like geometry calculation
issues not expression evaluation.
The expression evaluation normally should error out with a
stack overflow error but this is not really handled very well,
so there might be cases where it crashes (Please report
those, we can't fix issues we don't know about).
> The cheat sheat and at least one person on this thread showed
> an example of recursion that unrolls inside OpenSCAD into a
> loop (tail recursion). So when I need to do recursion I'll
> try to make everything in that format to avoid the overhead.
>
I'm still trying to dodge around officially documenting the
existing tail recursion as it's both just a simple ad-hoc
implementation and quite limited.
But maybe we just need to accept it's useful enough to cover
possible regressions of this feature.
ciao,
Torsten.
R
Ronaldo
Sun, Sep 25, 2016 9:18 PM
Unhappily there is no tips about tail recursion elimination on the manual.
May be some might be included in the section Tips & Tricks. The following
Torsten's comment in another thread may help you to design recursive
functions with tail recursion elimination:
OpenSCAD can do simple tail recursion elimination, so if the
function is matching the simple "cond ? result : recurse()"
it will not be limited by stack (there's still a loop limit
but that's much higher... a million, I think?).
// no tail recursion elimination possible, on my system
// that runs out of stack at list length of about 11000.
function sum(l, idx = 0) = idx >= len(l)
? 0
: l[idx] + sum(l, idx + 1);
echo(sum([each [1 : 10000]]));
// with tail recursion elimination, half a million loops
// take a second or two, but will not crash due to stack
// limit.
function sum_tr(l, idx = 0, ret = 0) = idx >= len(l)
? ret
: sum_tr(l, idx + 1, ret + l[idx]);
echo(sum_tr([each [1 : 500000]]));
Unhappily there is no tips about tail recursion elimination on the manual.
May be some might be included in the section Tips & Tricks. The following
Torsten's comment in another thread may help you to design recursive
functions with tail recursion elimination:
> OpenSCAD can do simple tail recursion elimination, so if the
> function is matching the simple "cond ? result : recurse()"
> it will not be limited by stack (there's still a loop limit
> but that's much higher... a million, I think?).
>
>
> // no tail recursion elimination possible, on my system
> // that runs out of stack at list length of about 11000.
> function sum(l, idx = 0) = idx >= len(l)
> ? 0
> : l[idx] + sum(l, idx + 1);
>
> echo(sum([each [1 : 10000]]));
>
>
> // with tail recursion elimination, half a million loops
> // take a second or two, but will not crash due to stack
> // limit.
> function sum_tr(l, idx = 0, ret = 0) = idx >= len(l)
> ? ret
> : sum_tr(l, idx + 1, ret + l[idx]);
>
> echo(sum_tr([each [1 : 500000]]));
--
View this message in context: http://forum.openscad.org/another-beginner-question-syntax-for-a-sum-tp18467p18483.html
Sent from the OpenSCAD mailing list archive at Nabble.com.