I don't quite understand the following code:
我不太明白以下代码:
game :: ([Move], Towers) -> Towers
game ([], towers) = towers
game situation = game (move situation)
situation
here has never mentioned in any part of the codes (there are a long code before this, called the tower of Hanoi, I suppose a lot of people here know it). Why can we directly use situation
here? I know this is correct and the code works very well, but I don't know why.
这里的情况从未在代码的任何部分提及过(在此之前有一个很长的代码,称为河内的塔,我想这里很多人都知道它)。为什么我们可以直接在这里使用情况我知道这是正确的,代码工作得很好,但我不知道为什么。
1 个解决方案
#1
situation
is an argument to the function game
. It would have the type ([Move], Towers)
. Essentially, what you're saying is "if situation
has no moves then return the towers, otherwise perform a move and then pass that result to game
".
情境是功能游戏的一个参数。它会有类型([Move],Towers)。基本上,你所说的是“如果情况没有动作,那么返回塔,否则执行一个动作然后将结果传递给游戏”。
It would be perfectly legal to write this function as
将此函数编写为完全合法
game ([], towers) = towers
game (moves, towers) = game (move (moves, towers))
But this requires taking apart a tuple then constructing a new one exactly like it, or you could use any other name for this value:
但是这需要拆分一个元组然后构造一个完全相同的新元组,或者你可以使用任何其他名称来表示这个值:
game ([], towers) = towers
game foobar = game (move foobar)
It's nothing more than a name for the argument to the function game
, what it's actually called isn't important (so long as it's not a reserved keyword, of course, you couldn't name it import
, for example).
它只不过是函数游戏参数的名称,它实际上被称为不重要的东西(只要它不是保留的关键字,当然,你不能将它命名为import)。
#1
situation
is an argument to the function game
. It would have the type ([Move], Towers)
. Essentially, what you're saying is "if situation
has no moves then return the towers, otherwise perform a move and then pass that result to game
".
情境是功能游戏的一个参数。它会有类型([Move],Towers)。基本上,你所说的是“如果情况没有动作,那么返回塔,否则执行一个动作然后将结果传递给游戏”。
It would be perfectly legal to write this function as
将此函数编写为完全合法
game ([], towers) = towers
game (moves, towers) = game (move (moves, towers))
But this requires taking apart a tuple then constructing a new one exactly like it, or you could use any other name for this value:
但是这需要拆分一个元组然后构造一个完全相同的新元组,或者你可以使用任何其他名称来表示这个值:
game ([], towers) = towers
game foobar = game (move foobar)
It's nothing more than a name for the argument to the function game
, what it's actually called isn't important (so long as it's not a reserved keyword, of course, you couldn't name it import
, for example).
它只不过是函数游戏参数的名称,它实际上被称为不重要的东西(只要它不是保留的关键字,当然,你不能将它命名为import)。