by manoDev · 1y ago · 0 comments
Real life sample:
let print_expr exp =
(* Local function definitions *)
let open_paren prec op_prec =
if prec > op_prec then print_string "(" in
let close_paren prec op_prec =
if prec > op_prec then print_string ")" in
let rec print prec exp = (* prec is the current precedence *)
match exp with
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;;
A function is defined as: let print_expr exp =
That seems pretty hard to read at a glance, and easy to mistype as a definition.Also, you need to end the declaration with `in`?
Then, semicolons...
open_paren prec 0;
print 0 f; print_string " + "; print 0 g;
... and even double semicolons ... print 0 exp;;
That looks like a language you really want an IDE helping you with.I actually really like the syntax of OCaml, its very easy to write and when you're used to it, easy to read (easier than reasonml IMO).
Double semicolons are afaik only used in the repl.
YMMV but let expressions are one of the nice things about OCaml - the syntax is very clean in a way other languages aren't. Yes, the OCaml syntax has some warts, but let bindings aren't one of them.
It's also quite elegant if you consider how multi-argument let can be decomposed into repeated function application, and how that naturally leads to features like currying.
> Also, you need to end the declaration with `in`?
Not if it's a top level declaration.
It might make more sense if you think of the `in` as a scope operator, eg `let x = v in expr` makes `x` available in `expr`.
> Then, semicolons...
Single semicolons are syntactic sugar for unit return values. Eg,
print_string "abc";
is the same as let () = print_string "abc" inThis doesn't look or feel all that different to me:
enum Expr {
Const(f64),
Var(String),
Sum(Box<Expr>, Box<Expr>),
Diff(Box<Expr>, Box<Expr>),
Prod(Box<Expr>, Box<Expr>),
Quot(Box<Expr>, Box<Expr>),
}
fn print_expr(exp: &Expr) {
fn open_paren(prec: i32, op_prec: i32) {
if prec > op_prec {
print!("(");
}
}
fn close_paren(prec: i32, op_prec: i32) {
if prec > op_prec {
print!(")");
}
}
fn print_rec(prec: i32, exp: &Expr) {
match exp {
Expr::Const(c) => print!("{}", c),
Expr::Var(v) => print!("{}", v),
Expr::Sum(f, g) => {
open_paren(prec, 0);
print_rec(0, f);
print!(" + ");
print_rec(0, g);
close_paren(prec, 0);
}
Expr::Diff(f, g) => {
open_paren(prec, 0);
print_rec(0, f);
print!(" - ");
print_rec(1, g);
close_paren(prec, 0);
}
Expr::Prod(f, g) => {
open_paren(prec, 2);
print_rec(2, f);
print!(" * ");
print_rec(2, g);
close_paren(prec, 2);
}
Expr::Quot(f, g) => {
open_paren(prec, 2);
print_rec(2, f);
print!(" / ");
print_rec(3, g);
close_paren(prec, 2);
}
}
}
print_rec(0, exp);
}The syntax is `let <constantname> <parameters> = <value> in <expression>;;` where `expression` can also have `let` bindings.
So you can have
let one = 1 in
let two = 2 in
let three = one + two in
print_endline (string_of_int three);; let x = v in expr
`x` is now available for use in `expr`In essence, an OCaml program is a giant recursive expression, because `expr` can have its own set of let definitions.
In the REPL, this is where the double semicolons come in, as a sort of hint to continue processing after the expression.
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 expFor this family of languages, it's very uncomplicated and easy to parse (and they all "sort of look the same" if you squint). Consider this: if you knew nothing of Java or C, their syntax would be weird mumbo-jumbo to you too. Even a language which is considered relatively simple, like Python, can trip you up in unexpected ways if you're unfamiliar with it.
Fortunately, getting familiar with the syntax is easy. Doubly so if you actually tackle a project with it.
Coming from a Java/C/Python/JS background, it's hard to casually read OCaml snippets before first getting acquainted with its syntax, because it belongs to a different family of programming languages.