(define (find sym tree)
(cond
[(empty? (dir-files tree))
(solution3? empty (dir-dirs tree) sym)]
[else
(solution3? (dir-files tree) (dir-dirs tree) sym)]))
(define (solution3? list1 list2 sym)
(cond
[(and (empty? list1)
(empty? list2)) false]
[(empty? list1)
(or (find? sym (first list2))
(solution3? list1 (rest list2) sym))]
[(empty? list2)
(or (symbol=? sym (file-name (first list1)))
(solution3? (rest list1) list2 sym))]
[(file? (first list1))
(or (symbol=? sym (file-name (first list1)))
(solution3? (rest list1) list2 sym))]
[(dir? (first list2))
(or (find? sym (first list2))
(solution3? list1 (rest list2) sym))]))
Figure 1:判断文件在不在目录里
(define (subdirectory tree)
(cond
[(empty? (dir-dirs tree)) empty]
[else
(cons (dir-dirs tree)
(append (answer? (first (dir-dirs tree)))
(subdirectory (first (rest (dir-dirs tree))))))]))
(define (answer? tree)
(cond
[(empty? tree) empty]
[else (subdirectory tree)]))
(define (all tree)
(append (list ts)
(subdirectory tree)))
(define doc (all ts))
Figure 2:生成所有目录的列表
格式: ‘(ts (text libs) (code docs))
(define (where sym list)
(cond
[(empty? list) empty]
[(and (dir? (first list))
(find sym (first list)))
(cons (first list)
(where sym (rest list)))]
[(cons? (first list))
(append (where? (first list) sym) ;;这一块是想将'((text libs) (code docs))都处理掉
(where? (rest list) sym))]
[else (where sym (rest list))]))
Figure 3:生成指定文件的路径
当然要生成find?函数,需要更高一级的抽象(比较简单,我就不贴了)
思路:
dir包括name,files,dirs三个部分,files,dirs都是列表,purpose要求生成目标路径,更多的是对
dirs的处理。
观察Figure44,整个流程先处理files,再处理dirs
而且可能遇到有两个子目录的情况(可能更多),因为随机性,你要都遍历一遍。在遍历前先判断要走哪一条路(利用之前的find函数,对子目录也适用)。
对Figure 2生成的doc文件的元素进行提取生成新的列表