F# Scribbles

My experiments with F#

Reverse a list elements with plain code

How to reverse a list elements

1
2
3
4
5
6
let reverse list =
        let rec rev rlist list =
            hmatch list with
            | [] -> rlist
            | h :: t -> rev (h :: rlist) t
        rev [] list

The “rev” is the inner function to recursively get the head of given list and prepend to the result.

1
2
let l = [1; 3; 5];;
reverse l;;

results [5; 3; 1]