;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 18.1.14-3) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp"))))) ;A web-page (wp) is a structure: ;(make-wp header doc) where ;header is a symbol and doc is ;a web-document. ; ;A web-document (doc) is either ;1. empty, ;2. (cons sym doc) where sym is a symbol and ;doc is a web-document, or ;3. (cons wp doc) where wp is a webpage and ;doc is a web-document. (define-struct wp (header doc)) ;find-for-wp : wp symbol -> list-of-symbols or false. Given a-wp and a-symbol, return the headers of the web-pages that are accessed on the way to locating a-symbol within documents within a-wp. Returns false if a-symbol never occurs. If there are 2 or more occurrences of a-symbol, find-for-wp returns a list-of-headers from only a single occurrence by following the path that gives preference to documents before embedded web pages. (define (find-for-wp a-wp a-symbol) (cond [(or (cons? (find-for-doc (wp-doc a-wp) a-symbol)) (empty? (find-for-doc (wp-doc a-wp) a-symbol))) (cons (wp-header a-wp) (find-for-doc (wp-doc a-wp) a-symbol))] [else false])) ;find-for-doc : doc symbol -> list-of-symbols or false ;Given a-doc and a-symbol, return the headers of the web-pages that are accessed on the way to locating a-symbol within documents within a-doc (a-doc inclusive). Returns false if a-symbol never occurs. Return empty if a-doc contains a-symbol. If there are 2 or more occurrences of a-symbol, find-for-doc returns the list-of-headers from only a single occurrence by following the path that gives preference to documents before embedded web pages. (define (find-for-doc a-doc a-symbol) (cond [(empty? a-doc) false] [else (local ((define firs ) (define res )) (cond [(symbol? (first a-doc)) (cond [(symbol=? a-symbol (first a-doc)) empty] [else (find-for-doc (rest a-doc) a-symbol)])] [(or (cons? (find-for-doc (rest a-doc) a-symbol)) (empty? (find-for-doc (rest a-doc) a-symbol))) (find-for-doc (rest a-doc) a-symbol)] [(or (cons? (find-for-wp (first a-doc) a-symbol)) (empty? (find-for-wp (first a-doc) a-symbol))) (find-for-wp (first a-doc) a-symbol)]))])) (define DOC7 (cons 'ImADoc empty)) (define DOC6 (cons 'ImADoc empty)) (define WP3 (make-wp 'ImAPage empty)) (define DOC5 (cons WP3 DOC6)) (define WP2 (make-wp 'AmIAPageToo? DOC5)) (define DOC4 (cons 'MaybeADoc DOC7)) (define DOC3 (cons WP2 DOC4)) (define DOC2 (cons 'ADoctor? DOC3)) (define DOC1 (cons 'OrADocument? DOC2)) (define WP1 (make-wp 'NoAWebPage DOC1)) (find-for-wp WP1 'ImADoc) ;find : wp symbol -> list-of-symbols or false. ;Given a-wp and a-symbol, return the headers of the web-pages that are accessed on the way to locating a-symbol within documents within a-wp. Returns false if a-symbol never occurs. If there are 2 or more occurrences of a-symbol, find-for-wp returns a list-of-headers from only a single occurrence by following the path that gives preference to documents before embedded web pages. (define (find a-wp a-symbol) (local ((define (find-for-wp a-wp a-symbol) (local ((define r (find-for-doc (wp-doc a-wp) a-symbol))) (cond [(or (cons? r) (empty? r)) (cons (wp-header a-wp) r)] [else false]))) (define (find-for-doc a-doc a-symbol) (local ((define firs (find-for-wp (first a-doc) a-symbol)) (define res (find-for-doc (rest a-doc) a-symbol))) (cond [(empty? a-doc) false] [(symbol? (first a-doc)) (cond [(symbol=? a-symbol (first a-doc)) empty] [else res])] [(wp? (first a-doc)) (cond [(or (cons? res) (empty? res)) res] [(or (cons? firs) (empty? firs)) firs])])))) (find-for-wp a-wp a-symbol)))