Lets assume the following function
val foo : int -> int -> bool -> int
let foo x y b =
let x,y = if x > y then y,x else x,y in
let rec loop x y b =
if x >= then x
else if b then loop (x 1) y (not b)
else loop x (y-1) (not b)
in
loop x y b
I still don't quite understand the concept of the "in". Does the line mean "let x,y = ... in" that it is executed immediately or only when you "Call" it? And when i dont need to call it why do i need the last line loop x y b? Thanks in advance :)
CodePudding user response:
in
is just part of the OCaml syntax - let PATTERN = EXPR1 in EXPR2
is an expression which binds the result of EXPR1
to PATTERN
and then evaluates EXPR2
with the new bindings present. In some languages like F# and Haskell, you don't (always) need in
- it's inferred from the indentation. OCaml syntax is indentation insensitive which requires it to have an explicit in
.
Does the line mean "let x,y = ... in" that it is executed immediately or only when you "Call" it?
It's evaluated immediately.
And when i dont need to call it why do i need the last line loop x y b?
In this code, the previous line defines a function named loop
with 3 arguments, and then later you call the function with the arguments x y b
.