Reputation: 27
I have written this function to calculate the sum of two integers and then output it s a int option type. But I get this warning. I have defined the None case, but I still get this. Can anyone help?
let add x (Some y) = match x with
|None -> if Some y = None then None
else Some y
|Some x ->
let z = x + y in
if z > max_int then None
else if z < min_int then None
else if (Some y) = None then Some x
else Some (x + y)
I did this but still something is missing. Can anyone think of a case where this does not work ?
let add x y = match (x, y) with
| (None, None) -> None
| (Some x, None) -> None
| (None, Some y) -> None
| (Some x, Some y) -> if x > max_int then None
else if x < 0 then None
else if y < 0 then None
else if x + y > max x y then None
else if x + y < min x y then None
else if y > max_int then None
else if x < min_int then None
else if y < min_int then None
else if x + y >= max_int then None
else if x + y <= min_int then None
else Some (x + y)
Upvotes: 0
Views: 1670
Reputation: 1418
The problem is here: let add x (Some y) = ...
.
This defines add
only in case the second argument isn't None
.
Upvotes: 2