Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  define action getInt like getLine
      (Stephen Blackheath [to Haskell-Beginners])
   2. Re:  defining expression
      (Stephen Blackheath [to Haskell-Beginners])
   3.  Problems with System-Eval-Haskell (Joe Fox)
   4. Re:  Problems with System-Eval-Haskell (Daniel Schoepe)
   5. Re:  define action getInt like getLine ([email protected])
   6. Re:  define action getInt like getLine (Stephen Tetley)
   7. Re:  define action getInt like getLine ([email protected])


----------------------------------------------------------------------

Message: 1
Date: Mon, 08 Feb 2010 09:25:13 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

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

[email protected] 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 <[email protected]>
>> An: [email protected]
>> CC: [email protected]
>> Betreff: Re: [Haskell-beginners] define action getInt like getLine
> 
>> Am Dienstag 02 Februar 2010 22:20:03 schrieb [email protected]:
>>> 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
> 


------------------------------

Message: 2
Date: Mon, 08 Feb 2010 09:43:26 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] defining expression
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

John,

I'm seeing the error you describe "Couldn't match expected type `Expression'
            against inferred type `IO b'" on this line:

evalStep d (Let n e1 e2) = do
        putStrLn ("Adding definition for "++n)     <---

The trouble here is that evalStep's type is :: Dict -> Expression -> 
Expression, and Haskell doesn't allow general I/O in a function with a 
return type other than IO <something>.  This restrictiveness is pretty 
much the whole point of Haskell.  You have two choices, either...

1. Change the type of evalStep to Dict -> Expression -> IO Expression, 
and of any function that calls it (and so on up the call chain), or

2. Use Debug.trace for your debug output, like this:

     trace ("adding/removing definition of "++n) $
     evalStep d (addEntry n e1 d) e2

If you want 'adding' and 'removing definition of' as separate messages 
before and after (sequenced relative to other traces you might add), 
this is not straightforward, but it can be done.  Essentially in pure 
Haskell code you have to think in terms of data dependency rather than 
sequence in time.


Steve

John Moore wrote:
> Hi,
>    This gives me the explaination that it expecting a Expression but it 
> been giving a IO b can some explain whats going on please.
>  
> import Maybe
> data Expression = Val Double
>                | Add Expression Expression
>                | Subtract Expression Expression
>                | Multiply Expression Expression
>                | Divide Expression Expression
>         | Var String
>         | Let String Expression Expression
>         deriving Show
> demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 
> 7))(Val 30))
> type Dict =[(String,Expression)]
> emptyDict :: Dict
> emptyDict = []
> addEntry :: String->Expression ->Dict -> Dict
> addEntry n e d = (n,e): d
> lookupEntry :: String -> Dict -> Maybe Expression
> lookupEntry n [] = Nothing
> lookupEntry n (x:xs) = if (n == k)
>    then (Just v)
>                 else lookupEntry n xs
>    where (k,v) = x
> evalStep :: Dict -> Expression ->  Expression
> evalStep d(Val x)=  (Val x)
> 
> evalStep d(Add x y)
>   = case x of
>       (Val a) -> case y of
>                    (Val b) -> Val (a+b)
>                    left -> Add x (evalStep d y)
>       right -> Add (evalStep d x)y
> evalStep d(Subtract x y)
>   = case x of
>       (Val a) -> case y of
>                    (Val b) -> Val (a-b)
>                    left -> Subtract x (evalStep d y)
>       right -> Subtract (evalStep d x)y
> evalStep d(Multiply x y)
>   = case x of
>       (Val a) -> case y of
>                    (Val b) -> Val (a*b)
>                    left -> Multiply x (evalStep d y)
>       right -> Multiply (evalStep d x)y
> evalStep d (Divide x y)
>   = case x of
>       (Val a) -> case y of
>                    (Val b) -> Val (a/b)
>                    left -> Divide x (evalStep d y)
>       right -> Divide (evalStep d x)y
> evalStep d (Let n e1 e2) = do
>        putStrLn ("Adding definition for "++n)
>        v <- evalStep d (addEntry n e1 d) e2
>        putStrLn ("Removing definition for "++n)
>        return v
> 
> evaluate :: Dict-> [Expression] -> Expression -> IO ()
> evaluate d(x:xs) e = do
>      putStrLn (show e)
>      putStrLn "Do another step (y/n) or rollback (r)? :"
>      c <- getLine
>      case c of
>        "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up 
> history
>                 
>        "r" ->  case (x:xs) of
>   (x:xs)-> evaluate d xs x
>   []-> do { putStrLn "Empty"
>    ;evaluate d(x:xs) e
>   }
>        "n"  ->  putStrLn $ "Ok you said no :" ++ c
>                                           
>                 
> John
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


------------------------------

Message: 3
Date: Mon, 8 Feb 2010 10:55:58 +0530
From: Joe Fox <[email protected]>
Subject: [Haskell-beginners] Problems with System-Eval-Haskell
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi

I am fairly new to haskell ,
I was trying to use Eval function in haskell but its giving me a problem ,

 even the example code given in *
http://www.cse.unsw.edu.au/~dons/hs-plugins/html/System-Eval-Haskell.html*<http://www.cse.unsw.edu.au/%7Edons/hs-plugins/html/System-Eval-Haskell.html>

do i <- eval "1 + 6 :: Int" [] :: IO (Maybe Int)
    when (isJust i) $ putStrLn (show (fromJust i))




is not working for me , it gives following error

on the commandline: Warning: -Onot is deprecated: Use -O0 instead

and doesnt print 7 , i guess that is what is expected to be printed.

Can any one point me in the right way how to use eval function ?


Thanks
Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100207/56978857/attachment-0001.html

------------------------------

Message: 4
Date: Mon, 08 Feb 2010 19:20:11 +0100
From: Daniel Schoepe <[email protected]>
Subject: Re: [Haskell-beginners] Problems with System-Eval-Haskell
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf8"

Excerpts from Joe Fox's message of Mon Feb 08 06:25:58 +0100 2010:
> Hi
> [..]
> Can any one point me in the right way how to use eval function ?
> 
> 
> Thanks
> Joe

I haven't used the module you mentioned but hint worked quite
well for me when I needed a way to evaluate Haskell expressions at runtime:

http://hackage.haskell.org/package/hint
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100208/d2957862/signature-0001.bin

------------------------------

Message: 5
Date: Mon, 08 Feb 2010 22:19:07 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

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]" 
> <[email protected]>
> An: [email protected]
> CC: [email protected]
> 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
> 
> [email protected] 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 <[email protected]>
> >> An: [email protected]
> >> CC: [email protected]
> >> Betreff: Re: [Haskell-beginners] define action getInt like getLine
> > 
> >> Am Dienstag 02 Februar 2010 22:20:03 schrieb [email protected]:
> >>> 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


------------------------------

Message: 6
Date: Mon, 8 Feb 2010 21:27:42 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

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,  <[email protected]> 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]" 
>> <[email protected]>
>> An: [email protected]
>> CC: [email protected]
>> 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
>>
>> [email protected] 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 <[email protected]>
>> >> An: [email protected]
>> >> CC: [email protected]
>> >> Betreff: Re: [Haskell-beginners] define action getInt like getLine
>> >
>> >> Am Dienstag 02 Februar 2010 22:20:03 schrieb [email protected]:
>> >>> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


------------------------------

Message: 7
Date: Mon, 08 Feb 2010 22:43:01 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: Stephen Tetley <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

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 <[email protected]>
> An: 
> CC: [email protected]
> 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:
> 
> http://www.haskell.org/pipermail/haskell-cafe/2010-January/072177.html
> 
> 
> Best wishes
> 
> Stephen
> 
> On 8 February 2010 21:19,  <[email protected]> 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]"
> <[email protected]>
> >> An: [email protected]
> >> CC: [email protected]
> >> 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
> >>
> >> [email protected] 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 <[email protected]>
> >> >> An: [email protected]
> >> >> CC: [email protected]
> >> >> Betreff: Re: [Haskell-beginners] define action getInt like getLine
> >> >
> >> >> Am Dienstag 02 Februar 2010 22:20:03 schrieb [email protected]:
> >> >>> 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
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 20, Issue 14
*****************************************

Reply via email to