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.  TCP Server (Raphael P?bst)
   2. Re:  TCP Server (Ertugrul Soeylemez)
   3.  Pearls of Functional Algorithm Design - find the smallest
      free number (Costello, Roger L.)
   4. Re:  TCP Server (Raphael P?bst)
   5. Re:  Pearls of Functional Algorithm Design - find the
      smallest free number (Felipe Almeida Lessa)
   6. Re:  Reading JSON using Text.JSON (Magnus Therning)
   7. Re:  Reading JSON using Text.JSON (Christopher Done)
   8. Re:  Reading JSON using Text.JSON (Magnus Therning)
   9. Re:  Reading JSON using Text.JSON (Adrien Haxaire)


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

Message: 1
Date: Mon, 16 May 2011 15:01:58 +0200
From: Raphael P?bst <[email protected]>
Subject: [Haskell-beginners] TCP Server
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi!
I need a TCP server for my current project and am now wondering if I
have to write it from scratch or if there is a package out there that
lets me avoid this. I have seen examples of TCP servers for example in
the Real World Haskell book, but they are a bit confusing to me at the
moment, so I'd like to avoid tackling this one.

Thanks

Raf



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

Message: 2
Date: Mon, 16 May 2011 15:30:37 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] TCP Server
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Raphael P?bst <[email protected]> wrote:

> I need a TCP server for my current project and am now wondering if I
> have to write it from scratch or if there is a package out there that
> lets me avoid this. I have seen examples of TCP servers for example in
> the Real World Haskell book, but they are a bit confusing to me at the
> moment, so I'd like to avoid tackling this one.

TCP servers are surprisingly easy to write.  You will want to learn how
to write concurrent programs in Haskell first, then you would just
listen on a port and accept connections in a loop like this:

    import Control.Exception
    import Network
    import System.IO

    handleClient :: Handle -> IO ()
    handleClient h = ...

    server :: IO ()
    server = do
        socket <- listenOn (PortNumber 4000)
        forever $ do
            (h, hostname, port) <- accept socket
            forkIO $ handleClient h `finally` hClose h

This is how it works basically.  Communication between the threads can
be carried out by using concurrency constructs like MVar and/or by using
software transactional memory (STM).  For really elegant concurrent
applications you should also learn how to use STM.

For learning concurrent programming I found the Control.Concurrent
documentation to be sufficient, but YMMV.  For STM you will find good
references in HaskellWiki's STM page.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





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

Message: 3
Date: Mon, 16 May 2011 09:33:41 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] Pearls of Functional Algorithm Design -
        find the smallest free number
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi Folks,

Here is a recurring problem:

    Find the difference between what you have versus what you need

Here are some examples:

1. Cooking: a recipe calls for this list of ingredients: eggs, flour, milk, 
chocolate. In my kitchen I have some ingredients. Is there a difference between 
what the recipe requires versus what I have in my kitchen?

2. Product Inventory: the inventory sheet says one thing. The actual products 
on the shelf says another. Is there a difference between what the inventory 
sheet says versus what is actually on the shelves?


This recurring problem calls for a nice algorithm to solve it.

I am reading a fabulous book, Pearls of Algorithm Design [1]. It describes how 
to solve the problem. I created Powerpoint slides that expand on the book's 
description:

http://www.xfront.com/Pearls-of-Functional-Algorithm-Design/Chapter1/Find-the-smallest-free-number.pptx
 

Comments welcome.

/Roger

[1] 
http://www.amazon.com/Pearls-Functional-Algorithm-Design-Richard/dp/0521513383/ref=sr_1_1?ie=UTF8&s=books&qid=1305469753&sr=8-1
 



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

Message: 4
Date: Mon, 16 May 2011 15:42:13 +0200
From: Raphael P?bst <[email protected]>
Subject: Re: [Haskell-beginners] TCP Server
To: Ertugrul Soeylemez <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Thanks for the enlightenment!
I'll try to work out if I need STM or just MVar.

On 5/16/11, Ertugrul Soeylemez <[email protected]> wrote:
> Raphael P?bst <[email protected]> wrote:
>
>> I need a TCP server for my current project and am now wondering if I
>> have to write it from scratch or if there is a package out there that
>> lets me avoid this. I have seen examples of TCP servers for example in
>> the Real World Haskell book, but they are a bit confusing to me at the
>> moment, so I'd like to avoid tackling this one.
>
> TCP servers are surprisingly easy to write.  You will want to learn how
> to write concurrent programs in Haskell first, then you would just
> listen on a port and accept connections in a loop like this:
>
>     import Control.Exception
>     import Network
>     import System.IO
>
>     handleClient :: Handle -> IO ()
>     handleClient h = ...
>
>     server :: IO ()
>     server = do
>         socket <- listenOn (PortNumber 4000)
>         forever $ do
>             (h, hostname, port) <- accept socket
>             forkIO $ handleClient h `finally` hClose h
>
> This is how it works basically.  Communication between the threads can
> be carried out by using concurrency constructs like MVar and/or by using
> software transactional memory (STM).  For really elegant concurrent
> applications you should also learn how to use STM.
>
> For learning concurrent programming I found the Control.Concurrent
> documentation to be sufficient, but YMMV.  For STM you will find good
> references in HaskellWiki's STM page.
>
>
> Greets,
> Ertugrul
>
>
> --
> nightmare = unsafePerformIO (getWrongWife >>= sex)
> http://ertes.de/
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 5
Date: Mon, 16 May 2011 13:20:08 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Pearls of Functional Algorithm Design
        - find the smallest free number
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

So the solution works in O(n) time to find the first item ingredient
that the recipe needs but you don't have.  It works in linear time
because there are strong assumptions about what your items are, so a
pigeonhole sort [1] is used in the checklist function.

We may also relax the conditions about what the items are if we say
that we want them in Sets (from Data.Set).  Then difference [2] is
exactly what we want and gives us all the items that are missing in
O(n) time.

With a similar restriction, but using Data.Map, we can also give a
slightly generalized algorithm where there may be multiple counts of
the items (e.g. multiple TVs in the shelves).  Then differenceWith [3]
also does the hard work for us in O(n) time.

Now, relaxing the assumptions about what the items are and allowing
them to come into lists in any other, I don't if it is possible to
solve the problem in less than O(n log n) time.  It is easy to do it
in O(n log n) time if the items are instances of Ord by constructing a
Set.  I suspect this is a lower bound.  And, unfortunately, I don't
see how the Algorithm 2 could be changed in a simple way to run in O(n
log n) time.

Cheers,

[1] http://en.wikipedia.org/wiki/Pigeonhole_sort
[2] 
http://hackage.haskell.org/packages/archive/containers/0.4.0.0/doc/html/Data-Set.html#v:difference
[3] 
http://hackage.haskell.org/packages/archive/containers/0.4.0.0/doc/html/Data-Map.html#v:differenceWith

-- 
Felipe.



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

Message: 6
Date: Mon, 16 May 2011 19:59:16 +0100
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Reading JSON using Text.JSON
To: Christopher Done <[email protected]>
Cc: [email protected]
Message-ID: <20110516185916.GA5135@ohann>
Content-Type: text/plain; charset="us-ascii"

On Mon, May 16, 2011 at 01:38:02AM +0200, Christopher Done wrote:
> On 15 May 2011 23:20, Magnus Therning <[email protected]> wrote:
> 
> > I wrote down some stuff from my first look at json, maybe it can
> > help you in getting further?
> >
> > http://therning.org/magnus/archives/719
> 
> FWIW, rather than:
> 
> readJSON (JSObject obj) = let
>         jsonObjAssoc = fromJSObject obj
>     in do
>         para <- mLookup "para" jsonObjAssoc >>= readJSON
>         seeAlso <- mLookup "GlossSeeAlso" jsonObjAssoc >>= readJSON
>         return $ GlossDef
>             { glossDefPara = para
>             , glossDefSeeAlso = seeAlso
>             }
> 
> You can just write:
> 
> readJSON object = do obj <- readJSON object
>                      GlossDef <$> valFromObj "para" obj
>                               <*> valFromObj "GlossSeeAlso" obj

FWIW, read the first comment ;-)

I still consider the monadic style easier to read than the
applicative so that's what I usually start with.

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4 
email: [email protected]   jabber: [email protected]
twitter: magthe               http://therning.org/magnus

I invented the term Object-Oriented, and I can tell you I did not have
C++ in mind.
     -- Alan Kay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110516/f641a548/attachment-0001.pgp>

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

Message: 7
Date: Mon, 16 May 2011 21:07:25 +0200
From: Christopher Done <[email protected]>
Subject: Re: [Haskell-beginners] Reading JSON using Text.JSON
To: Magnus Therning <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On 16 May 2011 20:59, Magnus Therning <[email protected]> wrote:
>> readJSON object = do obj <- readJSON object
>> ? ? ? ? ? ? ? ? ? ? ?GlossDef <$> valFromObj "para" obj
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <*> valFromObj "GlossSeeAlso" obj
>
> FWIW, read the first comment ;-)
>
> I still consider the monadic style easier to read than the
> applicative so that's what I usually start with.

I was more referring to the fact that you're re-implementing
valFromObj and needlessly pattern matching.



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

Message: 8
Date: Mon, 16 May 2011 20:21:38 +0100
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Reading JSON using Text.JSON
To: [email protected]
Message-ID: <20110516192138.GB5135@ohann>
Content-Type: text/plain; charset="iso-8859-1"

On Mon, May 16, 2011 at 09:07:25PM +0200, Christopher Done wrote:
> On 16 May 2011 20:59, Magnus Therning <[email protected]> wrote:
> >> readJSON object = do obj <- readJSON object
> >> ? ? ? ? ? ? ? ? ? ? ?GlossDef <$> valFromObj "para" obj
> >> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <*> valFromObj "GlossSeeAlso" obj
> >
> > FWIW, read the first comment ;-)
> >
> > I still consider the monadic style easier to read than the
> > applicative so that's what I usually start with.
> 
> I was more referring to the fact that you're re-implementing
> valFromObj and needlessly pattern matching.

Indeed I am, thanks for pointing that out.

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4 
email: [email protected]   jabber: [email protected]
twitter: magthe               http://therning.org/magnus


Perl is another example of filling a tiny, short-term need, and then
being a real problem in the longer term.
     -- Alan Kay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110516/e16f5926/attachment-0001.pgp>

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

Message: 9
Date: Mon, 16 May 2011 22:40:23 +0200
From: Adrien Haxaire <[email protected]>
Subject: Re: [Haskell-beginners] Reading JSON using Text.JSON
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello,

Thanks for your help and advices, I managed to read the sample file I have !

At the moment I can read a node like this :

instance JSON Node where
   readJSON object = do
     obj <- readJSON object
     coords <- valFromObj "coordinates" obj
     number <- valFromObj "number" obj
     return (Node coords number)
   showJSON (Node coords number) = makeObj [("coordinates",showJSON 
coords),("number", showJSON number)]

I will take a look at how to extend it, as I will have much more than 
nodes to read.

Still, I like the solution using <$>, <*>, etc, and would like to use 
it. I will need to read more about Applicative typeclasses before, 
though. But that's why I wanted to learn Haskell: I have many things to 
discover :)

The only thing that I do not understand yet, is why I can't use the 
'Node' data which I defined in another module. If the Node data 
declaration is in the file in which I read the JSON, it is ok. If I 
import the module in which I want to place it, ghc tells me that the 
data constructor 'Node' is not in scope.

in Input.hs:
import Elements (Node)

and in Elements.hs:


module Elements (
        Node(..)
        , Element(..)
        , Material(..)
        ) where
[...]
data Node = Node Coordinates Number
           deriving (Eq, Ord, Show)

Regarding pattern matching, I really like it. I am just discovering that 
it is way more powerful than I first thought!


To recap, the clouds which where on this JSON handling are gone, now it 
is mainly practicing it which will make me do precisely what I want with 
it.

Thanks a lot again for your tutorial, help and comments. This is very 
cheerful, and mmakes me as a part of the community, even if I am just 
starting :)

best regards,
Adrien




Le 16/05/2011 21:21, Magnus Therning a ?crit :
> On Mon, May 16, 2011 at 09:07:25PM +0200, Christopher Done wrote:
>> On 16 May 2011 20:59, Magnus Therning<[email protected]>  wrote:
>>>> readJSON object = do obj<- readJSON object
>>>>                       GlossDef<$>  valFromObj "para" obj
>>>>                                <*>  valFromObj "GlossSeeAlso" obj
>>>
>>> FWIW, read the first comment ;-)
>>>
>>> I still consider the monadic style easier to read than the
>>> applicative so that's what I usually start with.
>>
>> I was more referring to the fact that you're re-implementing
>> valFromObj and needlessly pattern matching.
>
> Indeed I am, thanks for pointing that out.
>
> /M
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




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

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


End of Beginners Digest, Vol 35, Issue 34
*****************************************

Reply via email to