
ahhh, I didn't know that it's so simple with read :) -------- Original-Nachricht --------
Datum: Mon, 8 Feb 2010 22:26:59 +0000 Von: Stephen Tetley
An: kane96@gmx.de Betreff: Re: [Haskell-beginners] define action getInt like getLine
Hello
Upthread Daniel Fisher posted this one
getInt :: IO Int getInt = fmap read getLine
If you check the type of read
*GHCi> :t read read :: (Read a) => String -> a
'reads' is polymorphic it will read anything from a String - well within reason, anything that is an instance of the Read type class. The "(Read a) => " part of the type signature indicates this constraint - that "a" ( the return type of the function read) must be an instance of Read.
As I wrote, you can easily derive an instance of Read by adding Read to the deriving clause of your data type, so the next thing to do is write a function swapping as few parts from Daniel's example as possible:
-- Here you have to make a change as you want a Month rather than an Int, so swap the ??? for what you want... getMonth :: IO ???
-- The function definition can stay the same - none of the components in Daniel's definition actually depended on reading an Int: getMonth = fmap read getLine
On 8 February 2010 21:43,
wrote: but I have to write the action readMonth :: IO Month on my own and still don't have an idea how to do it
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01