0
votes

I want to make a recursive function that sums the integers between two values. I'm doing:

 let rec sum_between x y = 
   if x>y then sum_between y x else
      if x=y then x else x + sum_between x+1 y ;;

But I get the error: This expression has type int -> int but an expression was expected of type int

What am I doing wrong?

1
need sum_between (x + 1) y - Edgar Aroutiounian
@EdgarAroutiounian How about posting this as an answer? - Frank Schmitt

1 Answers

2
votes

Function application has high precedence in OCaml. You need to parenthesize an expression when it's an argument to a function.

Your code

sum_between x+1 y

is parsed like this:

(sum_between x) + (1 y)

You need parentheses:

sum_between (x + 1) y

(Same answer as Edgar Aroutiounian but more helpful detail I hope.)