130 likes | 211 Views
CMSC 11500 Introduction to Computer Programming November 25, 2002. Mutual Recursion: Web pages. Roadmap. Recap: The Evaluator Mutual Recursion: Definitions relying on other definitions Example: Web pages Data definitions Example functions: count, find,. Recap: The Evaluator.
E N D
CMSC 11500 Introduction to Computer Programming November 25, 2002 Mutual Recursion:Web pages
Roadmap • Recap: The Evaluator • Mutual Recursion: • Definitions relying on other definitions • Example: Web pages • Data definitions • Example functions: count, find, ...
Recap: The Evaluator • Evaluate scheme code using scheme code • Subset of scheme: Numeric expressions & functs • Function definitions • Data definitions: s-exp & s-def • An s-exp is: • 1) number • 2) symbol • 3) (make-add s-exp s-exp) • 4) (make-mul s-exp s-exp) • 5) (make-app name s-exp)
Representing the Web • Web documents have: • Header and body • Body has • Words • (Links to) other documents • How can we model this? • (define-struct wp (header body)) • A web-page (w-p): • (make-wp h p) • Where h: symbol; p is ???
Representing the Web • Data definition: • A (Web) document is: • 1) '() • 2) (cons s p) • Where s: symbol; p: document • 3) (cons w p) • Where w: w-p; p: document • A Web-page (w-p) is: • (make-wp h p) • Where h: symbol; p: document
Mutually Recursive Functions • Data definitions cross-reference -> • Templates must cross-reference • Build simultaneously • Follow usual rules: • Condition for each clauses in def • Natural recursion for each self-ref • + cross-ref for each cross-ref
Functions for Web • Template: • (define (fn-for-doc doc) • (cond ((null? doc) ....) • ((symbol? (car doc)) • (...(car doc) ... (fn-for-doc (cdr doc)) • ((w-p? (car doc)) • (... (fn-for-wp (car doc))...(fn-for-doc (cdr doc))) • (define (fn-for-wp wp) • ...(wp-header wp) ...(fn-for-doc (wp-body wp))....
Size • Contract: • Size: w-p -> number • Purpose: • Find number of words (symbols) in w-p
Wp-to-file • Contract: • Wp-to-file: w-p -> (listof symbol) • Purpose: • Produce list of all symbols/header in w-p
Wp-to-file (define (wp-to-file wp) (doc-to-file (wp-body wp)) (define (doc-to-file doc) (cond ((null? doc) '()) ((symbol? (car doc)) (cons (car doc) (doc-to-file (cdr doc))) ((wp? (car doc)) (cons (wp-header (car doc)) (doc-to-file (cdr doc))))
Occurs • Contract: • Occurs: w-p symbol -> boolean • Purpose: • Return true if symbol in page (or embedded), false ow
Occurs (define (occurs wp word) (or (eq? (wp-header wp) word) (occurs-doc (wp-body wp) word)) (define (occurs-doc doc word) (cond ((null? doc) #f) ((symbol? (car doc)) (if (eq? (car doc) word) #t (occurs-doc (cdr doc) word))) ((wp? (car doc)) (or (occurs (car doc) word) (occurs-doc (cdr doc) word))))