Der Blog

One-line definition of the power set function

If you thought the one-line definition of the Fibonacci sequence in Haskell was beautiful, try this:

import Control.Monad

powerset :: [a] -> [[a]]
powerset = filterM (const [True, False])

A black box for sure, unless you have "internalized the list monad". But it does show how stonkingly powerful monads are.

[ Entry posted at: Mon 02 Jul 2007 18:45:21 BST | 0 comment(s)... | Cat: Geeky ]

Add Comment

Validate : XHTML / CSS / RSS / ATOM