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: Understanding State (Jan Jakubuv)
2. Re: Understanding State (Jan Jakubuv)
3. Re: Understanding State (Maur??cio)
4. Re: decorate-sort-undecorate in haskell (Ivan Uemlianin)
5. The Applicative instance for ((->) a) (Ian Duncan)
6. Re: The Applicative instance for ((->) a) (Tony Morris)
7. Re: The Applicative instance for ((->) a) (Thomas Davie)
8. Re: The Applicative instance for ((->) a) (Tony Morris)
9. List.sort (Patrick LeBoutillier)
10. Re: List.sort (Thomas Davie)
----------------------------------------------------------------------
Message: 1
Date: Fri, 10 Jul 2009 10:46:19 +0100
From: Jan Jakubuv <[email protected]>
Subject: Re: [Haskell-beginners] Understanding State
To: Geoffrey Marchant <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hello Geoffrey,
when you really want to make the first function parameter (`a -> (r,a)`) a
monad, you can use `r` as the state instead of `a` (and `[a]`). Then both
the first parameter and the result are in the same monad `State r` and you
can write:
update2 :: State r a -> Int -> [a] -> State r [a]
update2 sm 0 (a:as) = sm >>= return . (:as)
update2 sm i (a:as) = update2 sm (i-1) as >>= return . (a:)
Nevertheless in both cases (update' and update2) you need to pass some bogus
initial state (or a value in the case of update') to run the computation. In
this case I would use the `Writer r` monad anyway.
Finally just note that `State a` and `State [a]` are different monads and
they can not be easily used in the same computation (although it's indeed
possible).
Sincerely,
Jan.
On Thu, Jul 09, 2009 at 10:34:29PM -0600, Geoffrey Marchant wrote:
>
> Which leads to me writing this:
>
> > update' s 0 = do
> > (a:as) <- get
> > let (r, a') = runState s a
> > put (a':as)
> > return r
> > update' s i = do
> > (a:as) <- get
> > put as
> > r <- update' s (i-1)
> > as' <- get
> > put (a:as')
> > return r
>
--
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.
------------------------------
Message: 2
Date: Fri, 10 Jul 2009 10:59:39 +0100
From: Jan Jakubuv <[email protected]>
Subject: Re: [Haskell-beginners] Understanding State
To: Jan Jakubuv <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hi again,
sorry, now I see that it is not probably what you wanted because the first
monad parameter does not depend on `a`. So better try this version where the
first parameter is not a monad at all:
update3 :: (a -> (r,a)) -> Int -> [a] -> State r [a]
update3 f 0 (a:as) = let (r,a') = f a in put r >> return (a':as)
update3 f i (a:as) = update3 f (i-1) as >>= return . (a:)
Sincerely,
Jan.
On Fri, Jul 10, 2009 at 10:46:19AM +0100, Jan Jakubuv wrote:
> Hello Geoffrey,
>
> when you really want to make the first function parameter (`a -> (r,a)`) a
> monad, you can use `r` as the state instead of `a` (and `[a]`). Then both
> the first parameter and the result are in the same monad `State r` and you
> can write:
>
> update2 :: State r a -> Int -> [a] -> State r [a]
> update2 sm 0 (a:as) = sm >>= return . (:as)
> update2 sm i (a:as) = update2 sm (i-1) as >>= return . (a:)
>
--
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.
------------------------------
Message: 3
Date: Fri, 10 Jul 2009 10:42:12 -0300
From: Maur??cio <[email protected]>
Subject: [Haskell-beginners] Re: Understanding State
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> sorry, now I see that it is not probably what you wanted because the first
> monad parameter does not depend on `a`. So better try this version where the
> first parameter is not a monad at all:
Geoffrey,
Maybe you can give us an example on how you intend to use that
code. Any of Jan's examples, or even others, can be a choice
depending on that. For instance, are you going to update a full
list with a single Int parameter, and then update a list many
times with changing parameters? Are you going to take a single
element and update it many times? Where are those Ints and rs
comming from?
Maurício
------------------------------
Message: 4
Date: Sat, 11 Jul 2009 11:28:40 +0100
From: Ivan Uemlianin <[email protected]>
Subject: Re: [Haskell-beginners] decorate-sort-undecorate in haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Dear All
> I'll report back once I've written up.
I hope this doesn't count as blog spam.
I've written up the new Haskell language features from the responses to
my query on decorate-sort-undecorate:
http://llaisdy.wordpress.com/2009/07/11/decorate-sort-undecorate-in-haskell-advanced
dot (function composition) and dollar look very handy, and of course
lambda. The Arrow robot &&& I think I shall leave aside for now.
> Thanks to everyone for your comments.
And best wishes
Ivan
--
============================================================
Ivan A. Uemlianin
Speech Technology Research and Development
[email protected]
www.llaisdy.com
llaisdy.wordpress.com
www.linkedin.com/in/ivanuemlianin
"Froh, froh! Wie seine Sonnen, seine Sonnen fliegen"
(Schiller, Beethoven)
============================================================
------------------------------
Message: 5
Date: Sat, 11 Jul 2009 19:54:24 +0900
From: Ian Duncan <[email protected]>
Subject: [Haskell-beginners] The Applicative instance for ((->) a)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hey folks, I understand most of what is going on with the applicative
class, but I can't figure out why this instance is useful:
"instance Applicative ((->) a) where..."
Can anyone offer some intuition into how this is could be used?
Ian Duncan
------------------------------
Message: 6
Date: Sat, 11 Jul 2009 21:54:32 +1000
From: Tony Morris <[email protected]>
Subject: Re: [Haskell-beginners] The Applicative instance for ((->) a)
To: Ian Duncan <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Here are a couple of examples
> (+) <$> Just 7 <*> Just 8
Just 15
> (*) <$> [1, 2, 3] <*> [4, 5, 6]
[4,5,6,8,10,12,12,15,18]
Ian Duncan wrote:
> Hey folks, I understand most of what is going on with the applicative
> class, but I can't figure out why this instance is useful:
> "instance Applicative ((->) a) where..."
>
> Can anyone offer some intuition into how this is could be used?
>
> Ian Duncan
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Tony Morris
http://tmorris.net/
------------------------------
Message: 7
Date: Sat, 11 Jul 2009 14:03:50 +0200
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] The Applicative instance for ((->) a)
To: Tony Morris <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed
On 11 Jul 2009, at 13:54, Tony Morris wrote:
> Here are a couple of examples
>
>> (+) <$> Just 7 <*> Just 8
> Just 15
>
>> (*) <$> [1, 2, 3] <*> [4, 5, 6]
> [4,5,6,8,10,12,12,15,18]
Those are for Maybe and [] respectively though, not ((->) a)
Bob
------------------------------
Message: 8
Date: Sat, 11 Jul 2009 22:30:32 +1000
From: Tony Morris <[email protected]>
Subject: Re: [Haskell-beginners] The Applicative instance for ((->) a)
To: Thomas Davie <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Woops sorry, try again.
> let f = (+) <$> (+1) <*> (*7) in f 2
17
Consider:
\f g a -> f a `k` g a
can be rewritten:
liftA2 k
Thomas Davie wrote:
>
> On 11 Jul 2009, at 13:54, Tony Morris wrote:
>
>> Here are a couple of examples
>>
>>> (+) <$> Just 7 <*> Just 8
>> Just 15
>>
>>> (*) <$> [1, 2, 3] <*> [4, 5, 6]
>> [4,5,6,8,10,12,12,15,18]
>
> Those are for Maybe and [] respectively though, not ((->) a)
>
> Bob
>
--
Tony Morris
http://tmorris.net/
------------------------------
Message: 9
Date: Sat, 11 Jul 2009 09:06:21 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] List.sort
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I see the type of the List.sort function is:
Ord a => [a] -> [a]
I don't understand how this type can allow it to sort pairs:
Prelude> :m +List
Prelude List> sort [(1, "1"), (3, "3"), (2, "2")]
[(1,"1"),(2,"2"),(3,"3")]
How can a pair by of type "Ord a"? Is it a rule that the first element
is automatically used?
Patrick
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 10
Date: Sat, 11 Jul 2009 15:13:27 +0200
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] List.sort
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
instance (Ord a, Ord b) => Ord (a,b) where
compare (x,y) (z,w) =
case compare x z of
EQ -> compare y w
c -> c
Not certain that's the exact code that's used, but it's close enough.
Bob
On 11 Jul 2009, at 15:06, Patrick LeBoutillier wrote:
> Hi,
>
> I see the type of the List.sort function is:
>
> Ord a => [a] -> [a]
>
> I don't understand how this type can allow it to sort pairs:
>
> Prelude> :m +List
> Prelude List> sort [(1, "1"), (3, "3"), (2, "2")]
> [(1,"1"),(2,"2"),(3,"3")]
>
> How can a pair by of type "Ord a"? Is it a rule that the first element
> is automatically used?
>
>
> Patrick
>
> --
> =====================
> Patrick LeBoutillier
> Rosemère, Québec, Canada
> _______________________________________________
> 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 13, Issue 6
****************************************