Search code examples
sml

syntax error: inserting LPAREN


This is my first time with sml.

I don't understand what is wrong with this syntax:

 fun merge (l1 : int list , l2 : int list) : int list = 
  if ([] , l2) then l2
  else if (l1 , []) then l1
    else (x :: xs , y :: ys) 
        if x < y then x :: (merge (xs , l2))
           else y :: (merge (l1 , ys)));

Please help


Solution

  • The problem is that the if / then / else syntax is not used for pattern-matching. (You've melded two unrelated syntaxes.)

    So, for example, if ([] , l2) does not work, because the condition in an if expression needs to have type bool, which ([], l2) does not.

    Instead, you want to write:

    fun merge ([], l2) = l2
      | merge (l1, []) = l1
      | merge (l1 as x :: xs, l2 as y :: ys) =
           if x < y
           then x :: merge (xs, l2)
           else y :: merge (l1, ys)