aboutsummaryrefslogtreecommitdiff
path: root/ch03/3_b_1.hs
blob: 564b6be1c1ae54e35905734d2f363a31548f225f (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
26
27
28
29
-- 1. 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.
--
-- 2. Add a type signature for your function to your source file. To test it,
--    load the source file into ghci again.

myLength :: [a] -> Int
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