Last active
March 23, 2021 14:13
-
-
Save gabyfle/6412a0ed21c87f59c760045d7ca608d8 to your computer and use it in GitHub Desktop.
File (based on lists)
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
(* 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