<div dir="ltr"><div>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.</div><div><br></div>filterM2' :: (Monad m) => (m Bool) -> [a] -> m [a]<br>
filterM2' p [] = return []<br>
filterM2' p (x:xs) =<br>
    let rest = filterM2' p xs in<br>
      do b <- p<br>
         if b then liftM (x:) rest<br><div>
              else            rest</div><div><br></div><div><br>
   filterM2' [False,True] [1,2,3]</div><div><br></div><div>As for how this works, remember that lists are Monads and their instance makes them work like list comprehensions.  Consider the following.<br></div><div><br></div><div>test2 :: [(Int,Int)]<br></div><div>test2 = do<br>  x <- [1,2]<br>  y <- [3,4]<br>  return (x,y)</div><div><br></div><div>[(1,3),(1,4),(2,3),(2,4)]</div><div><br></div><div>test3 :: [String]<br></div><div><div>test3 = do<br>  x <- [True, False, True]<br>  if x<br>    then ["x was true"]<br>    else ["x was false"]</div><div><br></div><div>["x was true","x was false", "x was true"]<br></div><div><br></div></div><div>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.<br></div><div><br></div><div>filterM2' [True,True] [1,2]</div><div>[[1,2],[1,2],[1,2],[1,2]]</div><div><br></div><div>filterM2' [True,False] [1,2]</div><div>[[1,2],[1],[2],[]]</div><div><br></div><div>filterM2' [False,True] [1,2]<br>[[],[2],[1],[1,2]]<br><br></div><div>filterM2' [False,False] [1,2]<br>[[],[],[],[]]<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jun 17, 2018 at 9:24 AM, Olumide <span dir="ltr"><<a href="mailto:50295@web.de" target="_blank">50295@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear List,<br>
<br>
I'm trying to apply the following definition of filterM (from Brent Yorgey's blog <a href="https://byorgey.wordpress.com/2007/06/26/deducing-code-from-types-filterm/" rel="noreferrer" target="_blank">https://byorgey.wordpress.com/<wbr>2007/06/26/deducing-code-from-<wbr>types-filterm/</a>)<br>
<br>
import Control.Monad  -- for liftM<br>
<br>
filterM' :: (Monad m) => (a -> m Bool) -> [a] -> m [a]<br>
filterM' p [] = return []<br>
filterM' p (x:xs) =<br>
    let rest = filterM' p xs in<br>
      do b <- p x<br>
         if b then liftM (x:) rest<br>
              else            rest<br>
<br>
in order to understand how filterM can be used to compute the power set of a list, as follows<br>
<br>
   filterM' (const [False,True]) [1,2,3]<br>
<br>
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?<br>
<br>
Regards,<br>
<br>
- Olumide<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>