count :: Eq a => a -> [a] -> Int
count n [] = 0
count n (x:xs) | n == x = 1 + count n xs
| otherwise = count n xs
rmdups :: Eq a => [a] -> [a]
rmdups [ ] = [ ]
rmdups (x:xs) = x : rmdups (filter(/= x) xs)
使用这两个函数,需要创建第三个函数,称为频率:它应该计算列表中每个不同值在该列表中出现的次数。例如:频率“ababc”,应返回 [(3,'a'),(2,'b'),(1,'c')]。频率布局为:
frequency :: Eq a => [a] -> [(Int, a)]
Ps rmdups,从列表中删除重复项,因此 rmdups "aaabc" = abc 和 count 2 [1,2,2,2,3] = 3。
到目前为止,我有:
frequency :: Eq a => [a] -> [(Int, a)]
frequency [] = []
frequency (x:xs) = (count x:xs, x) : frequency (rmdups xs)
但这部分存在,(错误)。谢谢