nntpnews.net

Global Usenet Archiver


Register

[Haskell-beginners] define action getInt like getLine

Reply

  #1  
Old 02-02-10, 09:21 PM
kane96@gmx.de
 
Posts: n/a
Default [Haskell-beginners] define action getInt like getLine

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.
--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher!
Code:
Content visible to registered users only.
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #2  
Old 02-02-10, 09:39 PM
Rahul Kapoor
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

> Hi,
> how can I write an action
> getInt :: IO Int
> that works like
> getLine :: IO String


The most literal way to write getInt would be:

getInt :: IO Int
getInt = do
s <- getLine
return (read s)

which is just a more verbose version of:

getInt' = read `fmap` getLine

The above versions don't do any error checking, so you might
prefer a getInt :: IO Maybe Int which returns Nothing in case the input
is not an integer.

You should read up on Monadic IO in Haskell. The Real World Haskell book
is probably a good starting point.

Rahul
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #3  
Old 02-02-10, 09:41 PM
Daniel Fischer
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

Am Dienstag 02 Februar 2010 22:20:03 schrieb
Code:
Content visible to registered users only.
:
> 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
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #4  
Old 02-02-10, 10:01 PM
legajid
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

Hi,

here's another idea :

saisie_choix :: IO Int
saisie_choix=do
putStrLn "Type in 1 digit :"
xchoix <- getLine
if (length xchoix) /= 1 || head xchoix < '0' || head xchoix > '9'
then saisie_choix
else do
let nchoix=read xchoix::Int
return nchoix

or, with maybe :

{- -------------- MAYBE ------------------ -}

-- Fonction principale
mainmaybe=do
x <- lire
if isNothing x
then do
putStrLn "Donnee invalide"
else do
putStr " \b"

putStrLn ("Resultat : " ++ show (f x))

-- Définition de la fonction de calcul
f Nothing = -9999
f (Just x) = 2 * x

-- Fonction d'IO qui valide la saisie
lire :: IO (Maybe Integer)
lire = do
-- saisie
xn <- getLine
let x=read xn ::Integer

-- validation
if x < 5
then do
return (Just x)
else do
return Nothing

Didier.



Rahul Kapoor a écrit :
>> Hi,
>> how can I write an action
>> getInt :: IO Int
>> that works like
>> getLine :: IO String
>>

>
> The most literal way to write getInt would be:
>
> getInt :: IO Int
> getInt = do
> s <- getLine
> return (read s)
>
> which is just a more verbose version of:
>
> getInt' = read `fmap` getLine
>
> The above versions don't do any error checking, so you might
> prefer a getInt :: IO Maybe Int which returns Nothing in case the input
> is not an integer.
>
> You should read up on Monadic IO in Haskell. The Real World Haskell book
> is probably a good starting point.
>
> Rahul
> _______________________________________________
> Beginners mailing list
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.
>
>

_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #5  
Old 02-02-10, 10:18 PM
Daniel Fischer
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

Am Dienstag 02 Februar 2010 23:06:41 schrieb legajid:
> -- Définition de la fonction de calcul
> f Nothing = -9999
> f (Just x) = 2 * x


That's already in the standard libs:

ghci> :t maybe
maybe :: b -> (a -> b) -> Maybe a -> b
ghci> maybe (-9999) (*2) (Just $ fromEnum 'k')
214
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #6  
Old 07-02-10, 08:03 PM
kane96@gmx.de
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

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 < - >
> An:
Code:
Content visible to registered users only.
> CC:
Code:
Content visible to registered users only.
> Betreff: Re: [Haskell-beginners] define action getInt like getLine


> Am Dienstag 02 Februar 2010 22:20:03 schrieb
Code:
Content visible to registered users only.
:
> > 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


--
NEU: Mit GMX DSL über 1000,- ¿ sparen!
Code:
Content visible to registered users only.
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #7  
Old 07-02-10, 08:25 PM
Stephen Blackheath [to Haskell-Beginners]
 
Posts: n/a
Default 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

Code:
Content visible to registered users only.
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 < - >
>> An:
Code:
Content visible to registered users only.
>> CC:
Code:
Content visible to registered users only.
>> Betreff: Re: [Haskell-beginners] define action getInt like getLine

>
>> Am Dienstag 02 Februar 2010 22:20:03 schrieb
Code:
Content visible to registered users only.
:
>>> 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

>

_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #8  
Old 08-02-10, 09:19 PM
kane96@gmx.de
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

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]" < - >
> An:
Code:
Content visible to registered users only.
> CC:
Code:
Content visible to registered users only.
> 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
>
>
Code:
Content visible to registered users only.
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 < - >
> >> An:
Code:
Content visible to registered users only.
> >> CC:
Code:
Content visible to registered users only.
> >> Betreff: Re: [Haskell-beginners] define action getInt like getLine

> >
> >> Am Dienstag 02 Februar 2010 22:20:03 schrieb
Code:
Content visible to registered users only.
:
> >>> 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
Code:
Content visible to registered users only.
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #9  
Old 08-02-10, 09:43 PM
kane96@gmx.de
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

but I have to write the action
readMonth :: IO Month
on my own and still don't have an idea how to do it


-------- Original-Nachricht --------
> Datum: Mon, 8 Feb 2010 21:27:42 +0000
> Von: Stephen Tetley < - >
> An:
> CC:
Code:
Content visible to registered users only.
> Betreff: Re: [Haskell-beginners] define action getInt like getLine


> 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:
>
>
Code:
Content visible to registered users only.
>
>
> Best wishes
>
> Stephen
>
> On 8 February 2010 21:19, < - > 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]"

> < - >
> >> An:
Code:
Content visible to registered users only.
> >> CC:
Code:
Content visible to registered users only.
> >> 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
> >>
> >>
Code:
Content visible to registered users only.
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 < - >
> >> >> An:
Code:
Content visible to registered users only.
> >> >> CC:
Code:
Content visible to registered users only.
> >> >> Betreff: Re: [Haskell-beginners] define action getInt like getLine
> >> >
> >> >> Am Dienstag 02 Februar 2010 22:20:03 schrieb
Code:
Content visible to registered users only.
:
> >> >>> 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
Code:
Content visible to registered users only.
> > _______________________________________________
> > Beginners mailing list
> >
Code:
Content visible to registered users only.
> >
Code:
Content visible to registered users only.
> >

> _______________________________________________
> Beginners mailing list
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.


--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter
Code:
Content visible to registered users only.
_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #10  
Old 08-02-10, 10:01 PM
Daniel Fischer
 
Posts: n/a
Default Re: [Haskell-beginners] define action getInt like getLine

Am Montag 08 Februar 2010 22:43:01 schrieb
Code:
Content visible to registered users only.
:
> but I have to write the action
> readMonth :: IO Month
> on my own and still don't have an idea how to do it


Go take a look at

Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
The documentation should help you.


_______________________________________________
Beginners mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] how to define records containing finite lengthlists. Srikanth K fa.haskell 2 02-12-09 06:43 PM
[Haskell-beginners] hide Perl code inside Haskell program Hong Yang fa.haskell 3 12-09-09 09:13 AM
[Haskell-beginners] Haskell data structures: cheap immutablemanipulation and nested equality? Anand Patil fa.haskell 2 06-09-09 04:07 PM
[Haskell-beginners] HaL4: Haskell-Meeting in Germany, 12th June 2009 Janis Voigtlaender fa.haskell 1 02-06-09 12:21 PM
[Haskell-cafe] getLine and ^C on Windows Lyle Kopnicky fa.haskell 3 01-12-08 01:37 PM


All times are GMT +1. The time now is 05:04 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System