复制功能帮助

时间:2011-03-06 17:03:26

标签: haskell

  

可能重复:
  How to convert a list of (Char,Int) to a string with the given number of repeated chars?

我该如何做[(char,int)] - >复制的字符串不使用uncurry

例如输入['a',9]并输出“aaaaaaaaa”

2 个答案:

答案 0 :(得分:0)

只是有什么问题:

repli Char -> Integer -> String
repli _ 0 = ""
repli c i = c : repli  c (i-1)

此功能采用咖喱形式,但如果您想避免不合理,只需将其写成未经处理的形式:

repli (Char, Integer) -> String
repli t = if (snd t) = 0 then "" else ((fst t) : (repli ((fst t) (snd t)-1))))

答案 1 :(得分:0)

Haskell中的列表是“类型同质的”:它们只能包含一种类型。所以你不可能有像'['a',9]这样的清单。

可以有一个元组('a',9)。您还可以拥有元组列表,例如[('a',9)]。但是,该列表中的所有元组必须是(Char, Integer)类型。

另请参阅:http://en.wikibooks.org/wiki/Haskell/Lists_and_tuples

但是你不使用普通的replicate函数的动机是什么?

ghci> replicate 9 'a'
"aaaaaaaaa"