gpt4 book ai didi

printing - OCaml 打印语句

转载 作者:行者123 更新时间:2023-12-03 21:09:06 26 4
gpt4 key购买 nike

是否可以将用于调试/测试的打印语句放在代码块中?例如,在 Java 中,您可以使用 System.out.println("")在检查变量或其他事物的方法中间,但在 OCaml 中,会出现类似 print_string 的命令工作?它不会返回一个单元类型的值,从而导致错误,而不是允许您打印它吗?

最佳答案

这是一个简单的例子,它显示了 Ray Toal's answer 中提到的表达式序列。不一定需要括号:

let get_a_string =
let a_string = "a string" in
(* The semicolon causes the return value of the statement to be discarded *)
Printf.printf "Debug: %s\n" a_string;
a_string

let () = Printf.printf "Result: %s\n" get_a_string
另一种丢弃函数返回值的方法是使用 ignore。 :
ignore (Printf.printf "Debug info");

关于printing - OCaml 打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7382002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com