Extension # in sml

Suppose I have a list in sml that is very large, then sml shows some entries and then starts showing # character.

Can someone tell me how can I view the whole list?

+6
sml
source share
3 answers

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

The following is a snippet from the documentation for 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 the number of list items that we will not show in REPL by changing the printLength link

 - 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, ellipsis is written as a hash of '#'. This, for example, can be seen with the line below. Note the "#" at the end of the line val it = ... because the default print depth is 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 finally, 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 

Two proposed solutions will print the entire list no matter how long it takes.

+10
source share

Shorter version of Sabastian P code:

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

You can 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 (fx) | printContents (x::xs) = (print (fx ^ ", "); printContents xs) val _ = print "["; val _ = printContents ls; val _ = print "]\n" in () end; 

Usage example:

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

If you want to do this automatically, I doubt you can. If I remember correctly, cute printers are implementation specific, and most likely do not allow pretty printers to be installed for polymorphic types.

+1
source share

All Articles