"Corresponds ... true & # 8594; foo | false & # 8594; bar" has special meaning in Ocaml?

I came across the following design in different places of the Ocaml project. I am reading the code.

match something with
  true -> foo
  | false -> bar

At first glance, it works like a regular operator if. At a second glance, it works like a regular ifoperator! At the third glance, I decided to ask in SO. Does this construction have a special meaning or a subtle difference from an expression ifthat matters in special cases?

+5
source share
2 answers

Yes, this is an if statement.

match OCaml-, if, .

+11

, if, , .

" - switch, 10 " -

,

, OCAML , , ,

  > Let imply v = 
    match v with 
     | True, x -> x 
     | False, _ -> true;; 

  > Let head = function 
   | [] -> 42 
   | H:: _ -> am;

  > Let rec sum = function 
   | [] -> 0 
   | H:: l -> h + sum l;; 
+2

All Articles