RosettaCodeData/Task/Permutations/Haskell/permutations-4.hs

6 lines
249 B
Haskell
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
permutations :: [a] -> [[a]]
permutations = foldr (concatMap . insertEverywhere) [[]]
where insertEverywhere :: a -> [a] -> [[a]]
insertEverywhere x [] = [[x]]
insertEverywhere x l@(y:ys) = (x:l) : map (y:) (insertEverywhere x ys)