Skip to content

Instantly share code, notes, and snippets.

@vincentdchan
Last active December 10, 2017 14:43
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 vincentdchan/05e29867eed511b111d9cf0cf996846c to your computer and use it in GitHub Desktop.
Save vincentdchan/05e29867eed511b111d9cf0cf996846c to your computer and use it in GitHub Desktop.
给大佬感受一下OCaml
(* module stack *)
(* 用OCaml的module system 写一个类似C的Stack *)
module type Element = sig
type t
end
module Stack (Ele : Element)
= struct
type elementType = Ele.t
type t = elementType list ref
let create () = ref []
let push (tt:t) elem =
let _ = tt := elem::!tt
in ()
let pop (tt:t) =
match !tt with
| first::rest ->
let () =
tt := rest
in
first
| _ -> failwith "empty"
end
module MyStack = Stack (struct
type t = int
end)
let () =
let stack = MyStack.create () in
MyStack.push stack 1;
MyStack.push stack 2;
Printf.printf "%d\n" (MyStack.pop stack);
Printf.printf "%d\n" (MyStack.pop stack);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment