I'm trying to create a function for a board game that will read a position on the board as a string and convert to a coordinate that can be used in the program e.g. "D4 => (3,3), "F2" => (5,1)".
So far I have this:
getCoord :: String -> Maybe(Int, Int)
getCoord s =
let alphas = "ABCDEFGH"
coord1 = head(s)
coord2 = tail(s)
in ((elemIndex coord1 alphas)-1, read(head coord2)-1)
I'm still learning about the use of Maybe in Haskell and am encountering the error:
• Couldn't match expected type ‘Maybe (Int, Int)’
with actual type ‘(Maybe Int, Integer)’
• In the expression:
((elemIndex coord1 alphas) - 1, read (head coord2) - 1)
Would appreciate your help on where I might be going wrong. Thanks!
Maybe (Int, Int), the Int tuple is the argument ofMaybe. But(Maybe Int, Integer)is a tuple ofMaybe Intand anotherInt. So if the value of the Maybe is Nothing, for the second case you still need to provide the second Int.headandtail.getCoord [coord1, coord2] = .... Shorter and longer strings can be handled separately.elemIndexreturns aMaybe Int, not anInt. You can't directly subtract 1 from that value.