Define collect_recursively function