Skip to content

Instantly share code, notes, and snippets.

@egonSchiele egonSchiele/Main.hs
Last active Nov 12, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.