Hacker News new | ask | show | jobs
by nullspace 3249 days ago

  // Flip the BST using pattern matching!
  let rec flip bst =
    match bst with
    | Empty -> bst
    | Node(item, left, right) -> Node(item, flip right, flip left)

Without type annotations, how can it tell the flip takes an instance of BST as the parameter?
1 comments

The variable bst is something that can be matched with Empty and with Node(item, left, right), both of which are of type BST. When two values can be matched with each other, they must be of the same type. Therefore bst is of type BST too.