1

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.

1 Answer 1

2

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.