(define-struct dir (name contents)) ; Clearly don't need to recur on the natural number. ;(define (g a-dir n) ; ...(dir-name a-dir)... ; ...(f (dir-contents a-dir) n)...) ; dir-files-at-depth : dir nat -> lof (define (dir-files-at-depth a-dir n) (lofd-files-at-depth (dir-contents a-dir) n)) ;(define (f a-lofd n) ; (cond [(and (empty? a-lofd) ; (zero? n)) ; ...] ; [(and (empty? a-lofd) ; (positive? n)) ; ...(f a-lofd (sub1 n))...] ; [(and (cons? a-lofd) (symbol? (first a-lofd)) ; (zero? n)) ; ...(first a-lofd)... ; ...(f (rest a-lofd) n)...] ; [(and (cons? a-lofd) (symbol? (first a-lofd)) ; (positive? n)) ; ...(first a-lofd)... ; ...(f (rest a-lofd) n)...] ; ...(f a-lofd (sub1 n))...] ; ...(f (rest a-lofd) (sub1 n))...] ; [(and (cons? a-lofd) (dir? (first a-lofd)) ; (zero? n)) ; ...(g (first a-lofd))... ; ...(f (rest a-lofd) n)...] ; [(and (cons? a-lofd) (dir? (first a-lofd)) ; (positive? n)) ; ...(g (first a-lofd))... ; ...(f (rest a-lofd) n)...] ; ...(f a-lofd (sub1 n))...] ; ...(f (rest a-lofd) (sub1 n))...])) ; lofd-files-at-depth : lofd nat -> lof ; The first two cases of the template have been combined. (define (lofd-files-at-depth a-lofd n) (cond [(empty? lofd) empty] [(and (cons? lofd) (symbol? (first lofd)) (zero? n)) (cons (first lofd) (lofd-files-at-depth (rest lofd) n)))] [(and (cons? lofd) (symbol? (first lofd)) (positive? n)) (lofd-files-at-depth (rest lofd) n)] [(and (cons? lofd) (dir? (first lofd)) (zero? n)) (lofd-files-at-depth (rest lofd) n)] [(and (cons? lofd) (dir? (first lofd)) (positive? n)) (append (dir-files-at-depth (first lofd) (sub1 n)) (lofd-files-at-depth (rest lofd) n))]))