This expression is of type a * b * c, but type int was expected
I am new to OCaml and I would like to write a function that returns the sum of all multiples of 3 and 5 below z.
Here is what I did:
let rec problemone x y z = match x with
| x when x > z -> y
| x when x mod 3 = 0 && x mod 5 = 0 -> problemone(x+1,y+x,z)
| x when x mod 3 = 0 && x mod 5 <> 0 -> problemone(x+1,y+x,z)
| x when x mod 3 <> 0 && x mod 5 = 0 -> problemone(x+1,y+x,z)
;;
Unfortunately, this does not work, and he tells me that:
Error: This expression has type 'a * 'b * 'c
but an expression was expected of type int
The syntax for applying functions in OCaml is problemone (x+1) (y+x) z
, rather than problemone(x+1,y+x,z)
.
(x+1,y+x,z)
is interpreted as a tuple, so the error is of the tuple type 'a * 'b * 'c
. The tuple is passed as the first argument, which is expected to be int
. And since the functions are in OCaml, the compiler will not consider the application of only one argument to a function that expects multiple errors. Therefore, he only complains about the inconsistency of the type of the first argument.
(x+1, y+x, z)
( 3-) x
( y
z
).
problemone (x+1) (y+x) z
.