LoginSignup
1
1

More than 5 years have passed since last update.

組み合わせ(Haskell)

Last updated at Posted at 2013-04-10

Eqのインスタンスでないといけないのが微妙。

comb.hs
module Comb wherecomb :: Eq a => Int -> [a] -> [[a]]comb n xs = comb' n [([], xs)] where comb' :: Eq a => Int -> [([a],[a])] -> [[a]] comb' 0 tuples = map fst tuples comb' count tuples = comb' (count-1) $ flatten $ map shift tuples   where  shift :: Eq a => ([a], [a]) -> [([a], [a])]  shift tuple =   let (acc, rest) = tuple   in [ (x:acc, filtered) |  x <- rest, let filtered = filter (/=x) rest ]flatten :: [[a]] -> [a]flatten xs = foldl (++) [] xs```
```bash
*Comb> comb 3 "abcd"
["cba","dba","bca","dca","bda","cda","cab","dab","acb","dcb","adb","cdb","bac","dac","abc","dbc","adc","bdc","bad","cad","abd","cbd","acd","bcd"]
1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1