仅打印带有 SML/NJ 的打印输出

Printing only print output with SML/NJ

我正在尝试使用 SML/NJ,我使用 sml < source.sml 到 运行 代码,但它打印出太多信息。

例如,这是 source.sml:

fun fac 0 = 1
  | fac n = n * fac (n - 1)
val r = fac 10 ;
print(Int.toString(r));

这是输出:

Standard ML of New Jersey v110.77 [built: Tue Mar 10 07:03:24 2015]
- val fac = fn : int -> int
val r = 3628800 : int
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[autoloading done]
3628800val it = () : unit

Suppress "val it" output in Standard ML, How to disable SMLNJ warnings?, and SMLNJ want to remove "val it = () : unit" from every print statement execution,我得到了一些如何抑制它们的提示。

我执行了CM_VERBOSE=false sml < $filename,在代码里加了一行Control.Print.out := {say=fn _=>(), flush=fn()=>()};,但是还是有一些信息:

Standard ML of New Jersey v110.77 [built: Tue Mar 10 07:03:24 2015]
- 3628800

如何只打印输出?

sml命令旨在交互使用。在我看来,你最好从你的程序中构建一个独立的可执行文件。

有几个选项:

  1. 如果您依赖 SML/NJ 扩展,或者如果您根本无法使用其他 ML 实现,则可以按照 this post to build an SML/NJ heap image that can be turned into a standalone executable using heap2exec.

    [ 中的说明进行操作=25=]
  2. 更好的选择可能是使用 MLton 编译器,它是标准 ML 的另一种实现。它缺少 REPL,但与 SML/NJ 不同,它不需要样板来生成独立的可执行文件。构建就像发布一样简单:

    $ mlton your-program.sml
    $ ./your-program
    3628800