tags:

views:

485

answers:

1

I'm just starting out with SML, and I'm trying to modify some code so I understand what it's doing (I can't find a decent SML/NJ debugger, but that's a separate question).

fun type_check e theta env non_gens = 
  case e of
   constant_int _ => (integer,theta) 
  | 
   constant_bool _ => (boolean,theta) 
  | ...

Assume this is valid code in that constant_int is part of a datatype already declared, etc. How do I add a print statement to say 'Returning "integer"' to the case? I tried:

...
constant_int _ => (print "returning integer") (integer,theta)
...

But I get:

    stdIn:167.22-167.65 Error: operator is not a function [tycon mismatch]
      operator: unit
      in expression:
        (print "returning integer") (integer,theta)

I think I'm just not understanding how to execute a sequence of statements, only the last of which should be treated as the return value. Any advice?

Also, how would I print my own datatype value? SML didn't like when I tried to pass a datatype value into print() so I probably have to create a new print function for each datatype, is that correct?

+2  A: 

You can "execute a sequence of statements, only the last of which should be treated as the return value" by using semicolons.

...
constant_int _ => ( print "returning integer"; (integer,theta) )
...

In this case, we needed to wrap the sequence of statements in parentheses, because the semicolon has lower precedence than the case-expression, and so if we didn't have the parentheses, it would think that the case-expression ended at the semicolon, and that the entire case-expression is a part of the sequence, which is not what you want in this case.

newacct