From b261f475cdec2b4646f1cf6a1c58cbd174f3500f Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Fri, 10 Mar 2023 17:04:23 +0100 Subject: 3_b_1: Add solution --- ch03/3_b_1.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch03/3_b_1.hs (limited to 'ch03/3_b_1.hs') 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 -- cgit v1.2.3