我正在Haskell中创建一个函数,该函数将为我提供字符串中字符的频率。
输入:
frequencies "hello world"
不良输出:
[1 :- ’ ’,1 :- ’d’,1 :- ’e’,1 :- ’h’,3 :- ’l’,2 :- ’o’,1 :- ’r’,1 :- ’w’]
我的代码:
frequencies :: (Ord char) => [char] -> [With Int char]
frequencies a = map (buildWith) (group (sort (head (tails a))))
buildWith :: [char] -> With Int char
buildWith a = With (length a) (head a)
现在,我无法使它正常工作,主要是因为找不到与“ With”数据类型有关的信息。我不知道我是否正确使用它。这是Haskell解释器返回的错误:
[3 of 3] Compiling Huffman ( C:\\Huffman.hs,interpreted )
C:\\Huffman.hs:17:16: error:
Data constructor not in scope: With :: Int -> char -> With Int char
|
17 | buildWith a = (With (length a) (head a))
| ^^^^
Failed,two modules loaded.
我很好奇我在做错什么以及如何解决。预先感谢!
编辑: 对于对With数据类型感兴趣的人:
infix 1 :-
data With a b = a :- b
deriving (Show)
satellite :: With a b -> b
satellite (_ :- b) = b
instance (Eq a) => Eq (With a b) where
(a :- _) == (b :- _) = a == b
instance (Ord a) => Ord (With a b) where
(a :- _) <= (b :- _) = a <= b