aboutsummaryrefslogtreecommitdiff
path: root/ch03/3_b_1.hs
blob: 4db9c0c7b00327747309d5106ed02793d4580f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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