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")))))
8 ;where s is a symbol and
9 ;ewp, wp are web-pages (WP).
10 ;(ewp, embedded web page)
13 ;fun-for-wp : WP -> ???
14 ;(define (fun-for-wp 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)) ...]))
21 ;Determine the size of a wp, that is,
22 ;how many symbols are in a-wp.
27 [(symbol? (first a-WP)) (+ 1 (size (rest a-WP)))]
28 [else (+ (size (first a-WP))
29 (size (rest a-WP)))]))
37 (define MRSWP '(h i m y n a m e i s
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)
56 ; [(symbol? (first a-WP))
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)
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)
77 [(symbol? (first a-WP))
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))]))
84 ;replace : symbol symbol WP -> WP
85 ;Replace old with new in a-WP and return
88 (define (replace new old a-WP)
91 [(symbol? (first a-WP))
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.
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
123 ;(define NESTEDWP2 '(l e v e l z e r o