2009-09-01 11 views
2

Ich mache einige Hausaufgaben und während ich etwas Erfahrung mit SML habe, hat Haskell einige Kuriositäten. Betrachten Sie diese einfache Funktion:Haskell Funktion Muster-passende Ausgaben

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 

Dieser Code funktioniert. Allerdings, wenn ich den (x,y) Tupel auslagern (was ich nicht sowieso) mit einer einfachen p es beim Aufruf fehlschlägt (die Erklärung funktioniert natürlich):

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 

Was mir seltsam scheint, als zweiten Parameter ist bereits als Pos in der Definition getippt, also wie kommt es dazu, dass diese Drosseln, und nur bei Aufruf? Ich benutze Ghci BTW.

+0

Sie buchstabiert "Moves" als "move". – jrockway

Antwort

5

Sie haben ein "s" am Ende des Umzugs vergessen s Anruf, nicht wahr?

*Main> move [North, North] p 
+0

Ja, hat er. Er überholte etwas von Typ [a], als es 'Move 'erwartete. Und genau das sagt die Fehlermeldung. – jrockway