Too few positionals passed, why?

huangapple go评论60阅读模式
英文:

Too few positionals passed, why?

问题

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);              # 步骤 1
    }
    else {
        hanoi($n - 1, $start, $extra, $end);     # 步骤 2
        move_disk($n, $start, $end);             # 步骤 3
        hanoi($n - 1, $extra, $end, $start);     # 步骤 4
    }
}

sub print_instruction(Int:D $n, Str:D $start, Str:D $end --> Nil)
{
    say "将盘子 #$n 从 $start 移动到 $end";
}

调用 hanoi(3, 'A', 'B', 'C', print_instruction) 得到:

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>

<details>
<summary>英文:</summary>


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, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, 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>


</details>


# 答案1
**得分**: 16

以下是翻译好的部分:

1. `hanoi(3, 'A', 'B', 'C', print_instruction)`
   
   在这个语句中,你将调用 `print_instruction` 的结果作为第四个参数传递。从 `hanoi` 子程序的签名来看,显然你期望它是一个可调用对象。如果你想传递可调用对象,你需要在名称前面加上 `&`,所以应该这样写:

   `hanoi(3, 'A', 'B', 'C', &print_instruction);`

   因为 `print_instruction` 子程序期望接收3个参数,而你没有传递任何参数,所以会得到“期望3个参数,但传递了0个”错误。

2. 第一个对 `hanoi` 的调用有5个参数,但递归调用只传递4个。在那里你会得到类似的错误,但数字不同:期望5个参数,但得到了4个。最简单的解决方法是也将第五个参数传递:

   `hanoi($n - 1, $start, $extra, $end, &move_disk);`

最后:在标识符中使用下划线*非常*像上世纪90年代的风格,你也可以在Raku编程语言的标识符中使用连字符!所以你可以将 `move_disk` 子程序命名为 `move-disk`。

<details>
<summary>英文:</summary>

There are several things wrong with your code:

1. `hanoi(3, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, print_instruction)`

In that statement, you&#39;re passing the **result** of the call to `print_instruction` as the 4th parameter.  You&#39;re evidently expecting it to be something `Callable`judging from the signature of the `hanoi` sub.  If you want to pass the callable, you need to prefix the name with a `&amp;`, so:

    hanoi(3, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &amp;print_instruction);

Because the `print_instruction` sub does expect 3 arguments, and you called it with zero, you get the &quot;expected 3 arguments but got 0&quot; error. 

2. The first call to `hanoi` has 5 arguments, but the recursive calls only pass 4.  You will get a similar error there, but with different numbers: expected 5 arguments but got 4.  The simplest solution is to pass that on as well:

    hanoi($n - 1, $start, $extra, $end, &amp;move_disk);


Finally: using underscores in identifiers is *sooo* 1990&#39;s  :-)   You can also use the hyphen in identifiers in the Raku Programming Language!  So you can call the `move_disk` subroutine `move-disk`  :-)

</details>



huangapple
  • 本文由 发表于 2023年4月10日 22:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定