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 15.3.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.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))
17 ;
18 ;Template
19 ;fun-for-wp : wp -> ???
20 ;(define (fun-for-wp a-wp)
21 ; ... (wp-header a-wp) ...
22 ; ... (fun-for-doc (wp-doc a-wp)) ...)
23 ;
24 ;fun-for-doc : doc -> ???
25 ;(define (fun-for-doc a-doc)
26 ; (cond
27 ; [(empty? a-doc) ...]
28 ; [(symbol? (first a-doc))
29 ; ... (first a-doc) ...
30 ; ... (fun-for-doc (rest a-doc)) ...]
31 ; [(wp? (first a-doc))
32 ; ... (fun-for-wp (first a-doc)) ...
33 ; ... (fun-for-doc (rest a-doc)) ...]))
34 ;
36 ;size : wp -> number
37 ;Given a-wp, determine the number of
38 ;symbols it contains.
40 (define (size a-wp)
41 (+ 1
42 (size-doc (wp-doc a-wp))))
43 ;
44 ;size-doc : doc -> number
45 ;Given a-doc, determine the size
46 ;of the doc, which is the number
47 ;of symbols it contains.
48 ;
49 (define (size-doc a-doc)
50 (cond
51 [(empty? a-doc) 0]
52 [(symbol? (first a-doc)) (+ 1
53 (size-doc (rest a-doc)))]
54 [(wp? (first a-doc)) (+ (size (first a-doc))
55 (size-doc (rest a-doc)))]))
56 ;Tests
57 ;
58 (define DOC7 (cons 'ImADoc empty))
59 (define DOC6 (cons 'ImADocToo empty))
60 (define WP3 (make-wp 'ImAPage empty))
61 (define DOC5 (cons WP3 DOC6))
62 (define WP2 (make-wp 'AmIAPageToo? DOC5))
63 (define DOC4 (cons 'MaybeADoc DOC7))
64 (define DOC3 (cons WP2 DOC4))
65 (define DOC2 (cons 'ADoctor? DOC3))
66 (define DOC1 (cons 'OrADocument? DOC2))
67 (define WP1 (make-wp 'NoAWebPage DOC1))
68 ;
69 ;(size WP1)
70 ;8
72 ;wp-to-file : wp -> list-of-symbols
73 ;Given a-wp, produces a list-of-symbols containing
74 ;all the symbols in the headers of all web pages
75 ;and all the symbols within the body of a-wp,
76 ;excluding the bodies of immediately embedded
77 ;webpages.
78 ;
79 ;Examples:
81 (define WP-6 (make-wp 'ThisIsARepeat empty))
82 (define DOC-8 (cons WP-6 empty))
83 (define DOC-7 (cons 'ThisIsARepeat empty))
84 (define WP-5 (make-wp 'RepetionSuccess DOC-8))
85 (define DOC-6 (cons WP-5 DOC-7))
86 (define WP-4 (make-wp 'ThisIsARepeat DOC-6))
87 (define DOC-5.5 (cons WP-4 empty))
88 (define DOC-5 (cons 'NotIncluded DOC-5.5))
89 (define DOC-4 (cons 'ThisIsAnExample empty))
90 (define WP-3 (make-wp 'MyHeader empty))
91 (define DOC-2 (cons WP-3 DOC-4))
92 (define WP-2 (make-wp 'ThisIsAHeader DOC-5))
93 (define DOC-1 (cons WP-2 DOC-2))
94 (define WP-1 (make-wp 'TheHighestWP DOC-1))
96 ;(wp-to-file WP-1)
97 ;'(TheHighestWP ThisIsAHeader MyHeader ThisIsAnExample))
98 ;Notice that 'NotIncluded does not show up because
99 ;it is the body of an embedded web-page.
101 ;wp-to-file : wp -> list-of-symbols
102 (define (wp-to-file a-wp)
103 (append (list (wp-header a-wp))
104 (wp-to-file-for-doc (wp-doc a-wp))))
106 ;wp-to-file-for-doc : doc -> list-of-symbols
107 ;Given a-doc, produces a list-of-symbols containing all the
108 ;symbols found within the body of the document as well
109 ;as the headers of embedded web pages. It does not
110 ;return the symbols of the bodies of the embedded
111 ;web pages, however.
113 (define (wp-to-file-for-doc a-doc)
114 (cond
115 [(empty? a-doc) empty]
116 [(symbol? (first a-doc))
117 (append (list (first a-doc))
118 (wp-to-file-for-doc (rest a-doc)))]
119 [(wp? (first a-doc))
120 (append (list (wp-header (first a-doc)))
121 (wp-to-file-for-doc (rest a-doc)))]))
124 ;occurs : symbol wp -> boolean
125 ;Given a-symbol and a-wp, determine if
126 ;a-symbol appears anywhere in the webpage,
127 ;including in embedded webpages.
129 (define (occurs a-symbol a-wp)
130 (cond
131 [(symbol=? a-symbol (wp-header a-wp)) true]
132 [else (occurs-doc a-symbol (wp-doc a-wp))]))
134 ;occurs-doc : symbol doc -> boolean
135 ;Given a-symbol and a-doc, determine if
136 ;a-symbol is anywhere within a-doc, including
137 ;in embedded web pages.
139 (define (occurs-doc a-symbol a-doc)
140 (cond
141 [(empty? a-doc) false]
142 [(symbol? (first a-doc))
143 (cond
144 [(symbol=? a-symbol (first a-doc)) true]
145 [else (occurs-doc a-symbol (rest a-doc))])]
146 [(wp? (first a-doc))
147 (or
148 (occurs a-symbol (first a-doc))
149 (occurs-doc a-symbol (rest a-doc)))]))
151 ;Test
152 ;(wp-to-file WP-1)
154 ;find-for-wp : wp symbol -> list-of-symbols/false
155 ;Given a-wp and a-symbol, returns a list-of-symbols containing
156 ;all the headers encountered on the way to find the symbol.
157 ;Returns empty if the symbol does not occur.
159 ;find-for-wp : wp symbol -> list-of-symbols/false
160 (define (find-for-wp a-wp a-symbol)
161 (cond
162 [(occurs a-symbol a-wp) (append (list (wp-header a-wp))
163 (find-for-doc (wp-doc a-wp) a-symbol))]
164 [else empty]))
166 ;find-for-doc : doc -> list-of-symbols/false
167 ;Given a-doc and a-symbol, returns a list-of-symbols containing
168 ;all the headers encountered on the way to find a symbol.
169 ;Returns empty if the symbol does not occur.
171 (define (find-for-doc a-doc a-symbol)
172 (cond
173 [(empty? a-doc) empty]
174 [(symbol? (first a-doc))
175 (find-for-doc (rest a-doc) a-symbol)]
176 [(wp? (first a-doc)) (append (find-for-wp (first a-doc) a-symbol)
177 (find-for-doc (rest a-doc) a-symbol))]))
179 ;find : wp symbol -> list-of-symbols/false
180 ;Given a-wp and a-symbol, return the headers
181 ;of the web-pages that are accessed on the way
182 ;to locating a-symbol. Returns false
183 ;if a-symbol never occurs.
185 (define (find a-wp a-symbol)
186 (cond
187 [(empty? (find-for-wp a-wp a-symbol)) false]
188 [else (find-for-wp a-wp a-symbol)]))