Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding # in sml

Tags:

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?

like image 230
noddy Avatar asked Feb 19 '11 13:02

noddy


People also ask

What is expanding synonym?

Some common synonyms of expand are amplify, dilate, distend, inflate, and swell. While all these words mean "to increase in size or volume," expand may apply regardless of the manner of increase (such as growth, unfolding, addition of parts).

What does ever expanding mean?

ever-expanding adj (constantly becoming larger)

What is an example of expanding?

to increase in size, number, or importance, or to make something increase in this way: The air in the balloon expands when heated. They expanded their retail operations during the 1980s.

What does expanding mean in science?

Scientific definitions for expansion An increase in the volume of a substance while its mass remains the same. Expansion is usually due to heating. When substances are heated, the molecular bonds between their particles are weakened, and the particles move faster, causing the substance to expand.


3 Answers

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.

like image 127
Jesper.Reenberg Avatar answered Oct 21 '22 14:10

Jesper.Reenberg


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.

like image 21
Sebastian Paaske Tørholm Avatar answered Oct 21 '22 14:10

Sebastian Paaske Tørholm


Shorter version of Sabastian P.'s code:

fun printList f ls =
    print ("[" ^ String.concatWith ", " (map f ls) ^ "]\n");
like image 21
newacct Avatar answered Oct 21 '22 15:10

newacct