How to convert a list of (Char,Int) to a string with the given number of repeated chars?

前端 未结 4 1678
予麋鹿
予麋鹿 2020-12-22 05:23

How can I convert [(char,Int)] to a String of the Int in the second component gives the number of repetitions of the character in the first component? For examp

4条回答
  •  心在旅途
    2020-12-22 06:14

    This can be assembled from just a few functions in the Prelude. Since your input is a list of tuples, the return value becomes a list of strings.

    repChars :: (Char, Int) -> String
    repChars (c,n) = replicate n c
    
    Prelude> map repChars [('a',9),('b',10)]
    ["aaaaaaaaa","bbbbbbbbbb"]
    

    Or if you want to do it as a point-free one-liner:

    repCharList = map (uncurry (flip replicate))
    

    Is this homework? If so, please use the homework tag.

提交回复
热议问题