在 sml 中扩展 #

发布于 2024-10-18 04:47:08 字数 78 浏览 3 评论 0原文

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

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

Suppose I have a list in sml which is very big then sml shows a few of the entries and then starts showing # character.

Could someone tell me how could I view the whole list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚然 2024-10-25 04:47:08

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

以下是 控件文档的片段。 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

两个建议的解决方案将导致打印整个列表,无论它有多长。

Assuming this is SML/NJ you could use printLength, printDepth and friends from the Control.Print structure.

The following are a snippet from the documentation of the Control.Print structure:

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. 

Thus for example we can change how many elements of a list we wan't to be shown in the REPL, by changing the printLength reference

- 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

Note that for strings and data structures, the ellipsis is written as a hash '#' instead. This is for example seen with the below string. Note the '#' at the end of the val it = ... line, which is because the default print depth of strings are 70 characters:

- "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

And lastly, an example of how this is seen in nested data structures:

- 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

The two suggested solutions will of cause print the entire list no matter how long it is.

薄荷梦 2024-10-25 04:47:08

你可以做这样的事情:

(* 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;

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

You could do something like this:

(* 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;

An example of its use:

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

If you want to automatically do it, I doubt you can. If I recall correctly, the pretty printers are implementation specific, and most likely do not allow a pretty-printers to be installed for polymorphic types.

各自安好 2024-10-25 04:47:08

Sabastian P. 代码的简短版本:

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

Shorter version of Sabastian P.'s code:

fun printList f ls =
    print ("[" ^ String.concatWith ", " (map f ls) ^ "]\n");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文