2
我正在做一些功課,雖然我對SML有一些經驗,但Haskell有一些奇怪的地方。考慮這個簡單的功能: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
此代碼有效。但是,如果我換出(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
這似乎很奇怪,我,作爲第二個參數已經在定義中被定義爲Pos,那麼如何來調用這個扼流器,並且只能調用?我使用ghci btw。
您將「移動」拼寫爲「移動」。 – jrockway 2009-09-01 22:55:47