在下面的代码中,行val () = println! ("-> ", nn)
会生成错误:
$ myatscc collatz.dats
/home/brandon/workspace/ATStest/collatz/collatz.dats: 203(line=13, offs=13) -- 225(line=13, offs=35): error(3): the symbo
l [print] cannot be resolved due to too many matches:
D2ITMcst(print_option) of 0
D2ITMcst(print_list_vt) of 0
/home/brandon/workspace/ATStest/collatz/collatz.dats: 203(line=13, offs=13) -- 225(line=13, offs=35): error(3): the dynam
ic expression cannot be assigned the type [S2Ecst(atsvoid_t0ype)].
/home/brandon/workspace/ATStest/collatz/collatz.dats: 203(line=13, offs=13) -- 225(line=13, offs=35): error(3): mismatch
of static terms (tyleq):
The actual term is: S2Eerrexp()
The needed term is: S2Ecst(atsvoid_t0ype)
patsopt(TRANS3): there are [2] errors in total.
exit(ATS): uncaught exception: _2home_2brandon_2workspace_2ATS_2dPostiats_2src_2pats_error_2esats__FatalErrorExn(1025)
这是代码
(*
To compile:
myatscc collatz.dats
*)
#include "share/atspre_staload.hats"
//TODO: add guards to make sure nn is non-negative
fun collatz(nn: int): int = let
val newN: int =
if nn % 2 = 0 then nn / 2
else 3 * nn + 1
//FIXME: too many matches: print_option or print_list_vt:
// val () = println! ("-> ", nn)
//FIXME: Why is this not printing:
val () = println! ("-> ", 1)
in
if (newN: int) != (1: int) then collatz(newN)
else newN
end
implement
main0 () = {
val _ = collatz(42)
}
我也很困惑为什么这个假的println!没有被执行(val () = println! ("-> ", 1)
)。
这是Scala(run it online)中的一个程序,可以完成我的目标:
@tailrec
def collatz(n: BigInt): BigInt = {
assert (n > 0)
val newN =
if (n % 2 == 0) n / 2
else 3 * n + 1
println(s"-> $newN")
if (newN != 1) collatz(newN)
else newN
}
collatz(BigInt("7893450809435834508"))