deadmarshal

Untitled

Aug 31st, 2025
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.46 KB | None | 0 0
  1. pub type Tree(a) {
  2.   Leaf
  3.   Node(left: Tree(a), value: a, right: Tree(a))
  4. }
  5.  
  6. pub fn max(t: Tree(a), compare: fn(a, a) -> Order) -> Option(a) {
  7.   case t {
  8.     Leaf -> None
  9.     Node(l, v, r) -> {
  10.       let lmax = max(l, compare) |> option.unwrap(v)
  11.       let rmax = max(r, compare) |> option.unwrap(v)
  12.       case compare(lmax, v), compare(rmax, v) {
  13.         Eq, _ -> Some(v)
  14.         Gt, _ -> Some(lmax)
  15.         Lt, _ -> Some(rmax)
  16.       }
  17.     }
  18.   }
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment