0

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
N. Jans.
  • 1
  • 1
  • 2
    Where are you getting `With` from? That's not a standard type constructor. – chepner Nov 06 '19 at 18:36
  • 1
    The data constructor of `With` is, based on your sample output `:-`, so it should be `buildWith a = length a :- head a`. – Willem Van Onsem Nov 06 '19 at 18:36
  • 1
    Ha..! interesting... so infix data constructors ara allowed if they start with a colon... I learned this today. https://stackoverflow.com/q/30039123/4543207 – Redu Nov 06 '19 at 18:41
  • @WillemVanOnsem This was it, thanks for your help! Shame I did not think about this... – N. Jans. Nov 06 '19 at 18:43
  • @chepner My bad, I found it hidden inside an imported file, I thought is was a standard type. – N. Jans. Nov 06 '19 at 18:43

0 Answers0