htdp 16.3.4

(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文件的元素进行提取生成新的列表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值