我正在做一些功课,虽然我有一些SML的经验,但Haskell有些奇怪之处.考虑这个简单的功能:
type Pos = (Int, Int)
data Move = North | South | East | West
move :: Move -> Pos -> Pos
move North (x,y) = (x, y+1)
move South (x,y) = (x, y-1)
move East (x,y) = (x+1, y)
move West (x,y) = (x-1, y)
moves :: [Move] -> Pos -> Pos
moves (m:ms) (x,y) = moves ms (move m (x,y))
moves [] p = p
Run Code Online (Sandbox Code Playgroud)
这段代码有效.但是,如果我交换(x,y)元组(我不使用p它),简单的调用失败(声明当然可以正常工作):
moves :: [Move] -> Pos -> Pos
moves (m:ms) p = moves ms (move m p)
moves [] p = p
*Main> let p = (1,1) :: Pos
*Main> move [North, North] p
<interactive>:1:5:
Couldn't match expected type `Move' against inferred type `[a]'
In the first argument of `move', namely `[North, North]'
In the expression: move [North, North] p
In the definition of `it': it = move [North, North] p
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪,因为第二个参数已经在定义中被输入为Pos,那么为什么会出现这种窒息,并且仅在调用时呢?我正在使用ghci btw.
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |