Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #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")))))
4 ;A web-page (wp) is a structure:
5 ;(make-wp header doc) where
6 ;header is a symbol and doc is
7 ;a web-document.
8 ;
9 ;A web-document (doc) is either
10 ;1. empty,
11 ;2. (cons sym doc) where sym is a symbol and
12 ;doc is a web-document, or
13 ;3. (cons wp doc) where wp is a webpage and
14 ;doc is a web-document.
16 (define-struct wp (header doc))
18 ;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.
20 (define (find-for-wp a-wp a-symbol)
21 (cond
22 [(or (cons? (find-for-doc (wp-doc a-wp) a-symbol))
23 (empty? (find-for-doc (wp-doc a-wp) a-symbol))) (cons (wp-header a-wp) (find-for-doc (wp-doc a-wp) a-symbol))]
24 [else false]))
26 ;find-for-doc : doc symbol -> list-of-symbols or false
27 ;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.
29 (define (find-for-doc a-doc a-symbol)
30 (cond
31 [(empty? a-doc) false]
32 [(symbol? (first a-doc))
33 (cond
34 [(symbol=? a-symbol (first a-doc)) empty]
35 [else (find-for-doc (rest a-doc) a-symbol)])]
36 [(or (cons? (find-for-doc (rest a-doc) a-symbol))
37 (empty? (find-for-doc (rest a-doc) a-symbol))) (find-for-doc (rest a-doc) a-symbol)]
38 [(or (cons? (find-for-wp (first a-doc) a-symbol))
39 (empty? (find-for-wp (first a-doc) a-symbol))) (find-for-wp (first a-doc) a-symbol)]))
43 (define DOC7 (cons 'ImADoc empty))
44 (define DOC6 (cons 'ImADoc empty))
45 (define WP3 (make-wp 'ImAPage empty))
46 (define DOC5 (cons WP3 DOC6))
47 (define WP2 (make-wp 'AmIAPageToo? DOC5))
48 (define DOC4 (cons 'MaybeADoc DOC7))
49 (define DOC3 (cons WP2 DOC4))
50 (define DOC2 (cons 'ADoctor? DOC3))
51 (define DOC1 (cons 'OrADocument? DOC2))
52 (define WP1 (make-wp 'NoAWebPage DOC1))
54 (time (find-for-wp WP1 'ImADoc))
56 ;find : wp symbol -> list-of-symbols or false.
57 ;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.
59 (define (find a-wp a-symbol)
60 (local
61 ((define (find-for-wp a-wp a-symbol)
62 (local ((define r (find-for-doc (wp-doc a-wp) a-symbol)))
63 (cond
64 [(or (cons? r)
65 (empty? r)) (cons (wp-header a-wp) r)]
66 [else false])))
68 (define (find-for-doc a-doc a-symbol)
69 (local ((define firs (find-for-wp (first a-doc) a-symbol))
70 (define res (find-for-doc (rest a-doc) a-symbol)))
71 (cond
72 [(empty? a-doc) false]
73 [(symbol? (first a-doc))
74 (cond
75 [(symbol=? a-symbol (first a-doc)) empty]
76 [else res])]
77 [(wp? (first a-doc))
78 (cond
79 [(or (cons? res)
80 (empty? res)) res]
81 [(or (cons? firs)
82 (empty? firs)) firs])]))))
83 (find-for-wp a-wp a-symbol)))