字符串的排列,Haskell

时间:2014-12-16 11:28:43

标签: haskell permutation

我有一个给定的字符串:abcdpqrs,其中输出为:badcqpsr。

我目前的代码:

f :: [a] -> [a]
f (a:b:xs) = b:a:xs
f xs = xs

评估f "abcdpqrs"结果为"bacdpqrs"。如何用它来获得" badcqpsr"?

1 个答案:

答案 0 :(得分:2)

通过递归列表的其余部分,尝试处理的不仅仅是前两个字符:

f :: [a] -> [a]
f (a:b:xs) = b:a:f xs
f xs = xs