Convert constructor name to string in OCaml

My code has the following type definitions:

type tag =
| Head
| Title
| Body
| H1
| P;;

type domtree =
| Empty
| Node of tag * string * domtree list;;

I need to print tags along with strings. But I could not find a way to convert the tag (constructor names in the definition of the first type) into strings and combine them with the string part of domtree. Is there any specific way to do this? Does OCaml provide a way to convert non-line types to strings? I found a similar question here, but I did not quite understand it.

+4
source share
1 answer

There is no such tool in OCaml, and you will need to write a conversion function tag_to_string : tag -> string.

, , sed :

sed -e 's/\| \(.*\)/| \1 -> "\1"/'

.

| Head -> "Head"
| Title -> "Title"
| Body -> "Body"
| H1 -> "H1"
| P;; -> "P;;"

;;.

, Emacs.

+5

All Articles