文字列の文字数を再帰関数と組み合わせてカウントしようとしていますが、動作していないようです。再帰関数の文字列内の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
私は前の宿題の問題から 'テーブル'を覚えています。私はそれが単なる連想リストだと思っています、それを実装するための以前の宿題だったかもしれません。 – jberryman
newtypeテーブルa b = T [(a、b)] –