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. list monad and filterM ([email protected])
2. Re: list monad and filterM (Brent Yorgey)
3. Knights exercies in <Learn your good haskell> (Haisheng Wu)
----------------------------------------------------------------------
Message: 1
Date: Fri, 06 Apr 2012 16:58:02 +0200
From: [email protected]
Subject: [Haskell-beginners] list monad and filterM
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
I don't get this example ( from Learn you a Haskell
http://learnyouahaskell.com/for-a-few-monads-more ). The question is about
filterM and the List Monad
> import Control.Monad ( filterM )
> powerset :: [a] -> [[a]]
> powerset xs = filterM (\x -> [True, False]) xs
Looking into Control.Monad, filterM is defined:
filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
filterM _ [] = return []
filterM p (x:xs) = do
flg <- p x
ys <- filterM p xs
return (if flg then x:ys else ys)
Ok, let's do it step by step.
> test = powerset [1,2,3]
'p' (in the filterM function) is
p = \x -> [True, False]
so flg is [True, False]
Let's not calculate ys for a second, but go directly to the return function.
return (if [True, False] then x:ys else ys)
I expect the above code to throw a "Couldn't match expected type `Bool' with
actual type `[Bool]'"
So I can't see how the trick works! Any hint?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120406/d9505ce3/attachment.html>
------------------------------
Message: 2
Date: Fri, 6 Apr 2012 12:29:27 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] list monad and filterM
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Apr 06, 2012 at 04:58:02PM +0200, [email protected] wrote:
> I don't get this example ( from Learn you a Haskell
> http://learnyouahaskell.com/for-a-few-monads-more ). The question is about
> filterM and the List Monad
>
> > import Control.Monad ( filterM )
> > powerset :: [a] -> [[a]]
> > powerset xs = filterM (\x -> [True, False]) xs
>
> Looking into Control.Monad, filterM is defined:
>
> filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
> filterM _ [] = return []
> filterM p (x:xs) = do
> flg <- p x
> ys <- filterM p xs
> return (if flg then x:ys else ys)
>
> Ok, let's do it step by step.
>
> > test = powerset [1,2,3]
>
> 'p' (in the filterM function) is
>
> p = \x -> [True, False]
>
> so flg is [True, False]
No, flg is not [True, False]. <- is not assignment. It gets
translated into a call to (>>=) (bind). Given the way (>>=) works for
the list monad, flg will be first True, and then False.
-Brent
------------------------------
Message: 3
Date: Sat, 7 Apr 2012 00:53:27 +0800
From: Haisheng Wu <[email protected]>
Subject: [Haskell-beginners] Knights exercies in <Learn your good
haskell>
To: Haskell Beginer <[email protected]>
Message-ID:
<cafj8lze2yuq6xqjxigczwzxep6q3iso+wrxvrlcy1qufg4e...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi there,
There is one trivial exercise in chapter 12 <List Monad> saying
1. As an exercise, you can change this function
so that when you can reach one position from the other,
it tells you which moves to take.
2. how to modify this function so that
we also pass it the number of moves to take instead of
that number being hard-coded.
I have one answer (find the URL below) but I'm not happy with it.
https://github.com/freizl/dive-into-haskell/blob/master/monad/knight-queue.hs
Do you have better ideas?
thanks.
-Haisheng
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 46, Issue 8
****************************************