I'm trying to use SML/NJ, and I use sml < source.sml
to run the code, but it prints out too much information.
For example, this is the source.sml
:
fun fac 0 = 1
| fac n = n * fac (n - 1)
val r = fac 10 ;
print(Int.toString(r));
This is the output:
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
From 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, I got some hints how to suppress them.
I execute CM_VERBOSE=false sml < $filename
and added one line of Control.Print.out := {say=fn _=>(), flush=fn()=>()};
in the code, but I still have some message:
Standard ML of New Jersey v110.77 [built: Tue Mar 10 07:03:24 2015]
- 3628800
How can I print out only the output?
print in SML prints out a string. The value of print is not what is of interest, but rather the effect that it has, which is to output something on the screen. Consider the following revised version of tryGuess . In order to print we need a string so we use the built-in function Real.
(Note that SML is case sensitive.) For the integer functions, the signature is called INTEGER .
The sml
command is intended to be used interactively. It sounds to me like you would be better off building a standalone executable from your program instead.
There are a few options:
If you are relying on SML/NJ extensions, or if you simply cannot use another ML implementation, you can follow the instructions in this post to build an SML/NJ heap image that can be turned into a standalone executable using heap2exec.
A better option might be to use the MLton compiler, another implementation of Standard ML. It lacks a REPL, but unlike SML/NJ it requires no boilerplate to generate a standalone executable. Building is as simple as issuing:
$ mlton your-program.sml
$ ./your-program
3628800
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With