vault backup: 2023-09-07 16:08:59
This commit is contained in:
@ -49,3 +49,28 @@ Exception: Invalid_argument "oops"
|
||||
else a/b ;;;
|
||||
val div int -> int -> int = <fun>
|
||||
```
|
||||
|
||||
## 3.3. Filtering
|
||||
### Explicit filtering
|
||||
|
||||
> Explicit means that you matches the value
|
||||
|
||||
|
||||
**General syntax**
|
||||
|
||||
```Ocaml
|
||||
match expr with pattern1 -> result1 | pattern2 -> result2 | ... | patternn -> resultn
|
||||
```
|
||||
> All expressions and pattern must avec the same type
|
||||
|
||||
**Example**
|
||||
```Ocaml
|
||||
# let f x = match x with
|
||||
0 -> 18
|
||||
1 -> 24
|
||||
y -> y + y;;
|
||||
|
||||
val f : int -> int = <fun>
|
||||
|
||||
# f 1;;
|
||||
- : int = 24
|
||||
|
@ -163,6 +163,9 @@ val max4 : 'a -> 'a -> 'a -> 'a -> 'a = <fun>
|
||||
```
|
||||
### Exercise 3.4
|
||||
```Ocaml
|
||||
let highest_square_sum x1 x2 x3 = let bigger = max3(x1 x2 x3) and middle = middle3(x1 x2 x3) in (bigger*bigger, middle*middle)
|
||||
let highest_square_sum x y z =
|
||||
let sq x = x*x in
|
||||
sq(max3 x y z) + sq(middle3 x y z)
|
||||
val highest_square_sum : int -> int -> int -> int = <fun>
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user