Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub type Tree(a) {
- Leaf
- Node(left: Tree(a), value: a, right: Tree(a))
- }
- pub fn max(t: Tree(a), compare: fn(a, a) -> Order) -> Option(a) {
- case t {
- Leaf -> None
- Node(l, v, r) -> {
- let lmax = max(l, compare) |> option.unwrap(v)
- let rmax = max(r, compare) |> option.unwrap(v)
- case compare(lmax, v), compare(rmax, v) {
- Eq, _ -> Some(v)
- Gt, _ -> Some(lmax)
- Lt, _ -> Some(rmax)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment