How to print variable in sml?

user4348719 picture user4348719 · Feb 14, 2015 · Viewed 23.2k times · Source

I'm new to SML and in debugging I usually print out the variable to see if it matches up to what I expected it should be. I would like to print out a variable within a function
what i have is this :
function header..

let val prod 
 val prod = c + x * y;
in 
 (print "product "; prod mod 10):: (multiplyAux (prod div 10) xs y)
end;


Right now its printing the string product, but I would like to be able to print the variable prod itself.

Answer

ben rudgers picture ben rudgers · Feb 14, 2015

The only thing that print can print is strings. So to print a numeric value, it must first be converted to a string. For example:

- print("product " ^ (Int.toString (43 mod 5)) ^ "\n");
product 3
val it = () : unit

Note that Int.toString uses the curried function syntax (i.e. it does not require a tuple for it's argument) so the parenthesis around 43 mod 5 are to make the value clear not to make Int.toString work.

- Int.toString 5;
val it = "5" : string