Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This expression has type a*b*c but type int was expected

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
like image 930
JIJOJJH Avatar asked Mar 07 '23 02:03

JIJOJJH


1 Answers

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.

like image 104
glennsl Avatar answered Mar 10 '23 15:03

glennsl