2

i am doing a function in OCaml with i want to count the number of iterations in the variable n. This is the function:

let mapdoble f1 f2 l = let rec aux n f_1 f_2 l1 l2= match(n,f_1,f_2,l1,l2) with (n,_,_,[],l2) -> l2 | (n,f_1,_,h::t,l2) when n mod 2 = 0 -> aux n+1 f1 f2 t l2@[f_1 h] | (n,_,f_2,h::t,l2) when n mod 2 = 1 -> aux n+1 f1 f2 t l2@[f_2 h] in aux 0 f1 f2 l [];; 

When i compile it i have this error, i don't know what it is:

Error: This expression has type 'a -> 'b -> 'c list -> 'd -> 'd but an expression was expected of type int 
2
  • 1
    I think you're simply missing parentheses around the two instances of n+1. Commented Jan 22, 2016 at 12:20
  • @ReimerBehrends is write, you have to put parenthesis around n+1. You also have to match all cases, the compiler should warn you about that too with an example. Commented Jan 22, 2016 at 12:28

1 Answer 1

1

Function application, that is just written as a juxtaposition of function name and its arguments, binds tighter than infix operators, i.e., it has a higher precedence. To clarify the above said, let's go to an example. The expression:

aux n+1 f1 f2 t l2@[f_1 h] 

is actually parsed by a compiler as:

(aux n) + (1 f1 f2 t l2) @ ([f_1 h]) 

now, I hope, it is obvious, why you got so strange compiler messages. In particular, you are trying to apply (+) operator to the result of aux n, which is actually a function of four arguments and definitely not an int.

So the correct version would be:

aux (n + 1) f1 f2 t (l2 @ [f_1 h]) 

You have also some issues with pattern matching, it is not irrefutable, but it is out of the scope of the question.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.