Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/libraries/fps/tests/letter_frequency.hs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


{-
Counts the number of times each alphabetic character occurs in a dictionary.

Useful for benchmarking filter, map, sort and group.
-}
import Data.Char (isAlpha, toLower)
import Data.List (sortBy)
import qualified Data.ByteString.Char8 as P
import qualified Data.ByteString.Base  as P

main =
    mapM (\(a, b) -> putStrLn ([a] ++ ": " ++ show b))
    . sortBy (\a b -> snd b `compare` snd a)
    . map (\x -> (P.w2c . P.unsafeHead  $ x, P.length x))
    . P.group
    . P.sort
    . P.map toLower
    . P.filter isAlpha
    =<< P.getContents

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.