Jump to content

Letter frequency: Difference between revisions

m
→‎{{header|Haskell}}: Tidied ( Ormolu-formatted )
(Add Cowgol)
m (→‎{{header|Haskell}}: Tidied ( Ormolu-formatted ))
Line 2,207:
or, as an alternative to sorting and grouping the whole string, we could use some kind of container as the accumulator for a single fold, for example:
 
<lang haskell>import qualified Data.Map.Strict asList M(sortBy)
import qualified Data.Map.Strict as M
import Data.Ord (comparing)
import Data.List (sortBy)
 
charCounts :: String -> M.Map Char Int
Line 2,218:
 
main :: IO ()
main = do
s <- readFile "miserables.txt"
>>= mapM_ print
mapM_ print $ sortBy (flip $ comparing snd) $ M.toList (charCounts s)</lang>
. sortBy
(flip (comparing snd))
. M.toList
. charCounts</lang>
{{Out}}
<pre>(' ',516452)
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.