Problem #4

March 8, 2008 – 3:22 am

This also can be done using using one of the built functions but lets try to do this using recursion.


let rec nr_elements l =
     match l with
     | [] -> 0
     | x::xs -> 1 + nr_elements xs;;

nr_elements myList |> printf "Length: %d"

Just use forward recursion adding one to the result and then return zero at the base case (empty list).

Post a Comment