[Haskell] String permutation

Martin Percossi haskell at martinpercossi.com
Wed Jul 26 04:53:29 EDT 2006


Sukit Tretriluxana wrote:
> Dear expert Haskellers,
> 
> I am a newbie to Haskell and try to write several algorithms with it. 
> One of them is the string permutation which generates all possible 
> permutations using the characters in the string. 

While I could hardly be called an expert haskeller, here's my version:

permute        :: [a] -> [[a]]
permute []      = [[]]
permute list    = concat $ map (\(x:xs) -> map (x:) (permute xs)) (take 
(length list) (unfoldr (\x -> Just (x, tail x ++ [head x])) list))

HTH
Martin


More information about the Haskell mailing list