
Permutations
Perm1perm x = sequence . replicate x
main = print $ perm 2 "123"
["11","12","13","21","22","23","31","32","33"]
Perm2perm xs = []:[x:ps(x,ys)<f xs, ps<perm ys] where
f [] = []
f (x:xs) = (x,xs):f xs
main = print $ perm "123"
["","1","12","123","13","2","23","3"]
Perm3perm xs = []:[x:ps(x,ys)<f xs, ps<perm ys] where
f [] = []
f (x:xs) = (x,xs):[(y,x:ys)(y,ys)<f xs]
main = print $ perm "123"
["","1","12","123","13","132","2","21","213","23","231","3","31","312","32","321"]
Perm4import Data.List
perm [] = [[]]
perm (x:xs) = [i++[x]++tys<perm xs, (i,t)<zip (inits ys) (tails ys)]
main = print $ perm "123"
["123","213","231","132","312","321"]
Perm5import Data.List
perm n xs = p (n2) $ f ([],xs) where
p n xs
 n<0 = map fst xs
 otherwise = p (n1) $ concat $ map f xs
f (xs,[]) = []
f (xs,ys) = map (\(a:as)>(a:xs,as)) $ init $ tails ys
main = print $ perm 2 "1234"
["21","31","41","32","42","43"]
Perm6perm m n = f (m1) 0 [] where
f l s xs  l>0 = [0..ns] >>= \x> f (l1) (s+x) $ x:xs
 otherwise = return $ ns:xs
main = print $ perm 3 2
[[2,0,0],[1,1,0],[0,2,0],[1,0,1],[0,1,1],[0,0,2]]

