Output an element the number of times I want

前端 未结 6 1809
-上瘾入骨i
-上瘾入骨i 2021-01-21 03:11

I have this code:

lado ::  [([Char],Int)] -> [[Char]]
lado xs = [a | (a,b) <- xs]

I need to output this:

> lado [("A         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 03:35

    The transpose :: [[a]] -> [[a]] of [["A", "A", "A"], ["B", "B"], ["C"]] is:

    Prelude Data.List> transpose [["A", "A", "A"], ["B", "B"], ["C"]]
    [["A","B","C"],["A","B"],["A"]]
    

    if we then concatenate these with concat :: [[a]] -> [a], we get:

    Prelude Data.List> concat (transpose [["A", "A", "A"], ["B", "B"], ["C"]])
    ["A","B","C","A","B","A"]
    

    if we can thus make a lists of three "A"s, two "B"s, and one "C", then we can generate such list. I leave generating this list as an exercise. You can make use of replicate :: Int -> a -> [a] and use list comprehension, or map :: (a -> b) -> [a] -> [b].

提交回复
热议问题