Permutations: Difference between revisions
→{{header|Haskell}}: Slight reformulation of the Mathematica-based version, in terms of the applicative <*> operator
(→{{header|Python}}: with destructive list updates) |
(→{{header|Haskell}}: Slight reformulation of the Mathematica-based version, in terms of the applicative <*> operator) |
||
Line 2,939:
insertEverywhere x l@(y:ys) = (x:l) : map (y:) (insertEverywhere x ys)</lang>
A serialized version:
{{Trans|Mathematica}}
<lang haskell>import qualified Data.List as L
ins ::
ins x list n = L.intercalate [x] $ [snd, fst] <*> [L.splitAt n list]
permutations :: [a] -> [[a]]
permutations
foldl (\ac x -> ac >>= (fmap . ins x) <*> (enumFromTo 0 . length)) [[]]
main :: IO ()
main = print $ permutations [1, 2, 3]</lang>
{{Out}}
<pre>[[1,2,3],[2,3,1],[3,1,2],[2,1,3],[1,3,2],[3,2,1]]</pre>
=={{header|Icon}} and {{header|Unicon}}==
|