Haskell function template problems
I'm doing my homework, and while I have some experience with SML, Haskell has some oddities. Consider this simple function:
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
This code works. However, if I swap a tuple (x,y)
(which I don't use at all) with a simple one p
, it doesn't work when called (the declaration works fine, of course):
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
Which seems strange to me, since the second parameter is already entered as Pos in the definition, since this is what happens and only when called? I am using ghci btw.
+2
source to share