Remix.run Logo
yawaramin 4 days ago

> the syntax seems absolutely alien to me

Syntax is one of those funny things. The more you look at it, the more sense it makes. The more you learn about it and the reasons behind why it's designed that way–the more sense it makes.

> Also, you need to end the declaration with `in`?

Not all of them, just the ones that need to refer to previously defined bindings. So eg you could do:

    let print_expr exp =
      (* Local function definitions *)
      let open_paren prec op_prec = if prec > op_prec then print_string "("
      and close_paren prec op_prec = if prec > op_prec then print_string ")"
      in
      let rec print prec = function (* prec is the current precedence *)
        | Const c -> print_float c
        | Var v -> print_string v
        | Sum (f, g) ->
          open_paren prec 0;
          print 0 f; print_string " + "; print 0 g;
          close_paren prec 0
        | Diff (f, g) ->
          open_paren prec 0;
          print 0 f; print_string " - "; print 1 g;
          close_paren prec 0
        | Prod (f, g) ->
          open_paren prec 2;
          print 2 f; print_string " * "; print 2 g;
          close_paren prec 2
        | Quot (f, g) ->
          open_paren prec 2;
          print 2 f; print_string " / "; print 3 g;
          close_paren prec 2
      in
      print 0 exp