What does let rec *****= function means?

Hello ,
I’m a beginner in OCaml and i already know how to use normal recursive functions .
I was reading a postcode , here is it :

 let rec eval2 a val_a b val_b = function
    | Var x -> if x = a then val_a
               else if x = b then val_b
               else failwith "The expression contains an invalid variable"
    | Not e -> not(eval2 a val_a b val_b e)
    | And(e1, e2) -> eval2 a val_a b val_b e1 && eval2 a val_a b val_b e2
    | Or(e1, e2) -> eval2 a val_a b val_b e1 || eval2 a val_a b val_b e2
  let table2 a b expr =
    [(true,  true,  eval2 a true  b true  expr);
     (true,  false, eval2 a true  b false expr);
     (false, true,  eval2 a false b true  expr);
     (false, false, eval2 a false b false expr) ];;

and I didn’t understand the syntax of the first line because i’m used to use match arg with , but there it’s kind of function matching and i didn’t got it .
I searched for similar topics but i didn’t found one .
Can someone tell me what this syntax means or just leave me a link that explains the use of let rec *** = function .
Thanks

The first line is equivalent to let rec eval2 a val_a b val_b c = match c with

function is equivalent to fun x -> match x with <pattern>

Function Definition section of the manual will have more details: https://caml.inria.fr/pub/docs/manual-ocaml/expr.html#sec129

Haven’t you omitted a b between val_a and val_b or it should be like that ?
Thanks

I had indeed missed the b. I updated the answer. Thanks

I’m sorry i do not see why they are equivalent fun x -> match x with <pattern> takes only one argument which is x while let rec eval2 a val_a b val_b c = match c with function takes 3 arguments .

Maybe the ocaml manual on pattern-matching and functions will help. Search for “Pattern matching is used in an essential way for defining functions by cases.”

fun a b c -> <do something> is also equivalent to fun a -> fun b -> fun c -> <do-something>

https://www.cs.cornell.edu/courses/cs3110/2014sp/lectures/3/lec03.html Should be helpful to understand how OCaml works with functions (look for how currying works).

Example:

fun a b = a + c
(* This will have the type as int -> int -> int *)

The -> is right associative so you can think of the function to have the type int -> (int -> int)
Which indicates that its a function that takes one integer and in turn returns a new function that takes an integer and then returns an integer value as the result.

Thanks you very much , i’m reading it , it making it clear !!

Thank you very much !!
It’s clearer right now !
Thanks for your help

@anuragsoni @Chet_Murthy
I was reading in https://www.cs.cornell.edu/courses/cs3110/2014sp/lectures/3/lec03.html
and i didn’t get what does the expression is sugar for means in :
We saw that a function with multiple parameters is really just syntactic sugar for a function that is passed a tuple as an argument. For example,
let plus (x, y) = x + y
is sugar for
let plus (z : int * int) = match z with (x, y) -> x + y
which in turn is sugar for
let plus = fun (z : int * int) -> match z with (x, y) -> x + y

Does it mean smoother or easiest or something else ??

Yes. sugar was referring to the term “Syntactic sugar” i.e. something designed to make things easier to read or express.

For two syntaxes A and B, it means there is a mechanical rule for translating A to B, and that A is simpler, easier to understand, or has some other similar advantage. The A form is considered the “sugary” form because it’s more pleasant. So you can define your underlying language in the general B form, while allowing some simplifications for special cases. This is an old hacker term:

http://www.hacker-dictionary.com/terms/syntactic-sugar

4 Likes

@jeffsco @anuragsoni thank you very much