Print int list in sml

时光毁灭记忆、已成空白 提交于 2019-12-06 15:46:12

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:

But if in the REPL you evaluate

Control.Print.printLength := 500;

and

 Control.Print.linewidth := 80;

then:

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.

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]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!