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 14.4.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 is either
5 ;1. empty or
6 ;2. (cons s wp)
7 ;3. (cons ewp wp)
8 ;where s is a symbol and
9 ;ewp, wp are web-pages (WP).
10 ;(ewp, embedded web page)
11 ;
12 ;Template
13 ;fun-for-wp : WP -> ???
14 ;(define (fun-for-wp a-WP)
15 ; (cond
16 ; [(empty? a-WP) ...]
17 ; [(symbol? (first a-WP)) ... (first a-WP) (fun-for-wp (rest a-WP)) ...]
18 ; [else ... (first a-WP) (fun-for-wp (rest a-WP)) ...]))
19 ;
20 ;size : wp -> number
21 ;Determine the size of a wp, that is,
22 ;how many symbols are in a-wp.
24 (define (size a-WP)
25 (cond
26 [(empty? a-WP) 0]
27 [(symbol? (first a-WP)) (+ 1 (size (rest a-WP)))]
28 [else (+ (size (first a-WP))
29 (size (rest a-WP)))]))
31 (define MRWP '((h i)
32 (m y)
33 (n a m e)
34 (i s)
35 (j o e)))
37 (define MRSWP '(h i m y n a m e i s
38 (s a l l y)
39 (r i d e)
40 a n d i a m
41 (a n)
42 (a s t r o)
43 n a u t))
45 ;occurs1 : WP symbol -> number
46 ;Given a-WP and a-symbol, determines
47 ;the number of occurrences of a-symbol
48 ;in a-WP ignoring embedded webpages
49 ;(that is, do not search ewp for
50 ;webpages of the form (cons ewp wp)).
52 ;occurs1 : WP symbol -> number
53 ;(define (occurs1 a-WP a-symbol)
54 ; (cond
55 ; [(empty? a-WP) 0]
56 ; [(symbol? (first a-WP))
57 ; (cond
58 ; [(symbol=? (first a-WP) a-symbol) (+ 1 (occurs1 (rest a-WP) a-symbol))]
59 ; [else (occurs1 (rest a-WP) a-symbol)])]
60 ; [else (occurs1 (rest a-WP) a-symbol)]))
62 (define (occurs1 a-WP a-symbol)
63 (cond
64 [(empty? a-WP) 0]
65 [(and (symbol? (first a-WP))
66 (symbol=? (first a-WP) a-symbol)) (+ 1 (occurs1 (rest a-WP) a-symbol))]
67 [else (occurs1 (rest a-WP) a-symbol)]))
69 ;occurs2 : WP symbol -> number
70 ;Determines the number of times
71 ;a-symbol occurs in a-WP, counting
72 ;embedded web pages (ewp).
74 (define (occurs2 a-WP a-symbol)
75 (cond
76 [(empty? a-WP) 0]
77 [(symbol? (first a-WP))
78 (cond
79 [(symbol=? (first a-WP) a-symbol) (+ 1 (occurs2 (rest a-WP) a-symbol))]
80 [else (occurs2 (rest a-WP) a-symbol)])]
81 [else (+ (occurs2 (first a-WP) a-symbol)
82 (occurs2 (rest a-WP) a-symbol))]))
83 ;
84 ;replace : symbol symbol WP -> WP
85 ;Replace old with new in a-WP and return
86 ;the new WP.
88 (define (replace new old a-WP)
89 (cond
90 [(empty? a-WP) empty]
91 [(symbol? (first a-WP))
92 (cond
93 [(symbol=? (first a-WP) old) (cons new
94 (replace new old (rest a-WP)))]
95 [else (cons (first a-WP)
96 (replace new old (rest a-WP)))])]
97 [else (cons (replace new old (first a-WP))
98 (replace new old (rest a-WP)))]))
100 ;depth : WP -> number
101 ;Calculates the depth of a-WP, which
102 ;is the number of immediately embedded web pages.
103 ;If there are several immediately embedded webpages,
104 ;we return the maximum of the depths of
105 ;the embedded webpages plus 1 to account for the
106 ;embedding of the first webpage.
108 (define (depth a-WP)
109 (cond
110 [(empty? a-WP) 0]
111 [(symbol? (first a-WP)) (depth (rest a-WP))]
112 [else (max (+ 1 (depth (first a-WP)))
113 (depth (rest a-WP)))]))
115 ;(define NESTEDWP1 '(l e v e l z e r o
116 ; (o n e)
117 ; (o n e)
118 ; (o n e)
119 ; (o n e
120 ; (t w o
121 ; (t h r e e))
122 ; (t w o))))
123 ;(define NESTEDWP2 '(l e v e l z e r o
124 ; (o n e
125 ; (t w o
126 ; (t h r e e
127 ; (f o u r))))
128 ; (o n e
129 ; (t w o
130 ; (t h r e e
131 ; (f o u r
132 ; (f i v e
133 ; (s i x
134 ; (s e v e n)))))))
135 ; (o n e
136 ; (t w o
137 ; (t h r e e
138 ; (f o u r
139 ; (f i v e
140 ; (s i x))))))))