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

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
Section 4.2: Functions that Test Conditions (continued)
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Lists of Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
ISBN Chapter 15 Functional Programming Languages.
Data Structures: Binary Trees CMSC Introduction to Computer Programming October 28, 2002.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
Information Retrieval: aka “Google-lite” CMSC November 27, 2006.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
Meta-Circular Evaluation CMSC Introduction to Computer Programming November 20, 2002.
CS314 – Section 5 Recitation 10
Data Abstraction: Sets
Additional Scheme examples
Functional Programming Languages
History of Computing – Lisp
Edited by Original material by Eric Grimson
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Racket CSC270 Pepper major portions credited to
Chapter 15 – Functional Programming Languages
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
The Boolean (logical) data type boolean
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
Let's Play "What's the Question"
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
To understand recursion, one must first understand recursion.
Changing Data: (Continued)
Lisp.
Bringing it all Together: Family Trees
Presentation transcript:

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

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

Size

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