Search code examples
smlsmlnj

I want to make function maptree with standard ML


I want to make function maptree with standard ML. If function f(x) = x + 1; then

maptree(f, NODE(NODE(LEAF 1,LEAF 2),LEAF 3));

should make result

NODE(NODE(LEAF 2,LEAF 3),LEAF 4))

I write the code like below.

datatype 'a tree = LEAF of 'a | NODE of 'a tree * 'a tree;
fun f(x) = x + 1;
fun maptree(f, NODE(X, Y)) = NODE(maptree(f, X), maptree(f, Y))
| maptree(f, LEAF(X)) = LEAF(f X);

but when I execute this code like this

maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3)));

result is not I want to (NODE(NODE(LEAF 2,LEAF 3),LEAF 4))) but NODE(NODE(LEAF #,LEAF #),LEAF 4)). Why this happened(not a number but #)?


Solution

  • # is used by the REPL when the data structure it prints is deeper than a pre-set value. If you increase that value, you'll get the result you excepted. I assume you're using SML/NJ, which calls that setting print.depth:

    sml -Cprint.depth=20
    - maptree(f, (NODE(NODE(LEAF 1,LEAF 2),LEAF 3)));
    val it = NODE (NODE (LEAF 2,LEAF 3),LEAF 4) : int tree
    

    You can find more options like these by executing sml -H. Look them up under the "compiler print settings" section:

    compiler print settings:
         print.depth                                          (max print depth)
         print.length                                        (max print length)
         print.string-depth                            (max string print depth)
         print.intinf-depth                        (max IntInf.int print depth)
         print.loop                                                (print loop)
         print.signatures                       (max signature expansion depth)
         print.opens                                             (print `open')
         print.linewidth                   (line-width hint for pretty printer)