How to Print Variable in Sml?

How to Print Variable in Sml?

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.

0

1 Answer

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
13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.