Permutations by swapping: Difference between revisions

Content added Content deleted
Line 473:
 
=={{header|Haskell}}==
<lang haskell>insertEverywhere :: a -> [a] -> [[a]]
insertEverywhere x [] = [[x]]
insertEverywhere x l@(y:ys) = (x:l) : map (y:) (insertEverywhere x ys)
 
s_perm :: [a] -> [[a]]
s_perm = foldl aux [[]]
where aux items x = do (f, item) <- zip (cycle [reverse, id]) items
f (insertEverywhere x item)
 
s_permutations :: [a] -> [([a], Int)]
s_permutations = flip zip (cycle [1, -1]) . s_perm(foldl aux [[]])
where aux items x = do
where aux items x = do (f, item) <- zip (cycle [reverse, id]) items
f (insertEverywhereinsertEv x item)
insertEverywhere insertEv x [] = [[x]]
insertEverywhere insertEv x l@(y:ys) = (x:l) : map (y:) (insertEverywhere$ insertEv x ys)
 
main :: IO ()