0
votes

I have defined 4 functions that return a tuple of type (int * int) something as below all those functions are tested.

let f1 (p1, p2, p3, p4) =
 if <condition> then p1
 ....
 else p4

How can I define a function that will use output of those 4 functions to return a tuple of type (int * int, int * int, int * int, int * int)

let test(p1, p2, p3, p4) =
  (f1(p1, p2, p3, p4), f2(p1, p2, p3, p4), f3(p1, p2, p3, p4), f4(p1, p2, p3, p4))

OCaml interpeter gives me a syntax error, which doesnt really tell me anything.

3
Hard to tell without the actual error message (having the function code could help too). on a side note as you always pass (p1, p2, p3, p4) as a whole to f1 .. f4 you can simplify it test ps = f1 ps, f2 ps, f3 ps, f4 ps - Sehnsucht
Thank you, I just realized that I missed a second semicolon at the end of the function definition. Works now - Michael Daniloff
“which doesnt really tell me anything” so you know how we feel - Pascal Cuoq

3 Answers

2
votes

I don't see any problem in the code you show here. Most likely you'll have to show more code. It would also help to see the specific error from the compiler.

Here's some code that returns 3 pairs (just to keep the example smaller):

# let f (x, y) = (x + 1, y + 1);;
val f : int * int -> int * int = <fun>
# let g (x, y) = (f (x, y), f (x, y), f (x, y));;
val g : int * int -> (int * int) * (int * int) * (int * int) = <fun>
1
votes

The function f1 doesnt return the type you think about (int * int) but int :

# let f1 (p1, p2, p3, p4) = if true then p1+1 else p4+1;;
val f1 : int * 'a * 'b * int -> int = <fun>

For example if you have :

let f1 (p1, p2, p3, p4) = (p1+1,p2+1);;
let f2 (p1, p2, p3, p4) = (p1+1,p2+1);;
let f3 (p1, p2, p3, p4) = (p1+1,p3+1);;
let f4 (p1, p2, p3, p4) = (p1+1,p4+1);;

You can define, without error, your function test :

   # let test(p1, p2, p3, p4) = (f1(p1, p2, p3, p4), f2(p1, p2, p3, p4), f3(p1, p2, p3, p4), f4(p1, p2, p3, p4));;
val test :
  int * int * int * int ->
  (int * int) * (int * int) * (int * int) * (int * int) = <fun>

Also you can write :

let test nuplet = (f1 nuplet, f2 nuplet, f3 nuplet, f4 nuplet);;
0
votes

That was the case of a missing semicolon(the second one) at the end of the functioon definition. That was a stupid question on my part. Sorry