[Q] recursion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Q] recursion

David Faught
This is really a Smalltalk/Squeak language question, although it may
not sound like it at first.  There is a limited Squeak programming
environment in the MockTurtle turtle graphics package for Croquet,
essentially like a WorkSpace where the code is dynamically compiled
and executed like a DoIt, and not retained as a method in the turtle
class.

Since many turtle graphics examples use recursion to do interesting
things, I'm wondering if there is a way to do recursion in this kind
of environment, maybe using named blocks?

Thanks for any input you may have.  Simple examples would be much appreciated!

Reply | Threaded
Open this post in threaded view
|

Re: [Q] recursion

karl-8
David Faught skrev:

> This is really a Smalltalk/Squeak language question, although it may
> not sound like it at first.  There is a limited Squeak programming
> environment in the MockTurtle turtle graphics package for Croquet,
> essentially like a WorkSpace where the code is dynamically compiled
> and executed like a DoIt, and not retained as a method in the turtle
> class.
>
> Since many turtle graphics examples use recursion to do interesting
> things, I'm wondering if there is a way to do recursion in this kind
> of environment, maybe using named blocks?
>
> Thanks for any input you may have.  Simple examples would be much
> appreciated!
>
>
I have been doing some stuff like this with the etoys and found that
recursion did limited help
in drawing. This is because the patterns repeated would not change, so
you would just get
drawings like stairs or squares drawn 100 times on top of each other
etc. I implemented simple a L-System to get  interesting results.
And if a L-System is simple enough to be implemented in etoys so it
should not be hard in Smalltalk :-)
Here is the page I used:
http://instruct1.cit.cornell.edu/Courses/bionb441/LSystem/index.html
I see that there is some 3D stuff on that page as well.

Karl

Reply | Threaded
Open this post in threaded view
|

Re: [Q] recursion

David Faught
In reply to this post by David Faught
I was thinking that something like this might work:

| spiral |
self vi:1.
self r: 0.0 g: 0.8 b: 0.0. self lw: 1.
spiral := [ :size :angle |
        (size < 100) ifTrue: [
                self fo: size.
                self tr: angle.
                spiral value: size + 2 value: angle.
        ].
].
spiral value: 0 value: 91.

but it doesn't.  When this code is run, there is an error that says
something about trying to evaluate a block that is already being
evaluated.  So, I guess that recursion in this manner in this
environment is not possible.  Back to plain old loops ;-)

On 11/24/06, David Faught <[hidden email]> wrote:

> This is really a Smalltalk/Squeak language question, although it may
> not sound like it at first.  There is a limited Squeak programming
> environment in the MockTurtle turtle graphics package for Croquet,
> essentially like a WorkSpace where the code is dynamically compiled
> and executed like a DoIt, and not retained as a method in the turtle
> class.
>
> Since many turtle graphics examples use recursion to do interesting
> things, I'm wondering if there is a way to do recursion in this kind
> of environment, maybe using named blocks?
>
> Thanks for any input you may have.  Simple examples would be much appreciated!
>

Reply | Threaded
Open this post in threaded view
|

Re: [Q] recursion

Klaus D. Witzel
Hi David, try this:

  | spiral |
  self vi:1.
  self r: 0.0 g: 0.8 b: 0.0. self lw: 1.
  spiral := [ :size :angle |
  (size < 100) ifTrue: [
  self fo: size.
  self tr: angle.
  spiral copy value: size + 2 value: angle.
  ].
  ].
  spiral copy value: 0 value: 91.

Indeed, if you don't make a copy of a recursing block, it is (as the error  
message says) already being evaluated. But not so the copy. Note that  
every #value:value: needs its own copy.

/Klaus

On Sat, 25 Nov 2006 15:14:28 +0100, David Faught wrote:

> I was thinking that something like this might work:
>
> | spiral |
> self vi:1.
> self r: 0.0 g: 0.8 b: 0.0. self lw: 1.
> spiral := [ :size :angle |
> (size < 100) ifTrue: [
> self fo: size.
> self tr: angle.
> spiral value: size + 2 value: angle.
> ].
> ].
> spiral value: 0 value: 91.
>
> but it doesn't.  When this code is run, there is an error that says
> something about trying to evaluate a block that is already being
> evaluated.  So, I guess that recursion in this manner in this
> environment is not possible.  Back to plain old loops ;-)
>
> On 11/24/06, David Faught <[hidden email]> wrote:
>> This is really a Smalltalk/Squeak language question, although it may
>> not sound like it at first.  There is a limited Squeak programming
>> environment in the MockTurtle turtle graphics package for Croquet,
>> essentially like a WorkSpace where the code is dynamically compiled
>> and executed like a DoIt, and not retained as a method in the turtle
>> class.
>>
>> Since many turtle graphics examples use recursion to do interesting
>> things, I'm wondering if there is a way to do recursion in this kind
>> of environment, maybe using named blocks?
>>
>> Thanks for any input you may have.  Simple examples would be much  
>> appreciated!
>>
>
>



Reply | Threaded
Open this post in threaded view
|

Re: [Q] recursion

Stéphane Rollandin
there is also #clone, which seems faster

on my computer:

fact _ [:n | n isZero ifTrue: [1]
                ifFalse: [n * (fact copy value: (n - 1))]].

[fact copy value: 6] bench
        ==> '157928.2143571286 per second.'

fact _ [:n | n isZero ifTrue: [1]
                ifFalse: [n * (fact clone value: (n - 1))]].

[fact clone value: 6] bench
        ==> '208975.4049190162 per second.'



Stef