1
votes

I wanted to utilize the print function inside an SML program for sort of debugging purposes to print integer list type data, inside the function and during execution, e.g. inside a let block. However, as I saw, print can only print string type data. I cannot wait for the result to return to print what I want, because the function I created branches during execution and creates many different lists, and I want to see what is the resulting list at the end of each branch.

Therefore, is there a way to print a list inside of a function, as I would print a string?

2
I cannot wait for the result to return to print what I want so what do you want to do? you need to provide at least some demo code. - Jason Hu
Perhaps I wrote it without enough clarification, but I want to be able to print the list, which I have access to, inside a function, e.g. in a let block - Noob Doob
Edited the question. - Noob Doob
Edited the question. - Noob Doob
you need to implement the conversion from list of whatever to string, then print it. sml supports ; so you can print it first and return whatever result you need to return. - Jason Hu

2 Answers

2
votes

If it is an int list you can do something like this:

fun printIntList ints = app (fn i => print(Int.toString i ^" ")) ints;

Then printIntList [1,2,3] will print 1 2 3

You can do similar things for other types.

On edit: This is the best you can do with straight SML. SML/NJ has its own extensions including "access to compiler internals" and "user-customizable pretty printing" which sounds promising -- though I have little experience with their extensions to the standard library.

2
votes

Simple function for turning a list of ints into a string:

fun intlistToString []      = ""
  | intlistToString [x]     = Int.toString x
  | intlistToString (x::xs) = Int.toString x ^ ", " ^ intlistToString xs

Then you can use print (intlistToString myList) instead of print myList. It won't print the square brackets around the list, not without a little more code, but I'll leave that as an exercise because I'm lazy.