Anonymous user
Permutations: Difference between revisions
→{{header|Haskell}}
m (→{{header|REXX}}: corrected a misspelling. -- ~~~~) |
|||
Line 1,296:
main = mapM_ print (permutations [1,2,3])</lang>
A simple implementation, that assumes elements are unique and support equality:
<lang haskell>import Data.List (delete)
permutations :: Eq a -> [a] -> [[a]]
permutations [] = [[]]
permutations xs = [ x:ys | x <- xs, ys <- permutations (delete x xs)]</lang>
A slightly more efficient implementation that doesn't have the above restrictions:
<lang haskell>permutations :: [a] -> [[a]]
permutations [] = [[]]
permutations
where select [] = []
select (x:xs) = (x,xs) : [ (y,x:ys) | (y,ys) <- select xs ]</lang>
=={{header|Icon}} and {{header|Unicon}}==
|