Skip to content

Instantly share code, notes, and snippets.

@eduardoleon
Created April 22, 2019 03:17
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 eduardoleon/3bd2398ad2754f604762d52964103c07 to your computer and use it in GitHub Desktop.
Save eduardoleon/3bd2398ad2754f604762d52964103c07 to your computer and use it in GitHub Desktop.
Reversing binary trees.
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