【发布时间】:2017-02-08 07:27:28
【问题描述】:
我正在尝试用递归函数计算字符串中的字符数,但它似乎不起作用。
{- 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
如果我这样做:characterCounts "atraa" 我应该得到T [('a',3),('t',1),('r',1)],但我得到的是T [('a',1),('t',1),('r',1)]。
我们将不胜感激。
【问题讨论】:
-
只是为了确定:this 是您使用的
Table类型吗? -
我记得
Table来自之前发布的作业问题。我认为这只是一个关联列表,可能是以前的家庭作业来实现它。 -
新类型表 a b = T [(a,b)]