sub hanoi(Int:D $n, Str:D $start, Str:D $end, Str:D $extra,
&move_disk:(Int:D, Str:D, Str:D --> Nil)
--> Nil)
{
if $n == 1 {
move_disk(1, $start, $end); # Step 1
}
else {
hanoi($n - 1, $start, $extra, $end); # Step 2
move_disk($n, $start, $end); # Step 3
hanoi($n - 1, $extra, $end, $start); # Step 4
}
}
sub print_instruction(Int:D $n, Str:D $start, Str:D $end --> Nil)
{
say "Move disk #$n from $start to $end";
}
Calling hanoi(3, 'A', 'B', 'C', print_instruction)
gives:
Too few positionals passed; expected 3 arguments but got 0
in sub print_instruction at <unknown file> line 15
in block <unit> at <unknown file>
print_instruction
inhanoi(3, 'A', 'B', 'C', print_instruction)
callsprint_instruction
instead of referring to it like in Python. (Why? Too many good reasons to fit in comments but one is that Raku unifies field references and method calls -- both are.bar
-- as part of 100% encapsulation of object state. That solves the Fragile base class problem and makes Raku Actor model "ready".)