Skip to content

Instantly share code, notes, and snippets.

@gabyfle
Last active March 23, 2021 14:13
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 gabyfle/6412a0ed21c87f59c760045d7ca608d8 to your computer and use it in GitHub Desktop.
Save gabyfle/6412a0ed21c87f59c760045d7ca608d8 to your computer and use it in GitHub Desktop.
File (based on lists)
(* Empty the list l into m, reversing it *)
let rec clear l m = match l with
| [] -> m
| h :: t -> clear t (h :: m)
type 'a file = {top: 'a list; back: 'a list}
let empty () = {top= []; back = []}
let add a f = match f with
| {top = x; back = y} -> { top = x; back = a :: y }
let rec remove = function
| { top = []; back = [] } -> empty ()
| { top = []; back = l } -> remove { top = (clear l []); back = [] }
| { top = h :: t; back = l } -> { top = t; back = l }
let rec head = function
| { top = []; back = [] } -> empty ()
| { top = []; back = h :: [] } -> h
| { top = h :: []; back = [] } -> h
| { top = _; back = h :: t } -> head { top = []; back = t }
| { top = h :: t; back = [] } -> head { top = t; back = [] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment