Haskell道 その1
Haskellはforやwhileのような繰り返し構文がないんですね。なかなか戸惑いますが慣れでしょうか。
早速、コンビネーションを得る関数を作ってみた。
module Main (main) where combination :: Integer -> [Integer] -> [[Integer]] combination n elems | n <= 0 || length elems == 0 = [] combination 1 elems = map (\e -> [e]) elems combination n (e:elems) = current ++ others where current = map (e:) (combination (n-1) elems) others = combination n elems main :: IO () main = do print (combination 3 [1,2,3,4,5])
出力
*Main> main [[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]]