假设我在sml中有一个非常大的列表,然后sml显示了一些条目,然后开始显示# character。
有人能告诉我如何查看整个列表吗?
发布于 2011-02-22 22:29:25
假设这是SML/NJ,你可以使用Control.Print结构中的printLength,printDepth和朋友。
以下是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引用来更改列表中不想显示的元素的数量
- 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这两个建议的解决方案将导致打印整个列表,无论它有多长。
发布于 2011-02-19 22:01:20
你可以这样做:
(* 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;如果你想自动做到这一点,我怀疑你能做到。如果我没记错的话,漂亮的打印机是特定于实现的,并且很可能不允许为多态类型安装漂亮的打印机。
发布于 2011-02-22 15:43:37
萨巴斯蒂安·P的代码的简短版本:
fun printList f ls =
print ("[" ^ String.concatWith ", " (map f ls) ^ "]\n");https://stackoverflow.com/questions/5051081
复制相似问题