Skip to content

Instantly share code, notes, and snippets.

@23Skidoo
Created November 8, 2011 08:49
Show Gist options
  • Save 23Skidoo/1347308 to your computer and use it in GitHub Desktop.
Save 23Skidoo/1347308 to your computer and use it in GitHub Desktop.
Purely functional queue in Ocaml
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