haskell迭代列表推导

时间:2013-10-27 18:29:22

标签: haskell

我想在Haskell

中这样做

对于MyString = "ab"n = 4,结果应为>

["aaaa","aaab","aaba","aabb","abaa","abab","abba","abbb","baaa","baab","baba","babb","bbaa","bbab","bbba","bbbb"]

2 个答案:

答案 0 :(得分:7)

replicateM 4 "ab"

给出你要求的东西。您可以从Control.Monad

导入它

答案 1 :(得分:1)

如果你想自己写,你可以做以下事情。

seq i xs | i > 0 = [x:ys | x <- xs, ys <- seq (i-1) xs]
         | otherwise = [[]]