Ankit Shubham
Ankit Shubham

Reputation: 3599

Print int list in sml

Does there any function exist that directly prints the int list? I have to print int list for debugging purposes. I know that I can achieve this by writing my own functions but I want to know that is there any other method available?

Upvotes: 3

Views: 7602

Answers (3)

David Matthews
David Matthews

Reputation: 526

In Poly/ML there is a special function PolyML.print that will print most values using the appropriate pretty-print function. It's not part of Standard ML which is why it is in the PolyML structure. You may need to use a type-constraint if the function could be polymorphic.

> fun f (x: int list) = (PolyML.print x; ());
val f = fn: int list -> unit
> f [1,2,3,4];
[1, 2, 3, 4]
val it = (): unit

You can get fuller debugging information in Poly/ML by using the debugger. See http://www.polyml.org/documentation/Tutorials/Debugging.html.

Upvotes: 3

John Coleman
John Coleman

Reputation: 51998

SML/NJ doesn't have as many features for pretty printing as some other implementations of SML but its PRINTCONTROL signature gives some flexibility.

For example, with the default settings you have this:

enter image description here

But if in the REPL you evaluate

Control.Print.printLength := 500;

and

 Control.Print.linewidth := 80;

then:

enter image description here

Upvotes: 2

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

No, there's no built-in way to print anything other than strings in SML. You either write your own utilities or break down your functions into smaller components that can be tested separately within the REPL and then you'll get automatic pretty-printing of the return value.

If you want to build your own utilities, the MLton wiki has a page describing how to build a small library of combinators for printing most of the built-in types: http://mlton.org/TypeIndexedValues#_signature.

For lists, they get to the point where you can write:

val "[3, 1, 4]" =
  let open Show in show (list int) end [3, 1, 4]

Upvotes: 2

Related Questions