Last active
December 10, 2017 14:43
-
-
Save vincentdchan/05e29867eed511b111d9cf0cf996846c to your computer and use it in GitHub Desktop.
给大佬感受一下OCaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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