Created
April 22, 2019 03:17
-
-
Save eduardoleon/3bd2398ad2754f604762d52964103c07 to your computer and use it in GitHub Desktop.
Reversing binary trees.
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
signature TREE = | |
sig | |
type 'a tree | |
type 'a many | |
datatype 'a split = Pure of 'a | Many of 'a tree many | |
val pure : 'a -> 'a tree | |
val many : 'a tree many -> 'a tree | |
val rev : 'a tree -> 'a tree | |
val split : 'a tree -> 'a split | |
end | |
structure Tree2 : TREE = | |
struct | |
datatype 'a tree = P of 'a | M of 'a tree many | R of 'a tree many | |
withtype 'a many = 'a * 'a | |
val pure = P | |
val many = M | |
fun rev (M xs) = R xs | |
| rev (R xs) = M xs | |
| rev xs = xs | |
datatype 'a split = Pure of 'a | Many of 'a tree many | |
fun split (P x) = Pure x | |
| split (M xs) = Many xs | |
| split (R (a, b)) = Many (rev b, rev a) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment