Created
November 8, 2011 08:49
-
-
Save 23Skidoo/1347308 to your computer and use it in GitHub Desktop.
Purely functional queue in Ocaml
This file contains hidden or 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
type 'a queue = Queue of 'a list * 'a list | |
let empty = Queue ([], []);; | |
let add q elt = match q with | |
| Queue (front, back) -> Queue (elt::front, back);; | |
let take q = match q with | |
| Queue ([], []) -> raise (Invalid_argument "Empty queue!") | |
| Queue (front, b::bs) -> b, (Queue (front, bs)) | |
| Queue (front, []) -> let back = List.rev front | |
in (List.hd back, Queue ([], List.tl back));; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment