On Thu, 12 Sep 2002, Piers Cawley wrote:

> So, do we have a timetable for when the Perl 6 interpreter is going
> handle closures?

When it uses find_lex/store_lex instead of registers for locals, which
will take a bit of doing, though it's near the top of more than one todo
list.

> Also, consider the following:
>
>   sub fac($n) {
>     when 0  { 1 }
>     default { $n * fac($n - 1) }
>   }
>
>   print1 fac(10);
>
> Compiling this barfs because there's 'no topic in fac', despite the
> apocalypse stating that the first argument to a function shall be the
> default topic.

Sorry, that hasn't been implemented yet.  There are probably a number of
places where the current topic should be set, but is not.  I haven't gone
through the apocalypse in detail to pin them all down, unfortunately.
This one should be easy to fix, though -- it needs a call to set_topic()
in P6C::closure::val.

> So, putting in an explicit 'given', we have:
>
>   sub fac($n) {
>     given $n {
>       when 0  { 1 }
>       default { $n * fac($n - 1) }
>     }
>   }
>
>   print1 fac(10);
>
> Which throws a runtime 'Wrong type on top of stack!' error.

Even using "print" instead of "print1"?  This should be working, but I
think there's a bug with "given".  I'll take a look.

> So, am I doing something utterly wrong, or is it really impossible to
> write recursive functions with the current perl6 compiler?

You just have to coddle it a bit:

  languages/perl6% ./perl6 -we 'sub fac($n) { \
        if $n { $n * fac($n - 1) } else { 1 } }; print1 fac 10'
  3628800

/s

Reply via email to