[Haskell-beginners] Powerset function

Alexey Shmalko rasen.dubi at gmail.com
Tue Apr 21 15:23:59 UTC 2015


>From base-4.6.0.1 [1] the filterM is implemented like this:

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)

So
filterM (\x -> [True, False]) [1,2] is

filterM (\x -> [True, False]) (1:[2])
do
    flg <- (\x -> [True, False]) 1
    ys <- filterM (\x -> [True, False]) [2]
    return (if flg then x:ys else ys)

do
    flg <- [True, False]
    ys <- [[2], []]
    return (if flg then 1:ys else ys)

You get the idea. You'll end up with [[1,2],[1],[2],[]]

Hope this helps,
Alexey Shmalko

[1]
https://hackage.haskell.org/package/base-4.6.0.1/docs/src/Control-Monad.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150421/94477e38/attachment.html>


More information about the Beginners mailing list