Created
June 11, 2020 15:20
-
-
Save hurryabit/5b8a336ac1fa9326f8cfcc706122e65c to your computer and use it in GitHub Desktop.
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
let nil = { isEmpty = true } in | |
let cons = fun hd tl -> { isEmpty = false; head = hd; tail = tl } in | |
let rec foldrList = fun f z xs -> | |
if xs.isEmpty then | |
z | |
else | |
f xs.head (foldrList f z xs.tail) | |
in | |
let rec foldlList = fun f z xs -> | |
if xs.isEmpty then | |
z | |
else | |
foldlList f (f z xs.head) xs.tail | |
in | |
let read = foldlList (fun n d -> 10*n+d) 0 in | |
let empty = { isEmpty = true } in | |
let node = fun l x r -> { isEmpty = false; left = l; value = x; right = r } in | |
let leaf = fun x -> node empty x empty in | |
let rec foldrTree = fun f z t -> | |
if t.isEmpty then | |
z | |
else | |
foldrTree f (f t.value (foldrTree f z t.right)) t.left | |
in | |
let treeToList = foldrTree cons nil in | |
let exTree = node (leaf 1) 2 (leaf 3) in | |
read (treeToList exTree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment