Skip to content

Instantly share code, notes, and snippets.

@egonSchiele
Last active November 12, 2015 23:25
Show Gist options
  • Save egonSchiele/6a251f56bfeb3f9be7c8 to your computer and use it in GitHub Desktop.
Save egonSchiele/6a251f56bfeb3f9be7c8 to your computer and use it in GitHub Desktop.
kcombinations
{-# LANGUAGE MultiWayIf #-}
import Data.List
import Control.Monad
import Control.Monad.Trans.Writer
kcombinations n arr = snd . runWriter $ combos [] 1 n arr
combos :: (Eq a) => [a] -> Int -> Int -> [a] -> Writer [[a]] ()
combos acc step n array = forM_ (zip array [1..]) $ \(val, i) -> if
| val `elem` acc -> return ()
| n == step -> tell [acc ++ [val]]
| otherwise -> combos (acc ++ [val]) (step + 1) n (drop i array)
main = print $ kcombinations 2 "abc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment