Search code examples
listprintingsmlsmlnj

SML/NJ - Print a list mid-execution


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?


Solution

  • 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.