在Haskell中,你如何乘以字符串列表?

时间:2017-11-17 17:26:41

标签: haskell

我试图编写一个带有字符串和Int列表的函数,并返回每个字符串为" int"的字符串列表。倍。那就是:

duplicate :: [String] -> Int -> [String]
duplicate ["ab","ac","yt","hfg","lkj","poi"] 2 

输出应为

["ab","ab","ac","ac","yt","yt","hfg","hfg","lkj","lkj","poi","poi"]

1 个答案:

答案 0 :(得分:1)

duplicate = (. replicate) . (>>=)

或更基本的

duplicate xs n = concatMap (replicate n) xs

如果你想用符号表示列表乘法

> let (**) :: [a] -> Int -> [a]
|     (**) = (. replicate) . (>>=)

> ["a","b"]**3
["a","a","a","b","b","b"]