RosettaCodeData/Task/Letter-frequency/Koka/letter-frequency.koka
2025-08-11 18:05:26 -07:00

20 lines
538 B
Text

import std/os/file
import std/os/path
fun main()
val counts = count(read-text-file("input.txt".path).list)
counts.foreach fn((c, i))
println(c.show-char ++ " = " ++ i.show)
println("Total letters = " ++ counts.map(snd).sum.show)
fun add(freq: list<(char, int)>, c : char): list<(char, int)>
if c.is-alpha then
match freq
Nil -> [(c, 1)]
Cons((x, n), rest) ->
if x == c then Cons((x, n + 1), rest)
else Cons((x, n), add(rest, c))
else freq
fun count(text: list<char>)
text.foldl([], add)