Skip to content

Instantly share code, notes, and snippets.

@AndrasKovacs
Created January 29, 2014 11:25
Show Gist options
  • Save AndrasKovacs/8686107 to your computer and use it in GitHub Desktop.
Save AndrasKovacs/8686107 to your computer and use it in GitHub Desktop.
Combinatorics.
module Combinatorics (powerset, combsOf, perms) where
powerset :: [a] -> [[a]]
powerset (x:xs) = let xs' = powerset xs in xs' ++ map (x:) xs'
powerset [] = [[]]
combsOf :: Int -> [a] -> [[a]]
combsOf n _ | n < 1 = [[]]
combsOf n (x:xs) = combsOf n xs ++ map (x:) (combsOf (n - 1) xs)
combsOf _ _ = []
perms :: [a] -> [[a]]
perms [] = []
perms xs = foldr ((=<<) . ins) [[]] xs where
ins y (x:xs) = (y:x:xs) : map (x:) (ins y xs)
ins y _ = [[y]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment