<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def walk_dir(dir_path, walk_after, recurse=None, archive_mtime=None):
 """Recursively optimize a directory."""

    if recurse is None:
        recurse = Settings.recurse

    result_set = set()
    if not recurse:
        return result_set

    for root, _, filenames in os.walk(dir_path):
        for filename in filenames:
            filename_full = os.path.join(root, filename)
            try:
                results = walk_file(filename_full, walk_after, recurse,
                                    archive_mtime)
                result_set = result_set.union(results)
            except Exception:
                print("Error with file: {}".format(filename_full))
                raise

    return result_set