Search code examples
smlml

Getting rid of synatax errors in the following small program


datatype inttree = Empty | Node of int * inttree * inttree

fun insert(t,i)= if t=Empty then Node(i,Empty,Empty)
 else if t=Node(j,l,r):
        if (i=j) 
                 then t 
                 else if (i < j) 
          then Node(j,insert l i,r)
           else Node(j,l,insert r i)

the syntax errors are

stdIn:8.7-19.4 Error: syntax error: deleting  ELSE ID
stdIn:25.15-25.25 Error: syntax error: deleting  FUN ID
stdIn:25.32-25.36 Error: syntax error: deleting  IF ID

Solution

  • Properly formatted, here's your function:

    fun insert(t,i) =
        if t = Empty then
            Node(i,Empty,Empty)
        else if t = Node(j,l,r):
            if (i=j) then
                t 
            else if (i < j) then
                Node(j,insert l i,r)
            else
                Node(j,l,insert r i)
    

    Do you see what's wrong? The if t = Node(j,l,r): has no else to match it. Also, it should have a then instead of a colon (:).