Haskell 99 Problems – 04

Problem 4

(*) Find the number of elements of a list.

Example in Haskell:

Prelude> myLength [123, 456, 789]
3
Prelude> myLength "Hello, world!"
13

 


— 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

view raw

h04.hs

hosted with ❤ by GitHub

 

Discover more from Gaurav Sharma's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading