Problem 04

99 Haskell Problems

Problem 4

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

Example in Haskell:

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

Solution:

myLength x = length x

or

myLength [] = 0
myLength (_:xs) = 1 + myLength xs
Advertisements