diff options
| author | Jan Sucan <jan@jansucan.com> | 2023-03-10 17:04:23 +0100 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2023-03-10 17:04:23 +0100 |
| commit | b261f475cdec2b4646f1cf6a1c58cbd174f3500f (patch) | |
| tree | c0e486b75072f4c381e3ab8af84a066242da87f3 | |
| parent | dc15d5bd73107b5578b8f26c70bba1339041be0a (diff) | |
3_b_1: Add solution
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | ch03/3_b_1.hs | 25 |
2 files changed, 26 insertions, 1 deletions
@@ -53,7 +53,7 @@ more visible in the list the first exercise of a group is in bold italics. | 2_b_3 | yes | | | | **_3_a_1_** | yes | 60 | 3. Defining types, streamlining functions | | 3_a_2 | yes | | | -| **_3_b_1_** | | 69 | | +| **_3_b_1_** | yes | 69 | | | 3_b_2 | | | | | 3_b_3 | | | | | 3_b_4 | | | | 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 |
