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. Lambda Functions (Philip Scott)
2. Re: Lambda Functions (Thomas Davie)
3. Re: Lambda Functions (Felipe Lessa)
4. Re: Lambda Functions (Brent Yorgey)
5. Re: Lambda Functions (Andrew Wagner)
6. using parametrized monads and Prelude.>> (frea)
7. Overriding >>= for a Monad? (Patrick LeBoutillier)
8. Re: Overriding >>= for a Monad? (Alexander Dunlap)
----------------------------------------------------------------------
Message: 1
Date: Thu, 26 Feb 2009 22:07:25 +0000
From: Philip Scott <[email protected]>
Subject: [Haskell-beginners] Lambda Functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Okay, thanks everyone for the help with the last question; I've got
another one for you if you are keen. It's short and sweet.
You can easily define a lambda expression that takes a tuple - e.g. in ghci
> let foo = \(x,y) -> 42
foo :: (t, t1) -> Integer
> foo (5,5)
42
Yay. Now that isn't a very exciting function. Tt takes two anythings and
gives you a nice meaningful number. Now let us say for some perverse
reason I want to make a lambda to test for equality. That is,
reimplement the functionality of (==) using, well, (==).
> let foo2 = \(x,y) -> x == y
foo2 :: ((), ()) -> Bool
Uh-oh. Now things are getting a little odd. I am not entirely sure what
that type signature means (I was expecting to see something about the
types having to be the same and an instance of the Eq class but alas..)
The function certainly doesn't do what I want it to:
> foo2 (5,5)
No instance for (Num ())
arising from the literal `5' at <interactive>:1:6
Possible fix: add an instance declaration for (Num ())
In the expression: 5
In the first argument of `foo2', namely `(5, 5)'
In the expression: foo2 (5, 5)
This is itself a stupid problem, but it is a distilled version of some
trouble I was having writing a filter that works on a list of tuples.
Any pointers or slaps in the face for being too stupid warmly welcomed.
I have already tried my usual trick of smothering the thing parentheses
but it appeared to be all in vain.
All the best,
Philip
------------------------------
Message: 2
Date: Thu, 26 Feb 2009 23:25:42 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Lambda Functions
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
delsp=yes
On 26 Feb 2009, at 23:07, Philip Scott wrote:
> Okay, thanks everyone for the help with the last question; I've got
> another one for you if you are keen. It's short and sweet.
>
> You can easily define a lambda expression that takes a tuple - e.g.
> in ghci
>
> > let foo = \(x,y) -> 42
> foo :: (t, t1) -> Integer
>
> > foo (5,5)
> 42
>
> Yay. Now that isn't a very exciting function. Tt takes two anythings
> and gives you a nice meaningful number. Now let us say for some
> perverse reason I want to make a lambda to test for equality. That
> is, reimplement the functionality of (==) using, well, (==).
>
> > let foo2 = \(x,y) -> x == y
>
> foo2 :: ((), ()) -> Bool
>
> Uh-oh. Now things are getting a little odd. I am not entirely sure
> what that type signature means (I was expecting to see something
> about the types having to be the same and an instance of the Eq
> class but alas..) The function certainly doesn't do what I want it to:
>
> > foo2 (5,5)
>
> No instance for (Num ())
> arising from the literal `5' at <interactive>:1:6
> Possible fix: add an instance declaration for (Num ())
> In the expression: 5
> In the first argument of `foo2', namely `(5, 5)'
> In the expression: foo2 (5, 5)
>
> This is itself a stupid problem, but it is a distilled version of
> some trouble I was having writing a filter that works on a list of
> tuples. Any pointers or slaps in the face for being too stupid
> warmly welcomed. I have already tried my usual trick of smothering
> the thing parentheses but it appeared to be all in vain.
I'm not 100% certain here, so someone may correct me, but I think this
is what's going on:
foo2 has no arguments. Because of this, ghci makes it a CAF. At this
point, the monomorphism restriction kicks in, and the CAF has to be
monomorphic. ghc then chooses a type for it, and defaulting choses
the most simple type it can find () (this is the type that contains
one value () ).
If instead, you do let foo3 (x,y) = x == y, you will get the type you
expected.
Another way to write it ofc would simply be uncurry (==).
Bob
------------------------------
Message: 3
Date: Thu, 26 Feb 2009 19:51:32 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Lambda Functions
To: Thomas Davie <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, Feb 26, 2009 at 7:25 PM, Thomas Davie <[email protected]> wrote:
> I'm not 100% certain here, so someone may correct me, but I think this is
> what's going on:
[snip]
Yep, that's right. Is it just me or the monomorphism restriction
suddenly became the hot topic here on beginners?
> Another way to write it ofc would simply be uncurry (==).
Better yet, write a type signature. I highly recommend writing type
signatures for *all* top-level definitions, including non-exported
ones:
- You avoid most cases where the monomorphism restriction would bother you.
- You avoid type errors on other functions (sometimes you make a
mistake but the code type checks with a wrong signature, and the type
error shows up only when you use the function elsewhere).
- It gives you some insights before implementing, and helps whoever
reads your code.
- It allows you to specialize the code when polymorphism is not needed.
Probably there are other reasons as well, but these are the most prominent.
--
Felipe.
------------------------------
Message: 4
Date: Thu, 26 Feb 2009 17:59:36 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Lambda Functions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Feb 26, 2009 at 07:51:32PM -0300, Felipe Lessa wrote:
>
> Better yet, write a type signature. I highly recommend writing type
> signatures for *all* top-level definitions, including non-exported
> ones:
>
> - It gives you some insights before implementing
Hear, hear! If you can't write down the intended type of a function,
you have very little hope of implementing it correctly. =)
-Brent
------------------------------
Message: 5
Date: Thu, 26 Feb 2009 18:01:31 -0500
From: Andrew Wagner <[email protected]>
Subject: Re: [Haskell-beginners] Lambda Functions
To: Felipe Lessa <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Excellent list. It seems like every time I hear about a problem with the
MMR, I go "wow, how come I never ran across that? oh yeah, because I write
type signatures..."
On Thu, Feb 26, 2009 at 5:51 PM, Felipe Lessa <[email protected]>wrote:
> On Thu, Feb 26, 2009 at 7:25 PM, Thomas Davie <[email protected]> wrote:
> > I'm not 100% certain here, so someone may correct me, but I think this is
> > what's going on:
> [snip]
>
> Yep, that's right. Is it just me or the monomorphism restriction
> suddenly became the hot topic here on beginners?
>
> > Another way to write it ofc would simply be uncurry (==).
>
> Better yet, write a type signature. I highly recommend writing type
> signatures for *all* top-level definitions, including non-exported
> ones:
>
> - You avoid most cases where the monomorphism restriction would bother you.
>
> - You avoid type errors on other functions (sometimes you make a
> mistake but the code type checks with a wrong signature, and the type
> error shows up only when you use the function elsewhere).
>
> - It gives you some insights before implementing, and helps whoever
> reads your code.
>
> - It allows you to specialize the code when polymorphism is not needed.
>
> Probably there are other reasons as well, but these are the most prominent.
>
> --
> Felipe.
> _______________________________________________
> 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/20090226/8c993f12/attachment-0001.htm
------------------------------
Message: 6
Date: Fri, 27 Feb 2009 01:06:37 +0100
From: frea <[email protected]>
Subject: [Haskell-beginners] using parametrized monads and Prelude.>>
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Hello,
I would like to create few functions which use parametrized monads
(defined as in
http://computationalthoughts.blogspot.com/2009/02/comment-on-parameterized-monads.html
) and export their functionality outside a module and use them in a do
statement.
For example three exported funtions :
func1 num = do
x <- get
put (show (x + num))
func2 num = do
x <- get
put (((read x)::Int) + num)
execWith0 actions = runState actions 0
And in an another module, which has Prelude imported :
k = execWith0 ( do
func1 10
func2 5)
Unfortunately this doesn't compile as in the second module >> is
different than in PMonad. Is there any way which I could make it work?
Michal
------------------------------
Message: 7
Date: Thu, 26 Feb 2009 20:46:32 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Overriding >>= for a Monad?
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
I'm using Control.Monad.StateT as such:
data TapState = TapState {
planSet :: Bool,
noPlan :: Bool,
skipAll :: Bool,
testDied :: Bool,
expectedTests :: Int,
executedTests :: Int,
failedTests :: Int
} deriving (Show)
type TAP a = StateT TapState IO a
but I'd like to provide my own >>= function. Is there a way to
"derive" a new type from StateT in order to implement my own >>=?
Is thin done using "instance"?
Thanks,
Patrick
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 8
Date: Thu, 26 Feb 2009 18:24:15 -0800
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] Overriding >>= for a Monad?
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Feb 26, 2009 at 5:46 PM, Patrick LeBoutillier
<[email protected]> wrote:
> Hi all,
>
> I'm using Control.Monad.StateT as such:
>
> data TapState = TapState {
> planSet :: Bool,
> noPlan :: Bool,
> skipAll :: Bool,
> testDied :: Bool,
> expectedTests :: Int,
> executedTests :: Int,
> failedTests :: Int
> } deriving (Show)
>
> type TAP a = StateT TapState IO a
>
> but I'd like to provide my own >>= function. Is there a way to
> "derive" a new type from StateT in order to implement my own >>=?
> Is thin done using "instance"?
>
> Thanks,
>
> Patrick
>
> --
> =====================
> Patrick LeBoutillier
> Rosemère, Québec, Canada
The most direct way would probably to copy StateT's source code and
tinker with the >>= definition (also change the name to MyStateT or
whatever).
You could also define
> newtype MyStateT s a = MyStateT (StateT s IO a)
>
> instance Monad (MyStateT s) where
> return = MyStateT . return
> (>>=) = ...
Alex
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 8, Issue 27
****************************************