在sml中扩展#

时间:2011-02-19 13:42:04

标签: sml

假设我在sml中有一个非常大的列表,那么sml会显示一些条目,然后开始显示#character。

有人能告诉我如何查看整个列表?

3 个答案:

答案 0 :(得分:11)

假设这是SML / NJ,您可以使用printLengthprintDepth和来自Control.Print结构的朋友。

以下是Control.Print结构文档的摘录:

printDepth
    The depth of nesting of recursive data structure at which ellipsis begins. 

printLength  
    The length of lists at which ellipsis begins. 

stringDepth
    The length of strings at which ellipsis begins. 

因此,例如,我们可以通过更改printLength引用

来更改我们不会在REPL中显示的列表元素数量
- Control.Print.printLength;
val it = ref 12 : int ref
- [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19];
val it = [1,2,3,4,5,6,7,8,9,10,11,12,...] : int list

- Control.Print.printLength := 18;
val it = () : unit
- [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19];
val it = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,...] : int list

- Control.Print.printLength := 100;
val it = () : unit
- [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19];
val it = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] : int list

请注意,对于字符串和数据结构,省略号将被写为散列“#”。例如,使用以下字符串可以看到这一点。请注意val it = ...行末尾的“#”,这是因为字符串的默认打印深度为70个字符:

- "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
val it = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusm#" : string

- Control.Print.stringDepth;
val it = ref 70 : int ref

最后,举例说明如何在嵌套数据结构中看到它:

- Control.Print.printDepth;
val it = ref 5 : int ref
- SOME (SOME (SOME (SOME (SOME (SOME (SOME 42))))));
val it = SOME (SOME (SOME (SOME (SOME #))))
  : int option option option option option option option

- Control.Print.printDepth := 10;
val it = () : unit
- SOME (SOME (SOME (SOME (SOME (SOME (SOME 42))))));
val it = SOME (SOME (SOME (SOME (SOME (SOME (SOME 42))))))
  : int option option option option option option option

这两个建议的解决方案无论多长时间都会打印整个列表。

答案 1 :(得分:2)

更短版本的Sabastian P.代码:

fun printList f ls =
    print ("[" ^ String.concatWith ", " (map f ls) ^ "]\n");

答案 2 :(得分:1)

你可以这样做:

(* Prints a list in its entirety.
 * ls is a list of type 'a list
 * f is a function that converts an 'a to string *)
fun printList f ls =
  let
    (* Prints the contents of the list neatly using f *)
    fun printContents []  = ()
      | printContents [x] = print (f x)
      | printContents (x::xs) = (print (f x ^ ", "); printContents xs)

    val _ = print "[";
    val _ = printContents ls;
    val _ = print "]\n"
    in
        ()
    end;

使用它的一个例子:

val ls = List.tabulate (1000, fn n => n);
printList Int.toString ls;

如果您想自动执行此操作,我怀疑您是否可以。如果我没记错的话,漂亮的打印机是特定于实现的,并且很可能不允许为多态类型安装漂亮的打印机。