<span style='font-family:Verdana'><span style='font-size:12px'>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<br />
<br />
> import Control.Monad ( filterM )<br />
> powerset :: [a] -> [[a]] <br />
> powerset xs = filterM (\x -> [True, False]) xs <br />
<br />
Looking into Control.Monad, filterM is defined:<br />
<br />
filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]<br />
filterM _ [] = return []<br />
filterM p (x:xs) = do<br />
flg <- p x<br />
ys <- filterM p xs<br />
return (if flg then x:ys else ys)<br />
<br />
Ok, let's do it step by step.<br />
<br />
> test = powerset [1,2,3]<br />
<br />
'p' (in the filterM function) is<br />
<br />
p = \x -> [True, False]<br />
<br />
so flg is [True, False]<br />
<br />
Let's not calculate ys for a second, but go directly to the return function.<br />
<br />
return (if [True, False] then x:ys else ys)<br />
<br />
I expect the above code to throw a "Couldn't match expected type `Bool' with actual type `[Bool]'"<br />
<br />
So I can't see how the trick works! Any hint?<br />
</span></span>