5

I've never programmed in SML before, and I'm using SML/NJ. It keeps giving me the following at the end of each program I run:

val it = () : unit

What does this mean? Is it something I'm doing wrong?

Horse SMith
  • 1,003
  • 2
  • 12
  • 25

2 Answers2

4

it is the name of the result returned by your code. () : unit is a trivial placeholder value returned from things that are side-effect based.

It's more obvious when you enter something that's more commonly an expression at the prompt, e.g...

- 2 * 7;
  val it = 14 : int
Amber
  • 507,862
  • 82
  • 626
  • 550
  • Ah, so () is empty in SML? Perhaps like when you write a function to be able to be called without taking any arguments? – Horse SMith Oct 22 '13 at 22:50
  • 2
    @HorseSMith: `()` is the only value of the type `unit`, the unit type, a type that carries no information. All functions in ML take exactly one parameter. The unit type can be used as the parameter type and/or return type of a function that performs side effects and don't need to take or return (respectively) any information. – newacct Oct 24 '13 at 02:35
  • It should then be said that this `val it = () : unit` occurs in those situations when the interactive top-level is interested in some side-effect such as interpreting the content of a file and loading its effect into the top-level. – sshine Oct 25 '13 at 22:26
2

You can also use it for the side effect of printing things out:

fun printpos n = 
    if n <= 0 then (print "not positive!\n") 
    else (print (Int.toString n); print "\n");

 printpos ~1;
 printpos 1;

(* Output:
val printpos = fn : int -> unit
not positive!
val it = () : unit
1
val it = () : unit
*)