Traverse a multiway tree


#1

hey guys, have no idea how to traverse multiway trees and build a list of strings
pastebin


#2

Very likely that you need to use a syntax extension which will generate a traversal function for you. https://github.com/ocaml-ppx/ppx_deriving has an iter plugin for it. I would start from there


#3

You may also be interested in the visitors ppx.


#4

I am not sure if recommanding code generation tools is a good answer here. Sure, writing the traversal code is boring, but it is also quite straightforward. In my opinion it is far better to first understand how to write such traversal function before moving to generating tools.

@locaml, what did you try? If you are completely lost, a first good step since html_tree is a variant is to pattern match the html_tree argument in display_html.


#5

Exactly my thoughts.


#6

Thanks for your advice! check it pls pastebin


#7

This is a good first next. The next two steps are:

  • First, determine the result of display_html for the base case Word s and Empty. If you wanted to map these two case to a list of string, what will you do ? (hint: you should build a list here)
  • Second, the function display_html_list should not look at the content of the element of the list: this is the job of display_html. The job of display_html_list is to patch together the list created by display_html. You should remove the load function and start by pattern matching the list (without extending the individual elements).