Need examples: Recursion

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

Need examples: Recursion

kintel
Administrator
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116 

Cheers,

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566

signature.asc (210 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

nophead
Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,

 -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/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

mmccool
I wanted to build a maze-builder program.   Can be done by building a spanning tree recursively.  Another use case would be recursive tree (plant) modelling.  However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.   I can't program new primitives because I can't build up the polygon meshes programmatically.  Well, I can do it, but have to resort to using hull to build them up.  Note cons/car/cdr would give the ability to programmatically construct lists, without requiring modification-in-place of variables, but is a lot more powerful when combined with recursion.  For example, the maze-builder could have a modify-in-place table to record nodes already visited, or it could cons on each node visited to the head of a list that is passed up and down the recursion (a purely functional approach... although, inefficient compared to he other).

Btw, do use lexical scope as in scheme if you implement recursion.   May not matter if you don't support assigning functions to variables, though.

Michael McCool

On 2013/04/09, at 午後5:35, nop head <[hidden email]> wrote:

Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,

 -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/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

Triffid Hunter
In reply to this post by kintel
I had a project where I was trying to dynamically build pipes with multiple bends by having each bend as a toroidal section that translated its child to its endpoint.. ran into openscad's refusal to recurse modules and now I can't find the code

will keep digging :)

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

kintel
Administrator
In reply to this post by mmccool
On 2013-04-09, at 06:06 , Michael McCool wrote:

> […] However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.

I agree - I hope to find time to look into this soon.

> Btw, do use lexical scope as in scheme if you implement recursion.

We do use lexical scoping - the reason why module recursion didn't work was that the scope mechanism was broken due to an earlier hack. Should be ok now, but I need to do more testing.

Cheers,

 -Marius

PS. For you Mac users out there: The latest Mac binary snapshot contains support for recursion and cascading child() usage.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

Peter Falke
Here is a small programm that runs through all childs:

module objects(a=10){


red()sphere(a); //****** red() ****

translate([2*a,0,0])cube(a,true);

translate([-2*a,0,0])cylinder(a,a,a/2,true);

}


module red(){

for(i=[0:$children])

color("red")child(i);

}

module green(){

for(i=[0:$children])

color("green")child(i);

}


module cut(c=100){

for(i=[0:$children])

difference(){

child(i);

translate([0,c,0])cube(2*c,true);

}

}


cut() // the difference() doesnt work at all

//red() // this red() troughts a warning and the centre sphere disapeards

//green()

objects();






On 9 April 2013 16:17, Marius Kintel <[hidden email]> wrote:
On 2013-04-09, at 06:06 , Michael McCool wrote:

> […] However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.

I agree - I hope to find time to look into this soon.

> Btw, do use lexical scope as in scheme if you implement recursion.

We do use lexical scoping - the reason why module recursion didn't work was that the scope mechanism was broken due to an earlier hack. Should be ok now, but I need to do more testing.

Cheers,

 -Marius

PS. For you Mac users out there: The latest Mac binary snapshot contains support for recursion and cascading child() usage.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

kintel
Administrator
On 2013-04-09, at 15:23 , Peter Falke wrote:

> Here is a small programm that runs through all childs:
>
Thanks!

Some comments:
1) You make the call "cut() objects();"

     This passes only _one_ child to the cut module. To pass separate children, you should call smth. like this:
    a=10;
        cut {
                red()sphere(a);
                translate([2*a,0,0])cube(a,true);
                translate([-2*a,0,0])cylinder(a,a,a/2,true);
        }

2) for (i=[0:$children]) will iterate out of bounds. It currently causes a weird error which I'll look into.
     Use for (i=[0:$children-1[)

Apart from that, your example works fine with yesterday's snapshot.

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

Whosawhatsis
In reply to this post by mmccool
I would like to be able to return a subset of the items in a vector. Here are some examples:

a = [6, 5, 2, 0, 8, 9];

b = a[1, 4];

echo(b); //should echo "[5, 8]"

a = [6, 5, 2, 0, 8, 9];

b = a[1:3];

echo(b); //should echo "[5, 2, 0]"


This borrows and generalizes the syntax already used to create vectors for a for loop, and keeps everything declarative while making more complex ways of organizing data possible/easier. You could also allow the last item to be blank for "to the end", e.g.:


a = [6, 5, 2, 0, 8, 9];

b = a[1:];

echo(b); //should echo "[5, 2, 0, 8, 9]"


This plus a concatenation syntax should fulfill Michael McCool's needs. Perhaps: 


a = [4, 7];

b = [2:5];

c = a, b; //not [a, b], which would make a vector of vectors! Enclosing in parens would distinguish this from other uses of the comma

echo(c); //should echo [4, 7, 2, 3, 4, 5]

Btw, it would also be nice to be able to declare a vector in the form of [5:0.2:6] (same as [5, 5.2, 5.4, 5.6, 5.8, 6]) and then select the nth member, and there should also be a way to make the last term exclusive rather than inclusive.


We also need a way to feed a vector of arbitrary length to a function and have it return a vector of the same length with the function applied to each member. Here's an example (though I'm not married to the syntax):


function a(b) = sin(b[]) * 5;

c = [30, 90, 180];

echo(a(c)); // should echo "[2.5, 5, 0]"

On Tuesday, April 9, 2013 at 3:06 AM, Michael McCool wrote:

I wanted to build a maze-builder program.   Can be done by building a spanning tree recursively.  Another use case would be recursive tree (plant) modelling.  However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.   I can't program new primitives because I can't build up the polygon meshes programmatically.  Well, I can do it, but have to resort to using hull to build them up.  Note cons/car/cdr would give the ability to programmatically construct lists, without requiring modification-in-place of variables, but is a lot more powerful when combined with recursion.  For example, the maze-builder could have a modify-in-place table to record nodes already visited, or it could cons on each node visited to the head of a list that is passed up and down the recursion (a purely functional approach... although, inefficient compared to he other).

Btw, do use lexical scope as in scheme if you implement recursion.   May not matter if you don't support assigning functions to variables, though.

Michael McCool

On 2013/04/09, at 午後5:35, nop head <[hidden email]> wrote:

Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,

 -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/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

nophead
With list operators the last one could be something like:

function a(b) = len(b)  > 1 ? ( sin(b[0]) * 5, a(b[1:)) : sin(b[0]) * 5

I.e. use recursion to iterate.

I think (been drinking)


On 9 April 2013 21:39, whosawhatsis <[hidden email]> wrote:
I would like to be able to return a subset of the items in a vector. Here are some examples:

a = [6, 5, 2, 0, 8, 9];

b = a[1, 4];

echo(b); //should echo "[5, 8]"

a = [6, 5, 2, 0, 8, 9];

b = a[1:3];

echo(b); //should echo "[5, 2, 0]"


This borrows and generalizes the syntax already used to create vectors for a for loop, and keeps everything declarative while making more complex ways of organizing data possible/easier. You could also allow the last item to be blank for "to the end", e.g.:


a = [6, 5, 2, 0, 8, 9];

b = a[1:];

echo(b); //should echo "[5, 2, 0, 8, 9]"


This plus a concatenation syntax should fulfill Michael McCool's needs. Perhaps: 


a = [4, 7];

b = [2:5];

c = a, b; //not [a, b], which would make a vector of vectors! Enclosing in parens would distinguish this from other uses of the comma

echo(c); //should echo [4, 7, 2, 3, 4, 5]

Btw, it would also be nice to be able to declare a vector in the form of [5:0.2:6] (same as [5, 5.2, 5.4, 5.6, 5.8, 6]) and then select the nth member, and there should also be a way to make the last term exclusive rather than inclusive.


We also need a way to feed a vector of arbitrary length to a function and have it return a vector of the same length with the function applied to each member. Here's an example (though I'm not married to the syntax):


function a(b) = sin(b[]) * 5;

c = [30, 90, 180];

echo(a(c)); // should echo "[2.5, 5, 0]"

On Tuesday, April 9, 2013 at 3:06 AM, Michael McCool wrote:

I wanted to build a maze-builder program.   Can be done by building a spanning tree recursively.  Another use case would be recursive tree (plant) modelling.  However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.   I can't program new primitives because I can't build up the polygon meshes programmatically.  Well, I can do it, but have to resort to using hull to build them up.  Note cons/car/cdr would give the ability to programmatically construct lists, without requiring modification-in-place of variables, but is a lot more powerful when combined with recursion.  For example, the maze-builder could have a modify-in-place table to record nodes already visited, or it could cons on each node visited to the head of a list that is passed up and down the recursion (a purely functional approach... although, inefficient compared to he other).

Btw, do use lexical scope as in scheme if you implement recursion.   May not matter if you don't support assigning functions to variables, though.

Michael McCool

On 2013/04/09, at 午後5:35, nop head <[hidden email]> wrote:

Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,


 -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/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
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

mmccool
In reply to this post by Whosawhatsis
Actually, list comprehensions for constructing lists would be great, since they are very concise, and are also declarative.   Something like

[for i = [a:b] f(i)]

to generate a set of elements using i from a  to b.


On Wed, Apr 10, 2013 at 5:39 AM, whosawhatsis <[hidden email]> wrote:
I would like to be able to return a subset of the items in a vector. Here are some examples:

a = [6, 5, 2, 0, 8, 9];

b = a[1, 4];

echo(b); //should echo "[5, 8]"

a = [6, 5, 2, 0, 8, 9];

b = a[1:3];

echo(b); //should echo "[5, 2, 0]"


This borrows and generalizes the syntax already used to create vectors for a for loop, and keeps everything declarative while making more complex ways of organizing data possible/easier. You could also allow the last item to be blank for "to the end", e.g.:


a = [6, 5, 2, 0, 8, 9];

b = a[1:];

echo(b); //should echo "[5, 2, 0, 8, 9]"


This plus a concatenation syntax should fulfill Michael McCool's needs. Perhaps: 


a = [4, 7];

b = [2:5];

c = a, b; //not [a, b], which would make a vector of vectors! Enclosing in parens would distinguish this from other uses of the comma

echo(c); //should echo [4, 7, 2, 3, 4, 5]

Btw, it would also be nice to be able to declare a vector in the form of [5:0.2:6] (same as [5, 5.2, 5.4, 5.6, 5.8, 6]) and then select the nth member, and there should also be a way to make the last term exclusive rather than inclusive.


We also need a way to feed a vector of arbitrary length to a function and have it return a vector of the same length with the function applied to each member. Here's an example (though I'm not married to the syntax):


function a(b) = sin(b[]) * 5;

c = [30, 90, 180];

echo(a(c)); // should echo "[2.5, 5, 0]"

On Tuesday, April 9, 2013 at 3:06 AM, Michael McCool wrote:

I wanted to build a maze-builder program.   Can be done by building a spanning tree recursively.  Another use case would be recursive tree (plant) modelling.  However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.   I can't program new primitives because I can't build up the polygon meshes programmatically.  Well, I can do it, but have to resort to using hull to build them up.  Note cons/car/cdr would give the ability to programmatically construct lists, without requiring modification-in-place of variables, but is a lot more powerful when combined with recursion.  For example, the maze-builder could have a modify-in-place table to record nodes already visited, or it could cons on each node visited to the head of a list that is passed up and down the recursion (a purely functional approach... although, inefficient compared to he other).

Btw, do use lexical scope as in scheme if you implement recursion.   May not matter if you don't support assigning functions to variables, though.

Michael McCool

On 2013/04/09, at 午後5:35, nop head <[hidden email]> wrote:

Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,

 -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/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
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

Whosawhatsis
In reply to this post by nophead
That would work, though I would prefer to iterate to zero and return an empty vector on the last iteration, so that the "sin(b[0]) * 5" part doesn't have to appear twice (and have to be updated in two places if the formula changes).

On Tuesday, April 9, 2013 at 1:49 PM, nop head wrote:

With list operators the last one could be something like:

function a(b) = len(b)  > 1 ? ( sin(b[0]) * 5, a(b[1:)) : sin(b[0]) * 5

I.e. use recursion to iterate.

I think (been drinking)


On 9 April 2013 21:39, whosawhatsis <[hidden email]> wrote:
I would like to be able to return a subset of the items in a vector. Here are some examples:

a = [6, 5, 2, 0, 8, 9];

b = a[1, 4];

echo(b); //should echo "[5, 8]"

a = [6, 5, 2, 0, 8, 9];

b = a[1:3];

echo(b); //should echo "[5, 2, 0]"


This borrows and generalizes the syntax already used to create vectors for a for loop, and keeps everything declarative while making more complex ways of organizing data possible/easier. You could also allow the last item to be blank for "to the end", e.g.:


a = [6, 5, 2, 0, 8, 9];

b = a[1:];

echo(b); //should echo "[5, 2, 0, 8, 9]"


This plus a concatenation syntax should fulfill Michael McCool's needs. Perhaps: 


a = [4, 7];

b = [2:5];

c = a, b; //not [a, b], which would make a vector of vectors! Enclosing in parens would distinguish this from other uses of the comma

echo(c); //should echo [4, 7, 2, 3, 4, 5]

Btw, it would also be nice to be able to declare a vector in the form of [5:0.2:6] (same as [5, 5.2, 5.4, 5.6, 5.8, 6]) and then select the nth member, and there should also be a way to make the last term exclusive rather than inclusive.


We also need a way to feed a vector of arbitrary length to a function and have it return a vector of the same length with the function applied to each member. Here's an example (though I'm not married to the syntax):


function a(b) = sin(b[]) * 5;

c = [30, 90, 180];

echo(a(c)); // should echo "[2.5, 5, 0]"

On Tuesday, April 9, 2013 at 3:06 AM, Michael McCool wrote:

I wanted to build a maze-builder program.   Can be done by building a spanning tree recursively.  Another use case would be recursive tree (plant) modelling.  However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.   I can't program new primitives because I can't build up the polygon meshes programmatically.  Well, I can do it, but have to resort to using hull to build them up.  Note cons/car/cdr would give the ability to programmatically construct lists, without requiring modification-in-place of variables, but is a lot more powerful when combined with recursion.  For example, the maze-builder could have a modify-in-place table to record nodes already visited, or it could cons on each node visited to the head of a list that is passed up and down the recursion (a purely functional approach... although, inefficient compared to he other).

Btw, do use lexical scope as in scheme if you implement recursion.   May not matter if you don't support assigning functions to variables, though.

Michael McCool

On 2013/04/09, at 午後5:35, nop head <[hidden email]> wrote:

Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,


 -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/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
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

nophead
Yes that is better if empty lists are allowed.


On 9 April 2013 23:07, whosawhatsis <[hidden email]> wrote:
That would work, though I would prefer to iterate to zero and return an empty vector on the last iteration, so that the "sin(b[0]) * 5" part doesn't have to appear twice (and have to be updated in two places if the formula changes).

On Tuesday, April 9, 2013 at 1:49 PM, nop head wrote:

With list operators the last one could be something like:

function a(b) = len(b)  > 1 ? ( sin(b[0]) * 5, a(b[1:)) : sin(b[0]) * 5

I.e. use recursion to iterate.

I think (been drinking)


On 9 April 2013 21:39, whosawhatsis <[hidden email]> wrote:
I would like to be able to return a subset of the items in a vector. Here are some examples:

a = [6, 5, 2, 0, 8, 9];

b = a[1, 4];

echo(b); //should echo "[5, 8]"

a = [6, 5, 2, 0, 8, 9];

b = a[1:3];

echo(b); //should echo "[5, 2, 0]"


This borrows and generalizes the syntax already used to create vectors for a for loop, and keeps everything declarative while making more complex ways of organizing data possible/easier. You could also allow the last item to be blank for "to the end", e.g.:


a = [6, 5, 2, 0, 8, 9];

b = a[1:];

echo(b); //should echo "[5, 2, 0, 8, 9]"


This plus a concatenation syntax should fulfill Michael McCool's needs. Perhaps: 


a = [4, 7];

b = [2:5];

c = a, b; //not [a, b], which would make a vector of vectors! Enclosing in parens would distinguish this from other uses of the comma

echo(c); //should echo [4, 7, 2, 3, 4, 5]

Btw, it would also be nice to be able to declare a vector in the form of [5:0.2:6] (same as [5, 5.2, 5.4, 5.6, 5.8, 6]) and then select the nth member, and there should also be a way to make the last term exclusive rather than inclusive.


We also need a way to feed a vector of arbitrary length to a function and have it return a vector of the same length with the function applied to each member. Here's an example (though I'm not married to the syntax):


function a(b) = sin(b[]) * 5;

c = [30, 90, 180];

echo(a(c)); // should echo "[2.5, 5, 0]"

On Tuesday, April 9, 2013 at 3:06 AM, Michael McCool wrote:

I wanted to build a maze-builder program.   Can be done by building a spanning tree recursively.  Another use case would be recursive tree (plant) modelling.  However, a lot of the use cases I can think of also require the ability to build and deconstruct lists, ie cons/car/cdr.  That is the main thing I find frustrating right now with SCAD.   I can't program new primitives because I can't build up the polygon meshes programmatically.  Well, I can do it, but have to resort to using hull to build them up.  Note cons/car/cdr would give the ability to programmatically construct lists, without requiring modification-in-place of variables, but is a lot more powerful when combined with recursion.  For example, the maze-builder could have a modify-in-place table to record nodes already visited, or it could cons on each node visited to the head of a list that is passed up and down the recursion (a purely functional approach... although, inefficient compared to he other).

Btw, do use lexical scope as in scheme if you implement recursion.   May not matter if you don't support assigning functions to variables, though.

Michael McCool

On 2013/04/09, at 午後5:35, nop head <[hidden email]> wrote:

Function recursion has always worked for me.


On 9 April 2013 04:57, Marius Kintel <[hidden email]> wrote:
Hi all,

I'm currently looking into supporting limited recursion of functions and modules, including advanced cascading child behavior.
It requires a pretty deep refactoring, so I'm looking for test cases which exercise this.

See the issue tracker for more info: https://github.com/openscad/openscad/issues/116

Cheers,


 -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/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/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

Peter Falke
In reply to this post by kintel
Hi,

thanks for pointing out my mistake with $children-1. 
An error should be thrown in this case. Something like ERROR: child not defined

On a more general note:
Presently the call child() (without any argument) calls really child(0), and only child(0).
I think it would be easier and less error prone when child() would call on all children that follow (from 0 to $children-1).


Sincerely,

TakeItAndRun

P.S.: I`d like to express my sincere thanks and gratitude for the continuing work and development of OpenSCAD.




On 9 April 2013 21:48, Marius Kintel <[hidden email]> wrote:
On 2013-04-09, at 15:23 , Peter Falke wrote:

> Here is a small programm that runs through all childs:
>
Thanks!

Some comments:
1) You make the call "cut() objects();"

     This passes only _one_ child to the cut module. To pass separate children, you should call smth. like this:
        a=10;
        cut {
                red()sphere(a);
                translate([2*a,0,0])cube(a,true);
                translate([-2*a,0,0])cylinder(a,a,a/2,true);
        }

2) for (i=[0:$children]) will iterate out of bounds. It currently causes a weird error which I'll look into.
     Use for (i=[0:$children-1[)

Apart from that, your example works fine with yesterday's snapshot.

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

kintel
Administrator
On 2013-04-10, at 10:22 , Peter Falke wrote:

> thanks for pointing out my mistake with $children-1.  
> An error should be thrown in this case. Something like ERROR: child not defined
>
Yes, I just fixed that.

> On a more general note:
> Presently the call child() (without any argument) calls really child(0), and only child(0).
> I think it would be easier and less error prone when child() would call on all children that follow (from 0 to $children-1).
>
I kind of agree, but changing it would break backwards compatibility.
I find keeping backwards compatibility is crucial for OpenSCAD to maintain momentum.

We could of course discuss ways of doing a V2 of the language, and write a compatibility layer/converter for old files. I think this will be inevitable eventually, so having a strategy would be good.

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

nophead
It would be quite obscure though to have any existing code using child() when there is more than one child.


On 10 April 2013 16:28, Marius Kintel <[hidden email]> wrote:
On 2013-04-10, at 10:22 , Peter Falke wrote:

> thanks for pointing out my mistake with $children-1.
> An error should be thrown in this case. Something like ERROR: child not defined
>
Yes, I just fixed that.

> On a more general note:
> Presently the call child() (without any argument) calls really child(0), and only child(0).
> I think it would be easier and less error prone when child() would call on all children that follow (from 0 to $children-1).
>
I kind of agree, but changing it would break backwards compatibility.
I find keeping backwards compatibility is crucial for OpenSCAD to maintain momentum.

We could of course discuss ways of doing a V2 of the language, and write a compatibility layer/converter for old files. I think this will be inevitable eventually, so having a strategy would be good.

 -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/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

Peter Falke
I believe nobody is actually using the child() up to know anyway, so no code gets broken. :)

And most code will not break (see example).

redall()sphere();

translate([2,0,0])cube();


module red(){

color("red")child();

}


module redall(){

for(i=[0:$children-1])

color("red")child(i);

}



Only code like this would break:


module col(){

color ("red") child();

color ("green") child(1);

}


Actually this would work as expected, this will break:

module col(){

color ("green") child(1);

color ("red") child();

}




Sincerely,

TakeItAndRun


On 10 April 2013 18:12, nop head <[hidden email]> wrote:
It would be quite obscure though to have any existing code using child() when there is more than one child.


On 10 April 2013 16:28, Marius Kintel <[hidden email]> wrote:
On 2013-04-10, at 10:22 , Peter Falke wrote:

> thanks for pointing out my mistake with $children-1.
> An error should be thrown in this case. Something like ERROR: child not defined
>
Yes, I just fixed that.

> On a more general note:
> Presently the call child() (without any argument) calls really child(0), and only child(0).
> I think it would be easier and less error prone when child() would call on all children that follow (from 0 to $children-1).
>
I kind of agree, but changing it would break backwards compatibility.
I find keeping backwards compatibility is crucial for OpenSCAD to maintain momentum.

We could of course discuss ways of doing a V2 of the language, and write a compatibility layer/converter for old files. I think this will be inevitable eventually, so having a strategy would be good.

 -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/openscad
http://openscad.org - https://flattr.com/thing/121566



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

kintel
Administrator
On 2013-04-10, at 15:34 , Peter Falke wrote:

> I believe nobody is actually using the child() up to know anyway, so no code gets broken. :)
>
OK, I think you've convinced me :) I'll look at it.

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

nophead
It could be done by adding children() to be more explicit and backwards compatible.


On 10 April 2013 20:40, Marius Kintel <[hidden email]> wrote:
On 2013-04-10, at 15:34 , Peter Falke wrote:

> I believe nobody is actually using the child() up to know anyway, so no code gets broken. :)
>
OK, I think you've convinced me :) I'll look at it.

 -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/openscad
http://openscad.org - https://flattr.com/thing/121566
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

kintel
Administrator
In reply to this post by Whosawhatsis
Hi all,

This thread spawned a subthread on vector/list operations.
See here for the corresponding github issue:
https://github.com/openscad/openscad/issues/201

..and here for an initial wiki writeup of the ideas presented here:
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
Reply | Threaded
Open this post in threaded view
|

Re: Need examples: Recursion

kintel
Administrator
In reply to this post by Triffid Hunter

On 2013-04-09, at 08:26 , Triffid Hunter wrote:

> I had a project where I was trying to dynamically build pipes with multiple bends by having each bend as a toroidal section that translated its child to its endpoint.. ran into openscad's refusal to recurse modules and now I can't find the code
>
> will keep digging :)

Is this what you mean?
http://rocklinux.net/pipermail/openscad/2013-January/003715.html

It appears to work now :)

 -Marius

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad
http://openscad.org - https://flattr.com/thing/121566