I am trying to count the number of characters in a string coupled with a recursive function but it does not seem to be working.
{- characterCounts s
PRE: True
POST: a table that maps each character that occurs in s to the number of
times the character occurs in s
EXAMPLES:
-}
characterCounts :: String -> Table Char Int
characterCounts [] = Table.empty
characterCounts s = characterCountsAux s Table.empty
characterCountsAux:: String -> Table Char Int -> Table Char Int
characterCountsAux [] table = table
characterCountsAux (x:xs) table = characterCountsAux xs (Table.insert (table) x (count x (x:xs) ))
count:: Char -> String -> Int
count c s = length $ filter (==c) s
so in case I do: characterCounts "atraa" I should get T [('a',3),('t',1),('r',1)] but instead I get T [('a',1),('t',1),('r',1)].
Suggestions will be appreciated.
Tabletype you are using?Tablefrom a previous homework problem posted. I think it's just an association list, maybe a previous homework assignment to implement it.