Output an element the number of times I want

前端 未结 6 1864
-上瘾入骨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:41

    You can use recursion to accomplish this:

    lado :: [(a, Int)] -> [a]
    -- base case
    lado [] = []
    -- take each of the non-zero elements then recurse
    lado xs = map fst nonzero ++ lado subtracted
        where
            -- find elements with non-zero count
            nonzero = filter (\x -> snd x > 0) xs
            -- subtract one from the count for each of those elements
            subtracted = map (\(x, n) -> (x, n - 1)) nonzero
    

提交回复
热议问题