So I've got a datatype:
datatype ex = A of int | B of ex * ex;
and an example variable:
val x = (B (A 1, B (A 2, A 3)));
I want to be printing through it like this:
"(1, (2, 3))"
Any help would be appreciated! Thanks.
So I've got a datatype:
datatype ex = A of int | B of ex * ex;
and an example variable:
val x = (B (A 1, B (A 2, A 3)));
I want to be printing through it like this:
"(1, (2, 3))"
Any help would be appreciated! Thanks.
Assuming this pretty-printer is going to be a pp
: ex → string, here is a template:
fun pp (A i) = ...
| pp (B (ex1, ex2)) = ...
This template pattern matches against every possible value constructor for the type ex. The patterns go one level deep, so for your example value, ex1
is bound to A 2
, and ex2
is bound to A 3
in the first iteration of the call. You want to handle ex1
and ex2
recursively in the same way as you handled ex0
being B (A 1, B (A 2, A 3))
.
For concatenating strings, you might consider op ^
: string × string → string or String.concat
: string list → string:
- val test1 = "hello" ^ "world";
> val test1 = "helloworld" : string
- val test2 = String.concat [ "if ", "you've ", "got ", "many ", "strings!" ];
> val test2 = "if you've got many strings!" : string
For pretty-printing integers, you may consider Int.toString
: int → string. You may want to wrap this function, since Standard ML presents negative integers a little funky:
- val test3 = Int.toString ~5;
> val test3 = "~5" : string