今天上算法设计与分析课,第一节,里面看到了求集合幂集与集合全排列的问题,觉得用Haskell应该很容易实现,就写了这两个程序
setPower :: [a] -> [[a]]
setPower xs = powerSet xs [[]]
powerofSet :: [a] -> [[a]] -> [[a]]
powerofSet [] ys = ys
powerofSet (x:xs) ys = powerSet xs (ys ++ map ((:) x) ys)
arrange :: (Ord a) => [a] -> [[a]]
arrange [] = [[]]
arrange xs = [ x | y <- (map (/x -> map ((:) x) $ arrange (xs // [x])) xs), x <- y]