I'm very new to SML and I'm trying to write a function that determines whether a string is balanced or not. A balanced string is: 1. a string where the number of "(" occurrences is equal to the number of ")". 2. in every prefix ")" does not occur more than "(". For example : ")(" is not balanced because the second condition does not apply.
I wrote this code but I'm getting a compilation error:
fun balanceAux(s:string , i:int , c1:int , c2:int) : bool =
if i = size(s)-1 then
if c1=c2 then true
else false
else
if c2>c1 then false
else
if (str(String.sub(s,i)) = "(") then balanceAux(s,i+1,c1+1,c2)
else
if (str(String.sub(s,i)) = ")") then balanceAux(s,i+1,c1,c2+1);
else balanceAux(s,i+1,c1,c2);
fun balance(t:string) : bool =
balanceAux(t,0,0,0);
I'm getting 3 compilation errors:
inserting LPAREN
inserting LET
found at EOF
Any ideas what am I doing wrong? I read around that it might have to do with my code having an expression of:
If A then B else If C then D
but I cant understand what can I do.