Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mutual Recursion: Web pages CMSC 11500 Introduction to Computer Programming November 25, 2002.

Similar presentations


Presentation on theme: "Mutual Recursion: Web pages CMSC 11500 Introduction to Computer Programming November 25, 2002."— Presentation transcript:

1 Mutual Recursion: Web pages CMSC 11500 Introduction to Computer Programming November 25, 2002

2 Roadmap ● Recap: The Evaluator ● Mutual Recursion: – Definitions relying on other definitions – Example: Web pages ● Data definitions ● Example functions: count, find,...

3 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)

4 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 ???

5 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

6 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

7 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))....

8 Size ● Contract: – Size: w-p -> number ● Purpose: – Find number of words (symbols) in w-p

9 Size

10 Wp-to-file ● Contract: – Wp-to-file: w-p -> (listof symbol) ● Purpose: – Produce list of all symbols/header in w-p

11 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))))

12 Occurs ● Contract: – Occurs: w-p symbol -> boolean ● Purpose: – Return true if symbol in page (or embedded), false ow

13 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))))


Download ppt "Mutual Recursion: Web pages CMSC 11500 Introduction to Computer Programming November 25, 2002."

Similar presentations


Ads by Google