我正在编写SML代码,根据具体公式计算一段时间内的活人,死人和僵尸的数量。 我已将我的代码放入“.sml”文件中,然后在SMLNJ中打开它。我得到的错误如下。
zombies.sml:36.3 Error: syntax error: inserting LPAREN
zombies.sml:43.1 Error: syntax error found at EOF
这是我的代码。
val living = 500000.0;
val zombie = 0.0;
val dead = 1.0;
val counter = 0.0;
val d_ = 0.0001; (*chance of natural death*)
val z_ = 0.0001; (*chance of zombification after death*)
val b_ = 0.0095; (*chance of zombie attack*)
val a_ = 0.005; (*chance of a zombie dying?*)
val birth_rate = 1.0;
(*calculates amount of living*)
fun S(living:real, birth_rate:real, b_:real, zombie:real, d_:real) = birth_rate - b_*living*zombie - d_*living;
(*calculates amount of zombies*)
fun Z(zombie:real, b_:real, living:real, z_:real, dead:real, a_:real) = b_*living*zombie + z_*dead - a_*living*zombie;
(*calculates amount of dead*)
fun R(dead:real, d_:real, living:real, a_:real, zombie:real) = d_*living + a_*living*zombie - zombie*dead;
fun program (living:real, zombie:real, dead:real, birth_rate:real, b_:real, z_:real, d_:real, a_:real, counter:real) =
= if counter = 0 then 1
= else (S(living, birth_rate, b_, zombie, d_);
Z(zombie, b_, living, z_, dead, a_);
R(dead, d_, living, a_, zombie);
counter = (counter + 1);
program(living, zombie, dead, birth_rate, b_, z_, d_, a_, counter));
program(living, zombie, dead, birth_rate, b_, z_, d_, a_, counter);
最后,工作编辑。 功能语言很奇怪。
fun add(x, y) = x + y;
(*calculates amount of living*)
fun S(living, birth_rate, b_, zombie, d_) =
round(real(birth_rate+living) - b_*real(living)*real(zombie) - d_*real(living));
(*calculates amount of zombies*)
fun Z(zombie, b_, living, z_, dead, a_) =
round(b_*real(living)*real(zombie) + z_*real(dead) - a_*real(living)*real(zombie));
(*calculates amount of dead*)
fun R(dead, d_, living, a_, zombie) =
round(d_*real(living) + a_*real(living)*real(zombie) - real(zombie*dead));
fun main (living) =
let
val living = ref living;
val zombie = ref 1;
val dead = ref 1;
val birth_rate = 1;
val d_ = 0.0001; (*chance of natural death*)
val z_ = 0.0001; (*chance of zombification after death*)
val b_ = 0.0095; (*chance of zombie attack*)
val a_ = 0.005; (*chance of a zombie dying?*)
val count = ref 1;
in
while (!count < 10) do
(
living := S(!living, birth_rate, b_, !zombie, d_);
print("Living count: " ^ Int.toString(!living) ^ "\n");
zombie := Z(!zombie, b_, !living, z_, !dead, a_);
print("Zombie count: " ^ Int.toString(!zombie) ^ "\n");
dead := R(!dead, d_, !living, a_, !zombie);
print("Dead count: " ^ Int.toString(!dead) ^ "\n");
count := !count + 1;
print(Int.toString(!count) ^ "\n")
)
end;
答案 0 :(得分:1)
该行开头的那些=
显然是假的:
fun program (…) =
= if counter = 0 then 1
= else (S(living, birth_rate, b_, zombie, d_);
也许文件在传输中被破坏了? (=
用作MIME中的行继续符,但它位于行的末尾。)
此外,整数和浮点数是ML中的不同类型。 (这也适用于文字。)您需要决定counter
和birth_rate
整数或实数。
该行
counter = (counter + 1);
没有任何影响。变量在SML中是不可变的。您需要使用let绑定,或者直接使用递增的计数器值调用program
。
答案 1 :(得分:0)
要在StackOverflow上询问可能为useful for others的问题,请参阅此部分:
有些问题仍然偏离主题,即使它们符合上述类别之一:
- 问题寻求调试帮助(“为什么这段代码不起作用?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用。请参阅:How to create a Minimal, Complete, and Verifiable example。
在您的示例中,您已经确定问题出在代码的最后部分,但由于最后一部分取决于第一部分,您不能简单地发布最后一部分,因为否则问题可以不被复制。但语法错误可以。
在你达到这一点之前减少直线:
fun program (living:real, zombie:real, dead:real, birth_rate:real, b_:real, z_:real, d_:real, a_:real, counter:real) =
= if counter = 0 then 1
= else raise Fail "Not relevant"
会产生同样的错误。
也许你最好使用一个REPL来提供比SML / NJ更好的错误信息。
比较例如SML / NJ的输出:
typo.sml:24.3 Error: syntax error: inserting LPAREN
typo.sml:33.1 Error: syntax error found at EOF
/usr/lib/smlnj/bin/sml: Fatal error -- Uncaught exception Compile with "syntax error" raised at ../compiler/Parse/main/smlfile.sml:15.24-15.46
File "typo.sml", line 24, characters 2-4:
! = if counter = 0 then 1
! ^^
! Syntax error.