Blame


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