haskell 如何将这个列表解析函数重写为使用map的函数?

qhhrdooz  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(132)

我有这个函数,它只计算大写字母,并返回每个字母的计数,例如:输入"ABCd"得到:【一、一、一、二、三、四、五、六、七、八、八、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九、九
我用列表解析实现了这个函数,但是任务是用一个map函数来写它,我该怎么做呢?

counter xs = [count x xs |x<- ['A'..'Z']]
  where count x [] = 0
        count a (x:xs)
            |a == x = 1 + count a xs
            |otherwise = count a xs
yzckvree

yzckvree1#

正如注解中所暗示的,map需要一个函数来Map到列表中的每个元素,您的工作只是设计该函数。

counter xs = map (\ch -> count ch xs) ['A'..'Z']
  where
    count _ [] = ...
    count ch (y:ys)
      | ch == y = ...
      | otherwise = ...

相关问题