(F Sharp) An Implementation Of The `Map’ Function In F Sharp (.NET)

Here is how map may be implemented in F Sharp. (At the very least, it is my attempt at implementing it!)

You can run this in the interactive shell.

#light;;

let rec my_map fn xs =
    match xs with
    | first::rest -> (fn first) :: (my_map fn rest)
    | [] -> []
;;

my_map (fun x -> x + 1) [1; 2; 3; 4; 5;];;
val it : int list = [2; 3; 4; 5; 6]

Leave a Reply

Your email address will not be published. Required fields are marked *