Skip to content

Instantly share code, notes, and snippets.

@klapaucius
Created October 27, 2011 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klapaucius/1319152 to your computer and use it in GitHub Desktop.
Save klapaucius/1319152 to your computer and use it in GitHub Desktop.
map in ocaml
(* batList.ml *)
type 'a mut_list = {
hd: 'a;
mutable tl: 'a list
}
external inj : 'a mut_list -> 'a list = "%identity"
let map f = function
| [] -> []
| h :: t ->
let rec loop dst = function
| [] -> ()
| h :: t ->
let r = { hd = f h; tl = [] } in
dst.tl <- inj r;
loop r t
in
let r = { hd = f h; tl = [] } in
loop r t;
inj r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment