[Haskell-beginners] Difficulty understanding how to use filterM to compute powerset
Olumide
50295 at web.de
Sun Jun 17 13:24:56 UTC 2018
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
More information about the Beginners
mailing list