SML error: syntax error: insert DOT

When I run this code in the REPL it throws Error: syntax error: inserting DOT

. I would like to know what this error message means.

I have since fixed the code and still want to know the meaning of this post for future reference.

part_dir

is a function that returns a custom data type direction

with possible templates Left

and Right

.

fun same (fs)=
    case fs of
         (f1::f2::fs') => case (part_dir(f1),part_dir(f2)) of
                               (dir1=dir2) => same (f2::fs')
                             | _           => false
        | _            => true

      

+3


source to share


2 answers


"Syntax error: insert DOT" means that an unexpected token has appeared at the position he is complaining about and that the dot will be legal at that position. However, this does not mean that dot will be the only valid one in this position, or that replacing the token with a dot will correct your mistake.

For the most part, the error message is useless without telling you that there is a syntax error. You should probably just ignore the "DOT insertion" part as it usually doesn't lead you in the right direction. Just look at the row and column it is complaining about and try to find that there is a syntax error.

In your case, the problem is something that is dir1=dir2

not legal. A legal template will be a data type constructor with templates for each of its arguments, variable name, _

or constant or template tuples. There is no pattern to say "a tuple containing two elements that are equal". To do this, you need a template (dir1, dir2)

and then a condition dir1=dir2

in if

.



In your case, you don't even need it if

. You can simply write:

(dir1, dir2) => dir1 = dir2 andalso same (f2 :: fs)

      

+4


source


Also note that your two case statements do not work as you expect. Your last match for the outer shell pattern is actually part of the inner case. In other words, your code actually makes this sense.

fun same (fs)=
    case fs of
      (f1::f2::fs') => case (part_dir(f1),part_dir(f2)) of
                         (dir1=dir2) => same (f2::fs')
                       | _           => false
                       | _           => true

      

When you fix the error indicated by sepp2k and compile the code, you will notice that the interpreter will throw a warning and an error. First the error that the last match of the inner case is redundant, and then a warning that there are no exhaustive matches in this case. That is, it has no cases for all possible inputs.



Thus, it is always useful to put parentheses around case statements, since the same case will occur if your function same

had other matches under the case. This is because a pipe is a common syntax when multiple patterns are matched.

With the parenthesis added, your function will get the intended value

fun same (fs)=
    case fs of
      (f1::f2::fs') => (case (part_dir(f1),part_dir(f2)) of
                         (dir1=dir2) => same (f2::fs')
                       | _           => false)
    | _             => true

      

+1


source







All Articles