[OpenSCAD] Programming in Functional OpenSCAD

Ronaldo Persiano 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 ][0];

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
> applied.
>
> On 31 January 2018 at 13:22, NateTG <nate-openscadforum at pedantic.org>
> wrote:
>
>> I understand what tail recursion is. I'm just not confident about when or
>> how
>> 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
>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20180131/8a3ce245/attachment-0002.html>


More information about the Discuss mailing list