Disturbingly Concise

import Control.Monad

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

I saw this over on a rather good introduction to dynamic programming. It does exactly what you’d expect it to do. (i.e., powerset [1,2] = [[1,2],[1],[2],[]].)

Outstanding.

Leave a Reply


This is a free Wordpress template provided by Mathew Browne | Web Design | SEO