Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Difficulty understanding how to use filterM to compute powerset (Olumide) 2. Re: Difficulty understanding how to use filterM to compute powerset (David McBride) ---------------------------------------------------------------------- Message: 1 Date: Sun, 17 Jun 2018 14:24:56 +0100 From: Olumide <50...@web.de> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Difficulty understanding how to use filterM to compute powerset Message-ID: <41001987-f04c-6b2c-e291-4f3f021aa...@web.de> Content-Type: text/plain; charset=utf-8; format=flowed Dear List, I'm trying to apply the following definition of filterM (from Brent Yorgey's blog https://byorgey.wordpress.com/2007/06/26/deducing-code-from-types-filterm/) import Control.Monad -- for liftM filterM' :: (Monad m) => (a -> m Bool) -> [a] -> m [a] filterM' p [] = return [] filterM' p (x:xs) = let rest = filterM' p xs in do b <- p x if b then liftM (x:) rest else rest in order to understand how filterM can be used to compute the power set of a list, as follows filterM' (const [False,True]) [1,2,3] Where p in the filterM' is (const [False,True]). What confuses me is that p x in filterM'. Based on my very limited understanding p x returns b = [False, True]. How b be tested in the subsequent if-statement if it is indeed a list? What am I getting wrong? Regards, - Olumide ------------------------------ Message: 2 Date: Sun, 17 Jun 2018 15:02:47 -0400 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Difficulty understanding how to use filterM to compute powerset Message-ID: <can+tr40y9xr8r7czny--jvtfzs5b2156ee6m1e5ztb2wuys...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" You are right, that the predicate in this case the argument is ignored. In fact you can rewrite it and it would work the same. filterM2' :: (Monad m) => (m Bool) -> [a] -> m [a] filterM2' p [] = return [] filterM2' p (x:xs) = let rest = filterM2' p xs in do b <- p if b then liftM (x:) rest else rest filterM2' [False,True] [1,2,3] As for how this works, remember that lists are Monads and their instance makes them work like list comprehensions. Consider the following. test2 :: [(Int,Int)] test2 = do x <- [1,2] y <- [3,4] return (x,y) [(1,3),(1,4),(2,3),(2,4)] test3 :: [String] test3 = do x <- [True, False, True] if x then ["x was true"] else ["x was false"] ["x was true","x was false", "x was true"] So the List Monad instance pairs each element with every other element, returning a list of every case. In filterM's case, it checks each x and based on that does something slightly different. In fact what it is doing is giving you two cases, one where x is there and one where it isn't, then running the same function on the remaining elements once for each of those two cases to fill in the remaining cases, appending all the results together. In my opinion it is easier to understand by just playing with it. filterM2' [True,True] [1,2] [[1,2],[1,2],[1,2],[1,2]] filterM2' [True,False] [1,2] [[1,2],[1],[2],[]] filterM2' [False,True] [1,2] [[],[2],[1],[1,2]] filterM2' [False,False] [1,2] [[],[],[],[]] On Sun, Jun 17, 2018 at 9:24 AM, Olumide <50...@web.de> wrote: > Dear List, > > I'm trying to apply the following definition of filterM (from Brent > Yorgey's blog https://byorgey.wordpress.com/2007/06/26/deducing-code-from- > types-filterm/) > > import Control.Monad -- for liftM > > filterM' :: (Monad m) => (a -> m Bool) -> [a] -> m [a] > filterM' p [] = return [] > filterM' p (x:xs) = > let rest = filterM' p xs in > do b <- p x > if b then liftM (x:) rest > else rest > > in order to understand how filterM can be used to compute the power set of > a list, as follows > > filterM' (const [False,True]) [1,2,3] > > Where p in the filterM' is (const [False,True]). What confuses me is that > p x in filterM'. Based on my very limited understanding p x returns b = > [False, True]. How b be tested in the subsequent if-statement if it is > indeed a list? What am I getting wrong? > > Regards, > > - Olumide > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180617/c505551a/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 120, Issue 9 *****************************************