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.  How to run Parser in chapter 8 Graham Hutton     Haskell book
      (Angus Comber)
   2.  Designing complex Haskell programs (Courtney Robinson)
   3. Re:  How to run Parser in chapter 8 Graham Hutton Haskell
      book (Christian Maeder)
   4. Re:  Designing complex Haskell programs (Bob Ippolito)
   5. Re:  Designing complex Haskell programs (Daniel Trstenjak)
   6. Re:  How to run Parser in chapter 8 Graham        Hutton Haskell
      book (Angus Comber)


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

Message: 1
Date: Fri, 3 Jan 2014 15:58:52 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] How to run Parser in chapter 8 Graham
        Hutton  Haskell book
Message-ID:
        <CAAtGUhVt3gztjV+W6GgQiqWAMZo+9D=t1qrxk7rgrcgmfbp...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140103/61d0e1bb/attachment-0001.html>

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

Message: 2
Date: Fri, 3 Jan 2014 16:45:08 +0000
From: Courtney Robinson <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Designing complex Haskell programs
Message-ID:
        <cago6xjbukobujn6yceebfwimre3qoyqsnpcznsv5e6tk5og...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I'm trying to take the training wheels of and moving more of my code base
to Haskell from C++ but finding it increasingly tricky.

I have a subset of a gossip protocol written in C++.
When a server comes online it connects to 1 or more nodes already in the
cluster and get data from them about other nodes they know of.

The new node merges the information and keeps a copy of the merged view.
Every so often it contacts the nodes it knows about and refreshes the
merged view. It also must have the up to date view ready to be sent in
response to a new node joining.

I currently can't wrap my head around how to maintain this state. How would
a more experienced Haskeller approach this problem? Code is OK if it
demonstrates a particular point but I'm more interested in the line of
thought that would go into designing a solution as I suspect that'll be
more useful as I get further into the migration.

As a gauge to you for my current level in Haskell. I read and understand
most Haskell programs fine. I write some but currently heavily rely on
hackage/hoogle docs for APIs, even some common ones.

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140103/2cf178d7/attachment-0001.html>

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

Message: 3
Date: Fri, 03 Jan 2014 17:50:16 +0100
From: Christian Maeder <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] How to run Parser in chapter 8 Graham
        Hutton Haskell book
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

the Monad instance used for the do-notation of your parser p is that
of the function type (Monad ((->) r)) due to your Parser' type synonym.

This instance is not suitable. In p x and y get values of type [(Char, 
String)] via item, but you would want x and y to be of type Char.

In order to fix it, you could desugar the do-Notation and use a proper 
replacement for >>=.

(Alternatively, your Parser type could be turned into a proper data type 
with a proper Monad instance.)

HTH Christian

Am 03.01.2014 16:58, schrieb Angus Comber:
> 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
>



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

Message: 4
Date: Fri, 3 Jan 2014 09:16:28 -0800
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Designing complex Haskell programs
Message-ID:
        <cacwmpm9ujj5okc5gsjc0cm02dtxnxqdg_hbolhcab3zy4gp...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Generally speaking, state lives on the call stack in functional programming
languages that have tail call elimination. Modification of the state is
done by recursion with a new value for the state. This is more or less
equivalent to a "do while" loop in imperative programming.

myServer :: State -> IO ()
myServer state = do
  state' <- updateState state
  myServer state'

For the concurrency, Control.Concurrent or Cloud Haskell (for a higher
level Erlang-like approach) is probably the way to go here. Parallel and
Concurrent Programming in Haskell is a great resource:
http://chimera.labs.oreilly.com/books/1230000000929


On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson <[email protected]>wrote:

> I'm trying to take the training wheels of and moving more of my code base
> to Haskell from C++ but finding it increasingly tricky.
>
> I have a subset of a gossip protocol written in C++.
> When a server comes online it connects to 1 or more nodes already in the
> cluster and get data from them about other nodes they know of.
>
> The new node merges the information and keeps a copy of the merged view.
> Every so often it contacts the nodes it knows about and refreshes the
> merged view. It also must have the up to date view ready to be sent in
> response to a new node joining.
>
> I currently can't wrap my head around how to maintain this state. How
> would a more experienced Haskeller approach this problem? Code is OK if it
> demonstrates a particular point but I'm more interested in the line of
> thought that would go into designing a solution as I suspect that'll be
> more useful as I get further into the migration.
>
> As a gauge to you for my current level in Haskell. I read and understand
> most Haskell programs fine. I write some but currently heavily rely on
> hackage/hoogle docs for APIs, even some common ones.
>
> Thanks
>
> _______________________________________________
> 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/20140103/86f9e21c/attachment-0001.html>

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

Message: 5
Date: Fri, 3 Jan 2014 18:26:21 +0100
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Designing complex Haskell programs
Message-ID: <20140103172621.GA4880@machine>
Content-Type: text/plain; charset=us-ascii


Hi Courtney,

On Fri, Jan 03, 2014 at 04:45:08PM +0000, Courtney Robinson wrote:
> I currently can't wrap my head around how to maintain this state. How
> would a more experienced Haskeller approach this problem?

I think that it mostly boils down how your application loop should look
like or which implementation options you have.


If you're able to poll the received data, then you might just have something 
like:

appLoop :: AppState -> IO ()
appLoop appState = do
   appState' <- pollData appState
   appLoop appState'

You're reading the received data and modifying your state and then
recursive call appLoop.


If you want or have to use callbacks, then one option is to put your
application state into an IORef (a mutable variable), which is shared by
your application loop and your callback code e.g.:

main :: IO ()
main = do
   appRef <- newIORef AppState { ... }
   setWhateverCallback $ \receivedData ->
      modifyIORef appRef $ \appData ->
         -- modify your appData by receivedData

   appLoop appRef


appLoop :: IORef AppState -> IO ()
appLoop appRef = do
   modifyIORef appRef $ \appData ->
      -- do whatever you want with your appData

   appLoop appRef


If you're using multiple threads, then you might want to use
a MVar instead of an IORef.


Greetings,
Daniel


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

Message: 6
Date: Fri, 3 Jan 2014 17:43:59 +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:
        <caatguhwttfm9v2zutcfl4wmrsttrrgobozkqqwtlcjyeids...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

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]> 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?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140103/71465d9a/attachment.html>

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

Subject: Digest Footer

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


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

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

Reply via email to