I'm currently working on a board evaluator in haskell. I'm trying to use map with a function with multiple parameters; I've read other SO questions regarding this but keep getting type errors so perhaps I'm just misunderstanding the Haskell types (I'm a Python programmer). Either way, here's the code:
scorePiecesRow [] _ = 0
scorePiecesRow (x:xs) y
| x == y = 1 + (scorePiecesRow xs y)
| x == '-' = 0 + (scorePiecesRow xs y)
| otherwise = -1 + (scorePiecesRow xs y)
scorePieces [] _ = 0
scorePieces board y = foldr (+) 0 (map (scorePiecesRow y) board)
scorePiecesRow
works just fine when I pass it anything like "wwwb--" 'w'
(which returns 3
), but as soon as I call scorePieces
(e.g. scorePieces ["www", "bb-"] 'w'
which should return 1
), I get a bunch of type errors:
<interactive>:37:14: Couldn't match expected type `Char' with actual type `[Char]' In the expression: "www" In the first argument of `scorePieces', namely `["www", "bb-"]' In the expression: scorePieces ["www", "bb-"] 'w' <interactive>:37:21: Couldn't match expected type `Char' with actual type `[Char]' In the expression: "bb-" In the first argument of `scorePieces', namely `["www", "bb-"]' In the expression: scorePieces ["www", "bb-"] 'w' <interactive>:37:28: Couldn't match expected type `[Char]' with actual type `Char' In the second argument of `scorePieces', namely 'w' In the expression: scorePieces ["www", "bb-"] 'w' In an equation for `it': it = scorePieces ["www", "bb-"] 'w'
I'm a bit confused by the error messages. The first one tells me, for example, that it's expecting Char
, but the first argument of scorePiecesRow
takes [Char]
.
If anyone could shed some light on this, it would be greatly appreciated!