[Haskell-cafe] powerSet = filterM (const [True, False]) ... is
this obfuscated haskell?
lrpalmer at gmail.com
Fri Jul 17 05:45:03 EDT 2009
On Fri, Jul 17, 2009 at 1:35 AM, Thomas Hartman <tphyahoo at gmail.com> wrote:
> on haskell reddit today
> powerSet = filterM (const [True, False])
The M is the list, i.e. *nondeterminism* monad. For each element in the
list, there is one return value where it appears (True), and one where it
does not (False).
Basically, regular filter says that for each element in the list, we need to
make a choice as to whether it occurs in the result. Here we use
nondeterminism to make both choices.
> is said to be beautiful / mind blowing. I just don't get it. I can
> play with transformations until I get
> powerSet  = []
> powerSet (x:xs) =
> let pxs = powerSet xs
> in map (x:) pxs ++ pxs
> which is understandable to me, but no matter how long I look at the
> original filterM definition it just doesn't click.
> Is this a uniquely haskell obfu, or is there a way of reading this
> definition that makes sense?
> If anybody agrees with me, care to throw out other examples of
> "obfuscated haskell considered harmful"?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe