aboutsummaryrefslogtreecommitdiff
path: root/ch03
diff options
context:
space:
mode:
Diffstat (limited to 'ch03')
-rw-r--r--ch03/3_b_1.hs25
1 files changed, 25 insertions, 0 deletions
diff --git a/ch03/3_b_1.hs b/ch03/3_b_1.hs
new file mode 100644
index 0000000..4db9c0c
--- /dev/null
+++ b/ch03/3_b_1.hs
@@ -0,0 +1,25 @@
+-- Write a function that computes the number of elements in a list. To test it,
+-- ensure that it gives the same answers as the standard length function.
+
+myLength (x:xs) = 1 + myLength xs
+myLength [] = 0
+
+-- ghci> :l 3_b_1.hs
+-- [1 of 1] Compiling Main ( 3_b_1.hs, interpreted )
+-- Ok, one module loaded.
+-- ghci> myLength []
+-- 0
+-- ghci> myLength [1]
+-- 1
+-- ghci> myLength [1, 2]
+-- 2
+-- ghci> myLength [1, 2, 3]
+-- 3
+-- ghci> length []
+-- 0
+-- ghci> length [1]
+-- 1
+-- ghci> length [1, 2]
+-- 2
+-- ghci> length [1, 2, 3]
+-- 3