mirror of
https://github.com/Noratrieb/idris-learning.git
synced 2026-01-14 21:15:02 +01:00
17 lines
502 B
Idris
17 lines
502 B
Idris
module Average
|
|
|
|
import Data.String
|
|
|
|
|
|
||| Calculate the average word length of a string
|
|
||| @str a string containing words seperated by whitespace
|
|
export
|
|
average : (str : String) -> Double
|
|
average str = let totalLength = sum (wordLengths str)
|
|
wordAmount = wordCount str in
|
|
cast totalLength / cast wordAmount
|
|
where
|
|
wordCount : String -> Nat
|
|
wordCount str = length (words str)
|
|
wordLengths : String -> List Nat
|
|
wordLengths str = map length (words str)
|