aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--ch03/3_b_3.hs24
2 files changed, 25 insertions, 1 deletions
diff --git a/README.md b/README.md
index b264c5d..0157c7f 100644
--- a/README.md
+++ b/README.md
@@ -55,7 +55,7 @@ more visible in the list the first exercise of a group is in bold italics.
| 3_a_2 | yes | | |
| **_3_b_1_** | yes | 69 | |
| 3_b_2 | yes, in 3_b_1 | | |
-| 3_b_3 | | | |
+| 3_b_3 | yes | | |
| 3_b_4 | | | |
| 3_b_5 | | | |
| 3_b_6 | | 70 | |
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