1XCoderX1
1XCoderX1

Reputation: 27

Ocaml option function

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?

Error

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

Answers (1)

kne
kne

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

Related Questions