diff options
| author | Jan Sucan <jan@jansucan.com> | 2023-03-11 13:35:05 +0100 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2023-03-11 13:35:05 +0100 |
| commit | 05ff6d55f6d6b0c8691b737dea2bc160b04178e2 (patch) | |
| tree | 9f9dc443807554e3fe55f7bb0f6bd6620aa5067e /ch03 | |
| parent | a0f3fe36437c53cafa240ca99e6dd3b67e8f71fb (diff) | |
3_b_3: Add solution
Diffstat (limited to 'ch03')
| -rw-r--r-- | ch03/3_b_3.hs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/ch03/3_b_3.hs b/ch03/3_b_3.hs new file mode 100644 index 0000000..c0d62d5 --- /dev/null +++ b/ch03/3_b_3.hs @@ -0,0 +1,24 @@ +-- Write a function that computes the mean of a list, i.e. the sum of all +-- elements in the list divided by its length. (You may need to use the +-- fromIntegral function to convert the length of the list from an integer into +-- a floating point number.) + +sumOfList [] = 0 +sumOfList (x:xs) = x + (sumOfList xs) + +meanOfList [] = 0 +meanOfList xs = sum / (fromIntegral len) + where sum = sumOfList xs + len = length xs + +-- ghci> :l 3_b_3.hs +-- [1 of 1] Compiling Main ( 3_b_3.hs, interpreted ) +-- Ok, one module loaded. +-- ghci> meanOfList [] +-- 0.0 +-- ghci> meanOfList [1] +-- 1.0 +-- ghci> meanOfList [1, 3] +-- 2.0 +-- ghci> meanOfList [2.3, 4.5, 6.9] +-- 4.566666666666666 |
