【问题标题】:Trouble with printing a string in OCaml在 OCaml 中打印字符串时遇到问题
【发布时间】:2016-08-18 22:39:08
【问题描述】:

代码在没有 print 语句的情况下编译。但是,使用打印语句,我在Error: This expression has type unit but an expression was expected of type int 的那一行得到错误。我也试过print_string numDigits

let rec rev_int num =
  if num / 10 == 0 then
    num
  else
    let temp = num mod 10 in
    let numDigits = string_of_int num in
    Printf.printf "%s" numDigits

    let num = (num - temp) / 10 in
    temp * (10 * String.length(numDigits)) + rev_int num;;

【问题讨论】:

    标签: printing types output ocaml format-string


    【解决方案1】:

    Printf 行末尾缺少分号。

    另一种操作系统,以let () = 开始这一行并以in 结束。

    【讨论】:

    • 谢谢;这解决了它!只是出于好奇,在其他代码printf 上以没有分号或以两个结尾。为什么是这种情况?
    • 2 个分号已过时,旨在分隔顶级定义。我建议不要在 ocaml 交互式循环之外使用它(无论如何它都是强制性的)。
    • 1 个分号用于分隔副作用(unit 键入的值);例如:let x = ref 0.0 in for i = 1 to 5 do x := !x +. 1./.float i; Printf.printf"%f\n"!x; done;。编译器确保 ; 之前的值是 unit 类型(这里是值 x := !x +. 1./.float i ; Printf.printf"%f\n"!x 和整个循环。
    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多