Evan
Evan

Reputation: 11

LPAREN Syntax Error in Standard ML

I'm having a continuous error pop when I try to run this program in ML:

fun find(s,file) =
    let fun findHelper(true, true, ch, w, file, acc, acc2) = TextIO.output(TextIO.stdOut, acc2^"\n")
    | findHelper(b1, b2, ch, w, file, acc) = ch = valOf(TextIO.input1(TextIO.file)) acc2^str(ch) 
    if ch = ""  then 
        if w = acc then b1 = true 
        else acc = "" 
    else if ch = "\n" then b2 = true
        else acc^str(ch)


in 
    findHelper(false, false, "", s, file, "", "")
end 

The error code is:

    project.sml:61.3 Error: syntax error: inserting  LPAREN
    project.sml:65.12 Error: syntax error: inserting  RPAREN

I've inserted a bunch of parenthesis to no avail and honestly, I don't even know why this error popped. The error is centering around the "if ch = "" then" , but there is no error for the other instance of ch, so I don't know why it error'd one and not the other.

Upvotes: 1

Views: 7252

Answers (1)

Andreas Rossberg
Andreas Rossberg

Reputation: 36078

The syntax error is that your inner function contains an expression of the form

 A if B then C else D

(where A is of the form "x = y"). To sequentially evaluate expressions, you have to use the semicolon operator, and parenthesise, thus:

(A; if B then C else D)

However, that would only get you past the syntax error, but not help much otherwise. The code doesn't make much sense. You somehow seem to be assuming that you can use = to assign to variables -- that is not the case. SML is a functional language, and variables are immutable. What you want to use is recursion.

Upvotes: 2

Related Questions