On Mon, Nov 15, 2010 at 9:00 AM,
<beginners-request@haskell.org> wrote:
Send Beginners mailing list submissions to
beginners@haskell.org
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
beginners-request@haskell.org
You can reach the person managing the list at
beginners-owner@haskell.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Why the length function I wrote has such a type
signature? (Tom Murphy)
2. Re: Why the length function I wrote has such a type
signature? (Daniel Fischer)
3. Rewriting using State and/or Reader? (Paul Sargent)
4. Re: Rewriting using State and/or Reader? (Brent Yorgey)
5. (no subject) (David Schonberger)
6. Ralf Laemmel's riddle on surviving without the monad
transformation library (C K Kashyap)
----------------------------------------------------------------------
Message: 1
Date: Fri, 12 Nov 2010 14:14:25 -0500
From: Tom Murphy <amindfv@gmail.com>
Subject: Re: [Haskell-beginners] Why the length function I wrote has
such a type signature?
Cc: beginners@haskell.org
Message-ID:
<AANLkTimjA0auRvt8z35BStt604on374Nk4YXUfUVCxzy@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
>
>
>>
> And the type signature given by ghci is
>
>> myLength :: (Num t1) => [t] -> t1
>>
>
>
But why is the signature not written as:
myLength :: [t] -> (Num t1) => t1
or something similar?
I thought that the Num typeclass being first implied that it was the
function's first argument.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101112/896c2084/attachment-0001.html
------------------------------
Message: 2
Date: Fri, 12 Nov 2010 21:12:48 +0100
From: Daniel Fischer <daniel.is.fischer@web.de>
Subject: Re: [Haskell-beginners] Why the length function I wrote has
such a type signature?
To: beginners@haskell.org
Message-ID: <201011122112.48289.daniel.is.fischer@web.de>
Content-Type: text/plain; charset="utf-8"
On Friday 12 November 2010 20:14:25, Tom Murphy wrote:
> > And the type signature given by ghci is
> >
> >> myLength :: (Num t1) => [t] -> t1
>
> But why is the signature not written as:
> myLength :: [t] -> (Num t1) => t1
> or something similar?
>
> I thought that the Num typeclass being first implied that it was the
> function's first argument.
A type signature like myLength's consists of two parts,
- a context; here (Num t1)
- the part giving the type (subject to the constraints in the context).
The language definition says the context comes first, then "=>", finally
the type.
If contexts were always written next to the type they apply to, it would
lead to unreadable type signatures and repetition (the same constraint can
apply to several type variables).
Not to mention multi parameter type classes.
Strictly speaking, there's a third part, the quantification, but that's
usually left implicit (explicit foralls require a language extension).
So the full type is
myLength :: forall a n. (Num n) => [a] -> n
for all types a and n, if n is an instance of Num, myLength has (can have)
the type [a] -> n.
------------------------------
Message: 3
Date: Fri, 12 Nov 2010 21:38:35 +0000
From: Paul Sargent <psarge@gmail.com>
Subject: [Haskell-beginners] Rewriting using State and/or Reader?
To: beginners@haskell.org
Message-ID: <6751A8C2-8904-4C73-8846-18C6DD1A01EE@gmail.com>
Content-Type: text/plain; charset=us-ascii
Hi,
My Haskell project has quite a lot of code in it which either:
a) Uses a set of common values which are stored in an "environment" data type.
b) Takes a set of values, and updates them repetitively.
Often the same function will do both.
Up to now I've tended to avoid monad based code (except List and Maybe), but I'm thinking that maybe I can simplify some of my code.
The code for case a) might look something like:
data Env = Env {rateA :: Double,
valueB :: Double} deriving (Show)
f :: Env -> Double -> Double -> Double
f env a b = a * ra + b * g' b
where ra = rateA env
g' = g env
g :: Env -> Double -> Double
g env b = (valueB env) + b
Basically I'm threading the Env parameter through all the function calls that need it. Am I right in saying this sort of stuff is a good candidate for the Reader monad? What do I gain if I do it?
Similarly am I right in saying that this type of code is a good candidate for State (called repeatedly from some loop somewhere)?
updateList :: Double -> [Item] -> [Item]
updateList t xs = map (updateItemToTime t) xs
updateItemToTime :: Double -> Item -> Item
updateItemToTime = some function which does an incremental calculation for a time slice
(obviously the code is nonsense, I'm just trying to give a feel of the structures I'm using)
Then, if I did move the code to these monads, how well do the live together? ...and how about functions which use functions in multiple different state or reader monads?
Basically, I haven't seen the advantage of using the monadic way for this code, so what am I missing?
Thanks
Paul
------------------------------
Message: 4
Date: Fri, 12 Nov 2010 17:12:33 -0500
From: Brent Yorgey <byorgey@seas.upenn.edu>
Subject: Re: [Haskell-beginners] Rewriting using State and/or Reader?
To: beginners@haskell.org
Message-ID: <20101112221233.GA30754@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii
On Fri, Nov 12, 2010 at 09:38:35PM +0000, Paul Sargent wrote:
> Hi,
>
> My Haskell project has quite a lot of code in it which either:
>
> a) Uses a set of common values which are stored in an "environment" data type.
> b) Takes a set of values, and updates them repetitively.
>
> Often the same function will do both.
>
> Up to now I've tended to avoid monad based code (except List and Maybe), but I'm thinking that maybe I can simplify some of my code.
>
> The code for case a) might look something like:
>
> data Env = Env {rateA :: Double,
> valueB :: Double} deriving (Show)
>
> f :: Env -> Double -> Double -> Double
> f env a b = a * ra + b * g' b
> where ra = rateA env
> g' = g env
>
> g :: Env -> Double -> Double
> g env b = (valueB env) + b
>
> Basically I'm threading the Env parameter through all the function
> calls that need it. Am I right in saying this sort of stuff is a
> good candidate for the Reader monad? What do I gain if I do it?
Yes, this is a good candidate for Reader. You gain not having to
thread the environment around everywhere. Unfortunately, you lose a
bit in syntax. These particular functions
could be written like this:
f :: Double -> Double -> Reader Env Double
f a b = do ra <- asks rateA
g' <- g b
return a * ra + b * g'
g :: Double -> Reader Env Double
g b = asks valueB >>= \b' -> return (b + b')
You can make this a little better with Applicative syntax, especially
if you use InfixApplicative
(http://hackage.haskell.org/package/InfixApplicative):
f a b = (pure a <^(*)^> asks rateA) <^(+)^> (pure b <^(*)^> g b)
well... I guess you can decide whether you think that's any better.
At least it's closer to the original.
> Similarly am I right in saying that this type of code is a good candidate for State (called repeatedly from some loop somewhere)?
>
> updateList :: Double -> [Item] -> [Item]
> updateList t xs = map (updateItemToTime t) xs
>
> updateItemToTime :: Double -> Item -> Item
> updateItemToTime = some function which does an incremental calculation for a time slice
>
> (obviously the code is nonsense, I'm just trying to give a feel of
> the structures I'm using)
Yes, this could make use of State.
>
> Then, if I did move the code to these monads, how well do the live
> together? ...and how about functions which use functions in multiple
> different state or reader monads?
There are actually nice ways to make different monads live together,
but it can require some rather complex and abstract machinery. See
e.g. the Monatron package
(http://hackage.haskell.org/package/Monatron) and this paper:
http://people.cs.kuleuven.be/~tom.schrijvers/Research/papers/monad_zipper_draft.pdf
> Basically, I haven't seen the advantage of using the monadic way for
> this code, so what am I missing?
Not too much, necessarily. For the sorts of simple things it seems
like you're doing, I think the syntactic overhead outweighs the
benefits.
-Brent
------------------------------
Message: 5
Date: Fri, 12 Nov 2010 23:32:32 -0800
From: David Schonberger <llp_yyz@hotmail.com>
Subject: [Haskell-beginners] (no subject)
To: <beginners@haskell.org>, <holidayspecial@primetimeshuttle.com>,
<noreply@welcome.skype.com>, <service@paypal.com>,
<kitchenbath@hgtvnewsletters.com>
Message-ID: <COL121-W8D2305DF070F029B361669A340@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"
http://ungoalperlavita.org/mainlink.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101113/f2effc24/attachment-0001.html
------------------------------
Message: 6
Date: Mon, 15 Nov 2010 15:27:00 +0530
From: C K Kashyap <ckkashyap@gmail.com>
Subject: [haskell-beginners] Ralf Laemmel's riddle on surviving
without the monad transformation library
To: beginners@haskell.org
Message-ID:
<AANLkTim53tVCt4kDYbXDRJN=OLwc-iEQRvZzkuej+qWG@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
Can someone provide me the solution to the following riddle that Ralf
asked in his lecture at
http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Dr-Ralf-Lmmel-Advanced-Functional-Programming-Evolution-of-an-Interpreter
Riddle: define a custom made monad (only involving (->) and Either
String) to survive without the monad transformation library.
--
Regards,
Kashyap
------------------------------
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 29, Issue 18
*****************************************