[OpenSCAD] Programming in Functional OpenSCAD
rcmpersiano at gmail.com
Wed Jan 31 14:56:54 EST 2018
Not always it is easy to write a tail recursion qualified to elimination.
And sometimes it is possible to avoid the recursion at all using a C-like
*for* enabled in a snapshot version. The treeinsertlist(() function, for
instance, may be rewritten iteractively as:
function treeinsertlist(tree,list) =
[for(i=len(list), tr=tree; i>=0; i=i-1, tr = treeinsert(tr,list[i]) )
if(i==0) tr ];
I am not an advocate of this form that is not easily grasped, but a complex
tail recursion elimination form may be also hard to read.
2018-01-31 16:33 GMT-02:00 doug moen <doug at moens.org>:
> "I understand what tail recursion is. I'm just not confident about when
> or how
> it gets applied in OpenSCAD."
> The tail recursion optimization is only applied in two very specific
> circumstances. It is not implemented in a general way.
> The first case:
> function f(x) = shouldExit ? result : f(...);
> Note that there is a tail recursive function call after the :
> The second case:
> function f(x) = keepGoing ? f(...) : result;
> Note that there is a tail recursive function call between ? and :
> The body of the function must match one of the two patterns. If you change
> these patterns in any way (eg, add a 'let' to define local variables), then
> the pattern no longer matches, and tail recursion optimization is not
> On 31 January 2018 at 13:22, NateTG <nate-openscadforum at pedantic.org>
>> I understand what tail recursion is. I'm just not confident about when or
>> it gets applied in OpenSCAD.
>> > From a language user point of view, it can be ignored.
>> It would be nice if that were true, but the nature of OpenSCAD means that
>> things can easily get stack constrained. even when they are working
>> properly. For example, Ronaldo didn't modify the tree insertion function
>> for some aesthetic reason but rather because stack overflows were a
>> practical issue.
>> Sent from: http://forum.openscad.org/
>> OpenSCAD mailing list
>> Discuss at lists.openscad.org
> OpenSCAD mailing list
> Discuss at lists.openscad.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Discuss