I am new to OCaml and I would like to write a function that return the sum of all the multiples of 3 and 5 below z.
Here is what I've done:
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 it doesn't work and it's telling me that:
Error: This expression has type 'a * 'b * 'c
but an expression was expected of type int
The syntax for function application in OCaml is problemone (x+1) (y+x) z
, not problemone(x+1,y+x,z)
.
(x+1,y+x,z)
is interpreted as a tuple, which is why the error refers to the tuple type 'a * 'b * 'c
. The tuple is passed as the first argument, which is expected to be an int
. And since functions are curried in OCaml, the compiler will not consider the application of only one argument to a function that expects multiple an error. It therefore only complains about the type mismatch of the first argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With