I'm looking at a compiler written for a extremely pared down version of C. I'm new to ocaml, and am particularly confused about this structure
let check (globals, functions) =
(* A bunch of stuff abstracted out *)
let check_function ...
.... (*A b bunch of stuff abstracted out*)
in (globals, List.map check_function functions)
Where globals is a list of (var_type, var_name) and functions is a function records. I didn't post the entire file (it's very long) and my question is just about the outermost let statement.
I've always only seen simple let statements where you might have something like
let name = expr1 in expr2
So what does it mean when you have
let name(param1, param2) = expr1 in (param1, expr1 param2)
Kind of similar to what I have here?
let name(param1, param2) = expr1 in (param1, expr1 param2)
Would be perhaps more clearly written as:
let name (param1, param2) =
expr1
in
(param1, expr1 param2)
That extra space makes it more apparent that name
is a function which takes one argument. That argument is a tuple of two values param1
and param2
. It is locally bound only for the expression (param1, expr1 param2)
which does not use this name
function.
The param1
and param2
names are not used in the expression expr1
(they might as well be (_, _)
) and are out of scope when we get to the expression (param1, expr1 param2)
so if this is valid code, they refer to bindings from before the code you've shown us.
Effectively name
does nothing at all.