The other answer and the first comment at the top both pointed out a misused keyword of begin
. It's a minor fix, but does not address what Antonio is asking about.
For starters, I am not familiar with what context you are using your function in, I am just going to tell you where your program went wrong in a trace-based approach.
First, I made a few changes to your code, so that it is more readable, and also easier to figure out the stack frame where things went wrong:
let separate = fun formula ->
let rec aux = fun counter start_position size ->
match formula.[start_position + size] with
| '(' -> aux (counter + 1) start_position (size + 1)
| ')' -> if (counter - 1) = 0 then (
(Printf.printf "%d %d %d\n" counter start_position size);
(
((Printf.printf "pass1\n"); (String.sub formula (start_position + 1) size))
,
(String.sub formula (size + start_position + 3) ((String.length formula) - (size + start_position + 2)))
)
)
else aux (counter - 1) start_position (size +1)
| _ -> aux counter start_position (size + 1)
in aux 0 (String.index formula '(') 0
;;
separate "&(A)(B)";;
(*
1 1 2
Exception: Invalid_argument "String.sub / Bytes.sub".
Raised at file "pervasives.ml", line 33, characters 25-45
Called from file "string.ml", line 47, characters 2-23
Called from file "//toplevel//", line 10, characters 4-108
Called from file "toplevel/toploop.ml", line 180, characters 17-56
*)
Run this, and you will find out the call that triggered the invalid argument exception is at the call of aux 1 1 2
.
Then you can use these codes to test why it went wrong:
let (counter, start_position, size) = (1,1,2) in
let formula = "&(A)(B)" in
(
(size + start_position + 3)
,
(String.length formula) - (size + start_position + 2)
) ;;
(*
- : int * int = (6, 2)
*)
And this is how you know why String.sub
does not accept your arguments:
String.sub "&(A)(B)" 6 2;;
(* Exception: Invalid_argument "String.sub / Bytes.sub".
Called from file "toplevel/toploop.ml", line 180, characters 17-56 *)
And this is as far as I can help you. Debugging in OCaml takes time, but it's doable. Go back to the good o'l printing trick.
And I hope this is enough information for you to see why your logic is faulty here. I hope you can figure out the way to fix it according to you problem context.