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: RWH Ch. 27 TCP Server Example (Francesco Bochicchio)
2. Re: Re: Thinking about monads (Brent Yorgey) (Thomas Davie)
3. Re: Re: Thinking about monads (Brent Yorgey) (Brent Yorgey)
4. Partial Loading and Debugging with GHCI (aditya siram)
5. Re: Partial Loading and Debugging with GHCI (Sean Bartell)
----------------------------------------------------------------------
Message: 1
Date: Wed, 15 Apr 2009 05:41:06 +0200
From: Francesco Bochicchio <[email protected]>
Subject: Re: [Haskell-beginners] RWH Ch. 27 TCP Server Example
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
2009/4/14 aditya siram <[email protected]>
> Hi all,
> I am trying to understand the TCP Syslog Server example [1] from RWH.
> Specifically my question is on this piece of code:
> -- | Process incoming messages
> procMessages :: MVar () -> Socket -> SockAddr -> IO ()
> procMessages lock connsock clientaddr =
> do connhdl <- socketToHandle connsock ReadMode
> hSetBuffering connhdl LineBuffering
> messages <- hGetContents connhdl
> mapM_ (handle lock clientaddr) (lines messages)
> hClose connhdl
> handle lock clientaddr
> "syslogtcpserver.hs: client disconnected"
>
> How does control stay on "mapM_ (handle lock clientaddr) (lines
> messages)" ? It would seem that the server would handle one message
> and immediately close the handle and end - but it doesn't. I'm
> guessing this has something to do with laziness, but I don't see how
> to apply it.
I think you are right thinking about laziness. The function hGetContents
produces a lazy
result (messages), therefore any function that consumes messages could be
considered as 'iteratively calling' hGetContents to get new elements of
messages ...
Laziness is a Very Cool Feature ... when it does not bite you with thunk
accumulation problems :-)
Ciao
------
FB
>
>
> thanks ..
> -deech
>
> [1] http://book.realworldhaskell.org/read/sockets-and-syslog.html
> _______________________________________________
> 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/20090415/66a73b82/attachment-0001.htm
------------------------------
Message: 2
Date: Wed, 15 Apr 2009 08:19:48 +0200
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Re: Thinking about monads (Brent
Yorgey)
To: Arthur Chan <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
delsp=yes
On 15 Apr 2009, at 00:08, Arthur Chan wrote:
> You know, I was wondering... if Monads are a subset of Functors,
> and Applicative is a subset of Functors, and Monads are a subset of
> Applicative... shouldn't it be possible to tack on the definitions
> that automatically derive Functor and Applicative? Isn't it the
> case that there is really only one way to define Applicative for a
> Monad anyway? And isn't there only one way to define fmap for a
> Monad that makes sense?
Yes, but at the same time no.
Firstly, it's possible that you want an applicative instance that
disagrees with your monad one not common, and usually ugly, but
possible.
Secondly, it's often possible to implement the applicative/functor
methods in a much more efficient way by dealing with them specifically.
In reality, what we want to see is this:
class Pointed f where
pure :: a -> f a
class Functor f where
fmap :: (a -> b) -> f a -> f b
class (Functor f, Pointed f) => Applicative f where
(<*>) :: f (a -> b) -> f a -> f b
class (Applicative f) => Monad f where
join :: f (f a) -> f a
Then we only need to define each behavior in one place, and the tree
is neatly seperated out so that if we have something that isn't an
Applicative, we can stop implementing after Functor, and if we have
something that isn't a Monad, we can stop implementing after
Applicative.
Bob
------------------------------
Message: 3
Date: Thu, 16 Apr 2009 09:08:53 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Re: Thinking about monads (Brent
Yorgey)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Tue, Apr 14, 2009 at 03:08:05PM -0700, Arthur Chan wrote:
> You know, I was wondering... if Monads are a subset of Functors, and
> Applicative is a subset of Functors, and Monads are a subset of
> Applicative... shouldn't it be possible to tack on the definitions that
> automatically derive Functor and Applicative? Isn't it the case that there
> is really only one way to define Applicative for a Monad anyway? And isn't
> there only one way to define fmap for a Monad that makes sense?
Actually, it's already possible to do this, in a way. If you have a
Monad, then fmap is liftM, pure is return, and (<*>) is ap. So you
already have implementations of Functor and Applicative. In fact, I
routinely do this:
import Text.ParserCombinators.Parsec
instance Applicative (GenParser tok st) where
pure = return
(<*>) = ap
There are also various proposals which would help in automating this
sort of process, like "class aliases". But in general, having a nicer
class hierarchy as Bob suggests would be much better.
-Brent
------------------------------
Message: 4
Date: Thu, 16 Apr 2009 12:34:59 -0500
From: aditya siram <[email protected]>
Subject: [Haskell-beginners] Partial Loading and Debugging with GHCI
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi all,
I'm curious as to how one would go about debugging the following program:
hello :: String
hello = "Hello"
world :: String
world = "World"
number :: Int
number = 1
helloWorld :: String
helloWorld = hello ++ " " ++ world
-- This fails!
bad :: String
bad = hello ++ number
Predictably once I load this program into ghci I get the following error:
[1 of 1] Compiling Main ( TestCode.hs, interpreted )
TestCode.hs:14:15:
Couldn't match expected type `[Char]' against inferred type `Int'
In the second argument of `(++)', namely `number'
In the expression: hello ++ number
In the definition of `bad': bad = hello ++ number
Failed, modules loaded: none.
At this point what I want to do is to query the output type of the different
functions like so:
> :t hello
but GHCI won't let me because the program hasn't been loaded.
I would then have to comment out 'bad', reload the program and query output
types to my hearts content. This is fine for a small program, but when I
have functions are downriver from 'bad', it gets extremely cumbersome. Is
there a way to load a file such that functions that compile stay loaded in
the interpreter even if something else fails?
Thanks ...
-deech
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090416/1f21ec49/attachment-0001.htm
------------------------------
Message: 5
Date: Thu, 16 Apr 2009 16:33:48 -0400
From: Sean Bartell <[email protected]>
Subject: Re: [Haskell-beginners] Partial Loading and Debugging with
GHCI
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
You can comment out the definition of bad and add a new one:
bad = undefined
The file should load properly. Functions that use "bad" will compile too,
but if they try to evaluate it an error will be raised.
(sorry, forgot to hit "reply to all" the first time)
On Thu, Apr 16, 2009 at 1:34 PM, aditya siram <[email protected]>wrote:
> Hi all,
> I'm curious as to how one would go about debugging the following program:
>
> hello :: String
> hello = "Hello"
>
> world :: String
> world = "World"
>
> number :: Int
> number = 1
>
> helloWorld :: String
> helloWorld = hello ++ " " ++ world
>
> -- This fails!
> bad :: String
> bad = hello ++ number
>
> Predictably once I load this program into ghci I get the following error:
> [1 of 1] Compiling Main ( TestCode.hs, interpreted )
>
> TestCode.hs:14:15:
> Couldn't match expected type `[Char]' against inferred type `Int'
> In the second argument of `(++)', namely `number'
> In the expression: hello ++ number
> In the definition of `bad': bad = hello ++ number
> Failed, modules loaded: none.
>
> At this point what I want to do is to query the output type of the
> different functions like so:
> > :t hello
> but GHCI won't let me because the program hasn't been loaded.
>
> I would then have to comment out 'bad', reload the program and query output
> types to my hearts content. This is fine for a small program, but when I
> have functions are downriver from 'bad', it gets extremely cumbersome. Is
> there a way to load a file such that functions that compile stay loaded in
> the interpreter even if something else fails?
>
> Thanks ...
> -deech
>
> _______________________________________________
> 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/20090416/8e408a7c/attachment.htm
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 10, Issue 16
*****************************************