Question

I'm a newby. I write below code.

Delay(e) == fn () => e
Force(e) == e()

fun time_consuming(n) =
   let fun tak(x, y, z) = if x <= y then y
               else tak(tak(x-1,y,z), tak(y-1,z,x), tak(z-1,x,y))
   in          
fun   tak(3*n, 2*n, n)
funend;
fun fib(n) = if n=0 orelse n=1 then 1 else fib(n-1) + fib(n-2);
fun odd(n) = (n mod 2) = 1;
fun f(x, y) = if odd(x) then 1 else fib(y);
f(fib(9), time_consuming(9));

fun lazy_f(x, y) = if odd(x) then 1 else fib(y());
lazy_f(fib(9), fn () => (time_consuming(9)));

It is lazy evaluation code.
But it has some error.

lazy.sml:1.13 Error: syntax error: inserting LPAREN
lazy.sml:4.2 Error: syntax error: inserting LET
lazy.sml:12.44 Error: syntax error: replacing SEMICOLON with EQUALOP
lazy.sml:15.21 Error: syntax error: inserting LPAREN
lazy.sml:17.1 Error: syntax error found at EOF

What these error mean?

Was it helpful?

Solution

In SML, errors will often cascade, that is, one fault can cause the loads of errors after the location of the fault. Therefore, a good tactic is to only look at the very first error, correct it, and retry the code.

If we look at your first error:

lazy.sml:1.13 Error: syntax error: inserting LPAREN

, we can see that it is caused by the first line. If we look at it, this is how it is interpreted by sml; Take the function Delay, send e as argument and get a new function as result. Take this function and send == as argument and get yet another function, to which the lambda function fn () => e is passed. However, SML syntax dictates that in order to pass lambda functions as arguments, they must be surrounded by parantheses, so it issues the error for missing left parantheses, which is cryptically written as "inserting LPAREN".

However, I do not see what the purpose of the first two lines, since the rest of the code is independent of it, and almost correct in syntax too, so now I will simply comment them out (surround them with "(*" and "*)".

Lets run the new code and look at the first of the new errors:

lazy.sml:7.4-8.4 Error: syntax error: deleting  IN FUN

Now we turn our attention to the eighth line, and see that it and the ninth has by mistake been prefixed with "fun". We replace the "fun" on line eight and nine with spaces and run it again.

Now it works! This is the working code:

(* Delay(e) == fn () => e *)
(* Force(e) == e() *)

fun time_consuming(n) =
   let fun tak(x, y, z) = if x <= y then y
               else tak(tak(x-1,y,z), tak(y-1,z,x), tak(z-1,x,y))
   in          
      tak(3*n, 2*n, n)
   end;
fun fib(n) = if n=0 orelse n=1 then 1 else fib(n-1) + fib(n-2);
fun odd(n) = (n mod 2) = 1;
fun f(x, y) = if odd(x) then 1 else fib(y);
f(fib(9), time_consuming(9));

fun lazy_f(x, y) = if odd(x) then 1 else fib(y());
lazy_f(fib(9), fn () => (time_consuming(9)));

As you could see, there were more errors than mistakes, and I hope that you have learnt that decoding the meaning of the error messages is not necessary, and simply looking at the location of the first error is often enough to realize what is wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top