首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在sml中扩展#

在sml中扩展#
EN

Stack Overflow用户
提问于 2011-02-19 21:42:04
回答 3查看 3.4K关注 0票数 8

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-02-22 22:29:25

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

以下是Control.Print结构文档中的一段代码:

代码语言:javascript
复制
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引用来更改列表中不想显示的元素的数量

代码语言:javascript
复制
- 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个字符:

代码语言:javascript
复制
- "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

最后,一个如何在嵌套数据结构中看到这一点的示例:

代码语言:javascript
复制
- 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

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

票数 12
EN

Stack Overflow用户

发布于 2011-02-19 22:01:20

你可以这样做:

代码语言:javascript
复制
(* 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;

它的用法示例如下:

代码语言:javascript
复制
val ls = List.tabulate (1000, fn n => n);
printList Int.toString ls;

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

票数 2
EN

Stack Overflow用户

发布于 2011-02-22 15:43:37

萨巴斯蒂安·P的代码的简短版本:

代码语言:javascript
复制
fun printList f ls =
    print ("[" ^ String.concatWith ", " (map f ls) ^ "]\n");
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5051081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档