I am making a function in Haskell that will give me the frequencies of chars in a string.
Input:
frequencies "hello world"
Disired output:
[1 :- ’ ’,1 :- ’d’,1 :- ’e’,1 :- ’h’,3 :- ’l’,2 :- ’o’,1 :- ’r’,1 :- ’w’]
My code:
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)
Now I cannot get this to work, mainly because I cannot find the information about the "With" datatype. I do not know if I use it correctly. This is the fault the Haskell interpreter gives back:
[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.
I am curious what I am doing wrong and how I can fix it. Thanks in advance!
Edit: For the ones interested in the With datatype:
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