可能重复:
How to convert a list of (Char,Int) to a string with the given number of repeated chars?复制函数帮助
我怎么做一个[(炭,INT)〕 - >字符串与复制不使用uncurry
例如键入['a',9],然后输出“aaaaaaaaa”
可能重复:
How to convert a list of (Char,Int) to a string with the given number of repeated chars?复制函数帮助
我怎么做一个[(炭,INT)〕 - >字符串与复制不使用uncurry
例如键入['a',9],然后输出“aaaaaaaaa”
刚刚发生了什么问题:
repli Char -> Integer -> String
repli _ 0 = ""
repli c i = c : repli c (i-1)
此功能是令行禁止的形式,但如果你想避免uncurry只是把它写在其uncurried形式:
repli (Char, Integer) -> String
repli t = if (snd t) = 0 then "" else ((fst t) : (repli ((fst t) (snd t)-1))))
列表中哈斯克尔是“型均质”:他们只能有一种类型。所以你永远不会有像[['a',9]这样的列表。
您可以有一个元组('a',9)
。你也可以有一个元组列表,例如[('a',9)]
。但是那个列表中的所有元组都必须是(Char, Integer)
。
参见:http://en.wikibooks.org/wiki/Haskell/Lists_and_tuples
但是,什么是你不使用普通的老replicate
功能动机是什么?
ghci> replicate 9 'a'
"aaaaaaaaa"
这是由同一用户btw。转贴不会让你得到任何地方。 -1 – delnan 2011-03-06 17:50:23
而且两个不同的用户也给出了正确的答案。 – 2011-03-06 18:54:59