[Haskell] String permutation

Sukit Tretriluxana tretriluxana.s at gmail.com
Wed Jul 26 00:44:36 EDT 2006


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. For example, if I say,

permute "abc"

It will return the the result as

["abc","acb","bca","bac","cab","cba"]

And here is the program I came up with.

permute :: String -> [String]
permute str = rotate str len len
   where len = length str

rotate :: String -> Int -> Int -> [String]
rotate _ _ 0 = []
rotate s 1 _ = [s]
rotate (ch:chs) len rcnt =
   map (\x -> ch : x) (rotate chs (len-1) (len-1))
   ++
   rotate (chs ++ [ch]) len (rcnt-1)

I am more than certain that this simple program can be rewritten to be more
succinct and possibly more efficient using Haskell's features. So I would
like to ask if any of you would kindly show me an example.

Regards,
Ed
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell/attachments/20060725/7b9622c5/attachment.htm


More information about the Haskell mailing list