Doubt regarding Types
Hal Daume III
hdaume at ISI.EDU
Mon Nov 3 19:35:56 EST 2003
Hi,
On Mon, 3 Nov 2003, Karthik Kumar wrote:
> -- Convert a string to an integer.
> -- This works perfectly fine.
> atoi :: [Char] -> Int
> atoi (h : []) = if isDigit h then digitToInt h else 0
> atoi (h : t) = if isDigit h then digitToInt h * ( 10 ^ length t) +
> atoi t else 0
you can use "read" for this.
> -- validateBoardSize
> -- To validate the board size
> validateBoardSize :: Int -> Bool
> validateBoardSize d = (d == 9 || d == 13 || d == 19 )
this looks fine
> getBoardSize :: IO Bool
> -- TODO : What could be the type of getBoardSize
> getBoardSize = do c <- getLine
> validateBoardSize ( atoi c )
>
> ERROR "test1.hs":21 - Type error in final generator
> *** Term : validateBoardSize (atoi c)
> *** Type : Bool
> *** Does not match : IO a
this is telling you something important. it's saying that the final
generator, "validateBoardSize (atoi c)" has type Bool, but it's expecting
it to have type IO something. You need to "lift" the pure Bool value into
IO by saying return:
> getBoardSize = do
> c <- getLine
> return (validateBoardSize (read c))
--
Hal Daume III | hdaume at isi.edu
"Arrest this man, he talks in maths." | www.isi.edu/~hdaume
More information about the Haskell-Cafe
mailing list