为什么我的 OCaml 巴贝奇程序无法运行?

tin*_*aiz 2 ocaml

代码:

let babbage = 
  let n = read_int in
  let current = ref n in
  let square = ref !current in
  let mul = !current * !current in
  while ((square := mul) mod 1000000 != 269696) && (!square < max_int) do
          current := !current + 1;
  done;
  if(!square > max_int) then
    print_string "Condition not satisfied before max_int reached."
  else print_string "The smallest number whose square ends in 269696 is"; !square
Run Code Online (Sandbox Code Playgroud)

错误:

let mul = !current * !current in
  Error: This expression has type unit -> int
         but an expression was expected of type int
         Hint: Did you forget to provide `()' as argument?
Run Code Online (Sandbox Code Playgroud)

仍在学习,但我想真正知道它出了什么问题

编辑#1:这是一个练习,所要求的类型是unit -> int并且其函数为let babbage () =

gle*_*nsl 5

错误消息给了你很好的提示,但是你必须遵循一些任务才能找到原因。

square最初被赋予 的值(和类型)current, 最初被赋予 的值n, 最初被赋予 的值read_int

read_int是一个函数unit -> int,因此n也将具有类型unit -> int,并且因此将具有currentsquare

如果您将错误消息中给出的提示应用于read_int,问题应该得到解决。(虽然这不是你唯一的问题,但这超出了这个问题的范围。)