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.  Why WinGHCi does not complain with no import? (Angus Comber)
   2. Re:  Why WinGHCi does not complain with no        import? (fa-ml)
   3. Re:  How to run Parser in chapter 8 Graham Hutton Haskell
      book (Angus Comber)
   4. Re:  Why WinGHCi does not complain with no        import?
      (Daniel Trstenjak)
   5.  Picking apart getLine (Angus Comber)
   6. Re:  Picking apart getLine (David McBride)


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

Message: 1
Date: Tue, 7 Jan 2014 13:58:37 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Why WinGHCi does not complain with no
        import?
Message-ID:
        <caatguhwpccy_v7z9xtruvjfwub9c4uvbse-ostx3tygy7sq...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I created my own version of getLine like this:

getLine' :: IO String
getLine'        = do x <- getChar
                     if x == '\n' then
                       return []
                     else
                       do
                         xs <- getLine'
                         return (x:xs)


Basically I created a new file called test.hs and copied above code into
it.  I opened WinGHCi afresh and :load test.hs

I can run getLine' in WinGHCi with no problem.  But how does WinGHCi know
about getChar and IO?

I assumed I would have to import like this:

import System.IO

Is it because WinGHCi does this import for me?

If I write this program and invoke using runghci:

getLine' :: IO String
getLine'        = do x <- getChar
                     if x == '\n' then
                       return []
                     else
                       do
                         xs <- getLine'
                         return (x:xs)


main = do
       getLine'


It also works without complaining.

Is there a list of built-in libraries?  Is that platform dependent?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140107/000fdb37/attachment-0001.html>

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

Message: 2
Date: Tue, 7 Jan 2014 15:05:44 +0100
From: fa-ml <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Why WinGHCi does not complain with no
        import?
Message-ID: <20140107140544.GA349@efikamx>
Content-Type: text/plain; charset=us-ascii

On Tue, Jan 07, 2014 at 01:58:37PM +0000, Angus Comber wrote:
> I can run getLine' in WinGHCi with no problem.  But how does WinGHCi know
> about getChar and IO?

getChar is defined in the Prelude [1] (and the Prelude gets imported by default
in every program/module)

[1] 
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:getChar


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

Message: 3
Date: Tue, 7 Jan 2014 14:09:08 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: Re: [Haskell-beginners] How to run Parser in chapter 8 Graham
        Hutton Haskell book
Message-ID:
        <CAAtGUhXHovkgZyUL_q2+9gSCwYwSEm2C=7ejab2kxi6egms...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

For anyone else benefit, this is how I fixed issue.

1. download Parsing.lhs from author's website.
2. Also download parser.lhs and paste contents into my prog_haskell.hs
file, removing text comments and > symbols.
3. At top of my prog_haskell.hs file you need import Parsing

You can then call the functions in WinGHCi, eg eval "3+5*2"

For some reason I was not able to work out how to import both Parsing.lhs
and parser.lhs together.  Will have to read up on modules later.



On 7 January 2014 11:43, Christian Maeder <[email protected]> wrote:

> Hi Angus,
>
> did you get an answer for your question? (Just pressing "reply" only
> answers to the list, as happened to my earlier answer to your initial
> question.)
>
> I don't know if WinGHCi works like ghci under linux (and I don't know
> which files Parsing.lhs and parser.lhs you tried to load.)
>
> At the ghci Prompt you can type ":browse" to see which names are in scope.
> Maybe parser.lhs could not be loaded for some reason that I cannot
> reproduce. (Maybe eval is explicitly not exported?)
>
> Cheers Christian
>
>
> Am 03.01.2014 18:43, schrieb Angus Comber:
>
>> I found some notes on the authors website explaining that the book code
>> would no longer work.  So I downloaded two files: Parsing.lhs and
>> parser.lhs.  In WinGHCi I then issued :load on both files.
>>
>> parser.lhs contains a function, eval, as follows:
>>
>> <other stuff above>
>>  > eval                          :: String -> Int
>>  > eval xs                       =  case (parse expr xs) of
>>  >                                     [(n,[])]  -> n
>>  >                                     [(_,out)] -> error ("unused input
>> " ++ out)
>>  >                                     []        -> error "invalid input"
>>
>> But in WinGHCi if I run: eval "2*3+4" I get error := Not in scope: `eval'
>>
>> I assume this is some sort of namespaces feature of Haskell that I have
>> not read about?  Or I somehow have to state what library modules to
>> use???  Can anyone help?
>>
>>
>>
>>
>>
>>
>>
>> On 3 January 2014 15:58, Angus Comber <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>>     I am reading Chapter 8 of Programming Haskell by Graham Hutton and
>>     trying to run the code in the book.
>>
>>     It seems that there are already defined library implementations of
>>     Parser so I used Parser' and generally ' on the end of each
>>     identifier to attempt to eliminate this problem.
>>
>>     So the code I copied from the book is:
>>
>>     type Parser' a = String -> [(a, String)]
>>
>>     return' :: a -> Parser' a
>>     return' v = \x -> [(v, x)]
>>
>>     failure' :: Parser' a
>>     failure' = \inp -> []
>>
>>     -- item doesn't seem to conflict with anything so no '
>>     item :: Parser' Char
>>     item = \inp -> case inp of
>>                          [] -> []
>>                          (x:xs) -> [(x,xs)]
>>
>>
>>     parse' :: Parser' a -> String -> [(a, String)]
>>     parse' p inp = p inp
>>
>>
>>     p :: Parser' (Char, Char)
>>     p = do  x <- item
>>              item
>>              y <- item
>>              return' (x,y)
>>
>>     When run from WinGHCi I get error:
>>
>>     prog_haskell.hs:458:9:
>>         Couldn't match type `[(Char, String)]' with `Char'
>>          Expected type: String -> [((Char, Char), String)]
>>            Actual type: Parser' ([(Char, String)], [(Char, String)])
>>          In the return type of a call of return'
>>          In a stmt of a 'do' block: return' (x, y)
>>          In the expression:
>>            do { x <- item;
>>                 item;
>>                 y <- item;
>>                 return' (x, y) }
>>
>>     458.9 is line at end with return' (x,y)
>>
>>
>>     Also in the chapter was the definition of >>= sequencing.  as in:
>>
>>     (>>=) :: Parser a -> (a -> Parser b) -> Parser b
>>     p >>= f  =  \inp -> case parse p inp of
>>                            [] -> []
>>                            [(v,out)] -> parse (f v) out
>>
>>     But I assumed this would already be in standard Haskell library and
>>     so I didn't need to define.  But even when I did add, still get same
>>     error.
>>
>>     How do I fix this?
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140107/6739ddf4/attachment-0001.html>

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

Message: 4
Date: Tue, 7 Jan 2014 15:36:17 +0100
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Why WinGHCi does not complain with no
        import?
Message-ID: <20140107143617.GA15314@machine>
Content-Type: text/plain; charset=us-ascii


Hi Angus,

On Tue, Jan 07, 2014 at 01:58:37PM +0000, Angus Comber wrote:
> Is it because WinGHCi does this import for me?

It's defined by the Haskell standard, that you're automatically getting the 
'Prelude' module:
http://haddocks.fpcomplete.com/fp/7.4.2/20130829-168/base/Prelude.html


Greetings,
Daniel


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

Message: 5
Date: Tue, 7 Jan 2014 16:54:15 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Picking apart getLine
Message-ID:
        <CAAtGUhWuGrEs60H4PbFRm_xO=s1nacz_phwtbcwt-31fhzv...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Before looking at getLine, I can understand this:

getnumber :: IO Int
getnumber = do x <- getChar
               if isDigit x then
                     return (ord x - ord '0')
               else
                     return 0

OK, it is not a very useful function but at least I understand it.  return
is required so that the function returns an IO Int.

But I don't understand this:

getLine' :: IO String
getLine'        = do x <- getChar
                     if x == '\n' then
                       return []
                     else
                       do
                         xs <- getLine'
                         return (x:xs)


I can understand what will happen if a user enters a newline (only).
 return [] brings the empty list into the monadic world.

But what is happening if x is not a newline?

xs <- getLine' will recursively call getChar and retrieve another character
from the input stream.  But it will do this BEFORE the return (x:xs) - so
what is happening to all the head elements in the list - the x element?

It is difficult to picture in my mind how this is working.  I understand
recursion but this looks tricky.

Can someone help me work this out?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140107/b8c21697/attachment-0001.html>

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

Message: 6
Date: Tue, 7 Jan 2014 12:15:02 -0500
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Picking apart getLine
Message-ID:
        <can+tr42k1_cb4_uefdif2ono2nzmuo_twqoswwpvbu8ftkz...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

You have the idea.  The x is fetched with getChar, then it sits in
that context until the return is executed.

So the x is sitting there and getLine' is called.  It makes its own x
via getChar, then maybe getLine' is called again.  Each getLine' sits
there with its own version of x until finally the last getLine' get's
a \n, and then returns a [].  Then the whole thing unwinds by
prepending x to [], then x to [x], then another x to [x,x], until
there are no more x's to return, and you have the whole string.

Hopefully that paragraph makes sense.

On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber <[email protected]> wrote:
> Before looking at getLine, I can understand this:
>
> getnumber :: IO Int
> getnumber = do x <- getChar
>                if isDigit x then
>                      return (ord x - ord '0')
>                else
>                      return 0
>
> OK, it is not a very useful function but at least I understand it.  return
> is required so that the function returns an IO Int.
>
> But I don't understand this:
>
> getLine' :: IO String
> getLine'        = do x <- getChar
>                      if x == '\n' then
>                        return []
>                      else
>                        do
>                          xs <- getLine'
>                          return (x:xs)
>
>
> I can understand what will happen if a user enters a newline (only).  return
> [] brings the empty list into the monadic world.
>
> But what is happening if x is not a newline?
>
> xs <- getLine' will recursively call getChar and retrieve another character
> from the input stream.  But it will do this BEFORE the return (x:xs) - so
> what is happening to all the head elements in the list - the x element?
>
> It is difficult to picture in my mind how this is working.  I understand
> recursion but this looks tricky.
>
> Can someone help me work this out?
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 67, Issue 12
*****************************************

Reply via email to