我想計算字符串中的字符數與遞歸函數相結合,但它似乎沒有工作。在遞歸函數中計算字符串中Char的實例
{- 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)]
。
建議將不勝感激。
只是可以肯定:在[這裏](HTTPS ://hackage.haskell.org/package/tables-0.4.1.1/docs/Data-Table.html)你正在使用的'Table'類型? – duplode
我記得上一次作業問題中的'Table'。我認爲這只是一個關聯列表,也許是以前的作業分配來實現它。 – jberryman
newtype表a b = T [(a,b)] –