abhi
abhi

Reputation: 59

Print to standard output in SML

datatype term = node of string*term list
         | vnode of string

I have a value of type term. How do I print it in SML to the standard output?

Upvotes: 3

Views: 1341

Answers (1)

sepp2k
sepp2k

Reputation: 370465

You need to first create a string out of the term and then print that using print. To turn a term into a string, you could define a function like this:

fun termToString (node (str, terms)) =
    "node(" ^ str ^ ", " ^ termListToString terms ^ ")"
  | termToString (vnode str) =
    "vnode(" ^ str ^ ")"
and termListToString terms =
    "[" ^ String.concatWith ", " (map termToString terms) ^ "]"

Upvotes: 3

Related Questions