[Haskell-beginners] define action getInt like getLine
Stephen Tetley
stephen.tetley at gmail.com
Mon Feb 8 16:27:42 EST 2010
Hello
You can derive Read:
> data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct | Nov | Dec deriving (Eq,Enum,Show,Read)
This will certainly throw an error on failure (might not be an error
you would want to transmit to a user though), and it will only read
"Jan" not "jan" or "January"...
Plenty of good suggestions in this thread:
http://www.haskell.org/pipermail/haskell-cafe/2010-January/072177.html
Best wishes
Stephen
On 8 February 2010 21:19, <kane96 at gmx.de> wrote:
> I want to read a "Month" from input and if this month issn't declared in my data type "Month" I want to throw an error message.
>
> data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct | Nov | Dec deriving (Eq,Enum,Show)
>
>
>
> -------- Original-Nachricht --------
>> Datum: Mon, 08 Feb 2010 09:25:13 +1300
>> Von: "Stephen Blackheath [to Haskell-Beginners]" <mutilating.cauliflowers.stephen at blacksapphire.com>
>> An: kane96 at gmx.de
>> CC: beginners at haskell.org
>> Betreff: Re: [Haskell-beginners] define action getInt like getLine
>
>> Hi there,
>>
>> Data types are required to start with a capital letter, so you'll have
>> to call it MyDatatype. I can't see where readLn is defined. Can you
>> paste a bit more of the code? I don't quite understand how your reading
>> of your own data type is meant to work - normally you would use read or
>> reads from the Read type class.
>>
>> A more subtle point - because of the way lazy evaluation works, it is
>> generally better to use 'fail' rather than 'error' when in a monad. In
>> some monads it's possible that 'error' may do nothing.
>>
>>
>> Steve
>>
>> kane96 at gmx.de wrote:
>> > thanks so far.
>> > I used "getInt = fmap read getLine", because I think it's enough for me
>> >
>> > I have to do it for a more complex case of reading an own data type
>> (myDatatype) with is deriving Show and return an error otherwise.
>> > I tried the following which didn't work:
>> >
>> > readMyDatatype :: IO myDatatype
>> > readMyDatatype = do
>> > if readLn == (show readLn)
>> > then return readLn
>> > else do error "input error"
>> >
>> >
>> > -------- Original-Nachricht --------
>> >> Datum: Tue, 2 Feb 2010 22:39:07 +0100
>> >> Von: Daniel Fischer <daniel.is.fischer at web.de>
>> >> An: beginners at haskell.org
>> >> CC: kane96 at gmx.de
>> >> Betreff: Re: [Haskell-beginners] define action getInt like getLine
>> >
>> >> Am Dienstag 02 Februar 2010 22:20:03 schrieb kane96 at gmx.de:
>> >>> Hi,
>> >>> how can I write an action
>> >>> getInt :: IO Int
>> >>> that works like
>> >>> getLine :: IO String
>> >>> but for Int instead of String. I know that I can read the input with
>> >>> getLine and then convert it by using read, but don't know how to write
>> >>> it as an action. I tried getInt :: IO Int
>> >>> getInt read <- getLine
>> >>> but that doesn't work.
>> >> There are many possibilities.
>> >>
>> >> The shortest is
>> >>
>> >> getInt :: IO Int
>> >> getInt = readLn
>> >>
>> >> another short and sweet is
>> >>
>> >> getInt :: IO Int
>> >> getInt = fmap read getLine -- or liftM read getLine
>> >>
>> >> But neither of these deals well with malformed input, if that's a
>> >> possibility to reckon with, use e.g. the reads function
>> >>
>> >> getInt :: IO Int
>> >> getInt = do
>> >> inp <- getLine
>> >> case reads inp of
>> >> ((a,tl):_) | all isSpace tl -> return a
>> >> _ -> handle malformed input
>> >
>
> --
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
More information about the Beginners
mailing list