OCaml Functions Using the function keyword

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

The function keyword automatically has pattern matching when you define the body of your function. Observe it below:

# let foo = function
0 -> "zero"
| 1 -> "one"
| 2 -> "couple"
| 3 -> "few"
| _ -> "many";;
val foo : int -> bytes = <fun>

# foo 0;;
- : bytes = "zero"

# foo 3;;
- : bytes = "few"                                                                         

# foo 10;;
- : bytes = "many"                                                                        

# let bar = function
"a" | "i" | "e" | "o" | "u" -> "vowel"
| _ -> "consonant";;
val bar : bytes -> bytes = <fun>                                                          

# bar "a";;
- : bytes = "vowel"

# bar "k";;
- : bytes = "consonant" 


Got any OCaml Question?