Doubt regarding Types
Karthik Kumar
kaykaydreamz at yahoo.com
Mon Nov 3 20:40:59 EST 2003
Hi Artie,
You are right . I had changed the code as follows -
run = do c <- getBoardSize
validateBoardSize c
-- Get the board size as input
getBoardSize :: IO ( Int )
validateBoardSize :: Int -> IO ()
... and follows.
run function is the entry point.
Cheers
Karthik.
--- Artie Gold <artiegold at austin.rr.com> wrote:
> Hal Daume III wrote:
> > 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))
> >
> >
>
> Of course the board size is now lost and gone forever (and a
> function called `getBoardSize' returning an IO Bool is, well, a
> little counterintuitive). ;-)
>
> To the OP: What does the top level code look like?[1] That should
> guide the rest of the design.
>
> HTH,
> --ag
>
> [1] The question is rhetorical.
> --
> Artie Gold -- Austin, Texas
> Oh, for the good old days of regular old SPAM.
>
__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/
More information about the Haskell-Cafe
mailing list