Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi subroutine recursive in Raku

Tags:

raku

I'm learning raku, following the book Thinking in Raku

There is an exercise that I need to define the ackermann function.

I defined a positive Integer subset:

subset Positive-Integer of Int where { $_ > 0}

Then I goes through the recursive version using:

multi ackermann(0, Positive-Integer $n) {
    $n + 1;
}

multi ackermann(Positive-Integer $m, 0) {
    ackermann $m - 1, 1;
}

multi ackermann(Positive-Integer $m, Positive-Integer $n) {
    ackermann $m - 1, ackermann $m, $n - 1;
}

but executing the code I get when executing:

ackermann 3, 4;

> * * &ackermann
> > * * &ackermann
> > * * &ackermann
> > ackermann 3, 4
Cannot resolve caller ackermann(Int:D, Int:D); none of these signatures match:
    (0, Int $n)
    (Int $m, 0)
  in sub ackermann at <unknown file> line 3
  in sub ackermann at <unknown file> line 3
  in sub ackermann at <unknown file> line 3
  in sub ackermann at <unknown file> line 3
  in sub ackermann at <unknown file> line 3
  in sub ackermann at <unknown file> line 3
  in sub ackermann at <unknown file> line 3
  in block <unit> at <unknown file> line 2

> 

I do not get the point of what it is happening here.

like image 349
anquegi Avatar asked Sep 14 '20 10:09

anquegi


1 Answers

Judging from your error output, you appear to be trying to run these examples in the REPL. Looking at the actual error in the error output, it appears to be missing this candidate:

multi ackermann(Positive-Integer $m, Positive-Integer $n) {
    ackermann $m - 1, ackermann $m, $n - 1;
}

If I take the whole code of your example, and put it into a file:

subset Positive-Integer of Int where { $_ > 0}

multi ackermann(0, Positive-Integer $n) {
    $n + 1;
}

multi ackermann(Positive-Integer $m, 0) {
    ackermann $m - 1, 1;
}

multi ackermann(Positive-Integer $m, Positive-Integer $n) {
    ackermann $m - 1, ackermann $m, $n - 1;
}

say ackermann 3, 4;

I get the expected result (125).

So this appears to me that you've made some kind of error when entering the code into a REPL session.

Pro Tip: if you're trying examples with multiple lines of code, it is generally easier to store each example into a separate file. This gives you more oversight of the code, makes it easier to make changes and see their results, and you have something to go back to later if you want to revisit examples you've done in the past.

like image 182
Elizabeth Mattijsen Avatar answered Nov 10 '22 20:11

Elizabeth Mattijsen