0hc.net Menu
Home Overview
About Menu
Impressum

Permutations

Perm1

perm x = sequence . replicate x main = print $ perm 2 "123" ["11","12","13","21","22","23","31","32","33"]

Perm2

perm 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"]

Perm3

perm 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"]

Perm4

import Data.List perm [] = [[]] perm (x:xs) = [i++[x]++t|ys<-perm xs, (i,t)<-zip (inits ys) (tails ys)] main = print $ perm "123" ["123","213","231","132","312","321"]

Perm5

import Data.List perm n xs = p (n-2) $ f ([],xs) where p n xs | n<0 = map fst xs | otherwise = p (n-1) $ 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"]

Perm6

perm m n = f (m-1) 0 [] where f l s xs | l>0 = [0..n-s] >>= \x-> f (l-1) (s+x) $ x:xs | otherwise = return $ n-s:xs main = print $ perm 3 2 [[2,0,0],[1,1,0],[0,2,0],[1,0,1],[0,1,1],[0,0,2]]
0hc.net    © 2001-2014 Harald Wolfsgruber