Why doesn't this definition cover all pattern cases?

后端 未结 3 1028
再見小時候
再見小時候 2021-01-13 09:39

So I\'m trying to triplize an element, i.e. making 2 other copies of the element.

So I\'ve written this:

triplize :: [a] -> [a]
tripl         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 10:24

    [x] matches a list of exactly one element. If you want to match any list, just remove [ and ]:

    triplize x = concatMap (replicate 3) x
    

    This would turn ["a", "b", "c"] into ["a", "a", "a", "b", "b", "b", "c", "c", "c"].

    On the other hand, if you want to take one element and return a list (containing 3 of that element), then your code should be

    triplize :: a -> [a]
    triplize x = replicate 3 x
    

提交回复
热议问题