F# | Length of a List

The code snippets listed below defines a function to compute the length of a give list using F#. Note that these functions are also called polymorphic function, as they work with any type of list (as shown in the output).

[gist https://gist.github.com/4124078 /]

A tail recursive implementation is outlined next.

[gist]4124111[/gist]

Following is a more succinct implementation using List.Fold.

[gist https://gist.github.com/4222723 /]

Output:

> length [];; val it : int = 0 > length [1;2;3;4];; val it : int = 4 > length ['a';'b'];; val it : int = 2