Problem 4
(*) Find the number of elements of a list.
Example in Haskell:
Prelude> myLength [123, 456, 789] 3 Prelude> myLength "Hello, world!" 13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| — problem 4 | |
| — the number of elements in a list or the length function | |
| — define it recursively | |
| — length of a list of one element is 1 | |
| — otherwise the length is 1 + the length of cdr of the list | |
| myLength :: [a] -> Integer | |
| myLength [] = 0 | |
| myLength [x] = 1 | |
| myLength (x:xs) = 1 + myLength xs | |