Skip to content

Instantly share code, notes, and snippets.

@LOZORD
Created September 4, 2015 04:24
Show Gist options
  • Select an option

  • Save LOZORD/94da315953fb98fc9ba8 to your computer and use it in GitHub Desktop.

Select an option

Save LOZORD/94da315953fb98fc9ba8 to your computer and use it in GitHub Desktop.
Powerset implementation in Haskell
import qualified Data.Set as Set
phi :: Set.Set a
phi = Set.empty
pset :: (Ord a) => Set.Set a -> Set.Set (Set.Set a)
pset set = if (Set.null set)
then Set.singleton phi
else
let head = Set.elemAt 0 set
headSet = Set.singleton head
headUnion = \lilsubset -> lilsubset `Set.union` headSet
rest = Set.deleteAt 0 set
subsets = pset rest -- recursive call
in subsets `Set.union` (Set.map headUnion subsets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment