[Haskell-beginners] How to run Parser in chapter 8 Graham Hutton Haskell book

Angus Comber anguscomber at gmail.com
Tue Jan 7 14:09:08 UTC 2014


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 <Christian.Maeder at dfki.de> 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 <anguscomber at gmail.com
>> <mailto:anguscomber at gmail.com>> 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
>> Beginners at haskell.org
>> 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.html>


More information about the Beginners mailing list