Recursive Data and Recursive Functions

Similar documents
CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Programming Languages 3. Definition and Proof by Induction

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

1. Let n be a positive number. a. When we divide a decimal number, n, by 10, how are the numeral and the quotient related?

Lecture 5: Implementing Lists, Version 1

Intro. Scheme Basics. scm> 5 5. scm>

by the evening of Tuesday, Feb 6

Lecture 6: Sequential Sorting

User-defined Functions. Conditional Expressions in Scheme

SCHEME AND CALCULATOR 5b

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

PROBLEM SOLVING 11. July 24, 2012

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

6.001 Notes: Section 17.5

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CMSC 330: Organization of Programming Languages. Operational Semantics

, has the form T i1i 2 i m. = κ i1i 2 i m. x i1. 1 xi2 2 xim m (2)

Properties and Definitions

Subclassing for ADTs Implementation

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

Putting the fun in functional programming

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

APCS-AB: Java. Recursion in Java December 12, week14 1

CS450 - Structure of Higher Level Languages

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

MITOCW watch?v=kz7jjltq9r4

2. CONNECTIVITY Connectivity

COPYRIGHTED MATERIAL. An Introduction to Computers That Will Actually Help You in Life. Chapter 1. Memory: Not Exactly 0s and 1s. Memory Organization

Reasoning about programs. Chapter 9 of Thompson

Assignment #1 Simple C++

AXIOMS FOR THE INTEGERS

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Problem Set 4: Streams and Lazy Evaluation

Discussion 11. Streams

6.001 Notes: Section 8.1

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

Handout 9: Imperative Programs and State

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSCI 3155: Homework Assignment 3

Reasoning About Programs Panagiotis Manolios

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Linked lists. A linked list is considered a recursive data structure because it has a recursive definition.

15-122: Principles of Imperative Computation, Fall 2015

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Chapter 10 Recursion

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Lists. Michael P. Fourman. February 2, 2010

Maps and Binary Search

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

Week 5 Tutorial Structural Induction

Skill 1: Multiplying Polynomials

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Neo4J: Graph Database

Program Calculus Calculational Programming

Basics of Computational Geometry

16 Multiple Inheritance and Extending ADTs

Lecture 3: Recursion; Structural Induction

CS 170 Java Programming 1. Week 10: Loops and Arrays

CS 61B Discussion 5: Inheritance II Fall 2014

Figure 4.1: The evolution of a rooted tree.

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

P1 Engineering Computation

Repetition Through Recursion

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Abstract Data Types. Lecture notes accompanying COL106 (Data Structures), Semester I, , IIT Delhi

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

CS 211 Programming Practicum Fall 2018

APCS Semester #1 Final Exam Practice Problems

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

STUDENT LESSON A12 Iterations

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Lecture 1: Overview

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Table of Laplace Transforms

A Small Interpreted Language

Type Checking and Type Equality

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Functional Programming in Haskell for A level teachers

Lecture 7: Implementing Lists, Version 2

Iteration Preview of Coming Attractions The Need to Get Your Act Together A Process for Developing While Statements

6.005 Elements of Software Construction Fall 2008

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Chapter 3. Set Theory. 3.1 What is a Set?

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

Transcription:

Structural Recursion and Induction Of all the material in this course, this lesson is probably the hardest for students to grasp at first, but it s also one of the most fundamental. Once you understand it, everything else in the course becomes much easier, so we want you to play around with these ideas as much as you can. Come see us if you re having trouble; we really want this to make sense to you. Objectives At the end of this lesson, you should be able to do the following: 1. Implement recursively defined data structures. 2. Write structurally recursive functions that may require a helper function or an additional parameter. 3. Explain what structural induction is and why it is not a circular argument. 4. Use structural induction to prove the correctness of basic recursive programs over recursive data. The exercises and problems included in this handout should help you learn this material. The problems are due at the beginning of class on Monday, July 1, along with the mathematical induction and recursion problems of the next handout, so don t wait until we ve finished all of the material before starting! Using DrJava At least for this part of the course, we ll be using a simple development environment called DrJava because it s very easy to install and it makes it possible to interactively experiment with our programs without having to write lots of skeleton code. The environment is being developed actively, and no documentation exists yet, but fortunately it s already quite usable for our purposes. Grab the latest copy from http://drjava.sf.net/. A walkthrough will be given in class. Recursive Data and Recursive Functions You should already be familiar with writing simple programs that use nonrecursive data structures like arrays, so today we re going to start off with a recursive implementation of lists. This is called a singly linked list, or simply a linked list. Enter Listing 1 into DrJava, save it as List.java, and compile it. Probably you ve not seen code written like this before, so let s walk through it. We re defining List to be an abstract type, and Empty and Cons are both subtypes of List. (Cons stands for construct we re constructing a list from a datum and another list.) These are the only subtypes of List, which means that every List is either an instance of Empty or an instance of Cons. (We can t have any instance of just List because we declared List to be abstract.) Now we can express lists of integers. Switch to the Interactions panel of DrJava and try: 1

class Empty extends List { class Cons extends List { int first; List rest; // not null public Cons(int first, List rest) { this.first = first; this.rest = rest; Listing 1: Our initial List class. > new Empty() Empty@4b771a > new Cons(3, new Empty()) Cons@443fbe > new Cons(3, new Cons(5, new Empty())) Cons@5aeb9e Notice that when you enter a Java expression without a terminating semicolon, DrJava evaluates the expression and prints the result. Unfortunately, the results aren t very intelligible right now; all we know from the result of the last expression is that it was an instance of Cons and that it was distinct from the previous Cons. Let s fix this. If an expression evaluates to an object, DrJava prints it by calling the tostring method of the object. All classes automatically have a tostring method that prints the kind of results we ve seen so far, but we want to write one that is more appropriate to Lists. One way to do it is to writen a recursive method, mirroring the structure of the data. Add the methods from Listing 2 to the three classes we already have. In this example, we have an abstract tostring method in the List class. In the Empty class, we implement the tostring method to simply return the constant result appropriate for the empty list. The Cons class contains both a value and a reference to the rest of the list, so we implement tostring to construct its result by recursively calling tostring on the rest of the list and then combining this with the value at the current position. Think of the recursion as creating separate copies of the method, each executing with its own local variables. It is not one set of local variables taking on many different values! Exercise: Following this model, try to write a sum method that returns the sum of the elements in the list. Then write a length method that returns the number of elements in the list. Exercise: Write a mapsquare method that returns a new list in which each element is the square of the corresponding element of the original list. Don t modify the original list. 2

public abstract String tostring(); class Empty extends List { public String tostring() { return "<Empty>"; class Cons extends List { public String tostring() { return "(" + first + " " + rest.tostring() + ")"; Listing 2: A recursive tostring implementation. Exercise: Write an isincreasing method that returns true if the list is in increasing order. Hint: This one requires a helper function. Problems 1. Write a max method that returns the maximum value in the list. Think: what should new Empty().max() evaluate to? 2. Write a filterevens method that returns a new list containing only the even numbers of the given list. 3. By now you should understand how lists are formed. Replace our tostring implementation with one that produces results like [2 1 1] rather than (2 (1 (1 <Empty>))). Think before you code: How will you be sure to include exactly one pair of brackets? To make constructing lists easier, also add the code from Listing 3. Now you should be able to write: > List.fromString("2 3 5 7").mapSquare() [4 9 25 49] 4. Write a recursive method filternonmultiples that takes a parameter n and returns the sublist containing only those values that are not multiples of n. For example, List.fromString("4 7 9 12 16 19").filterNonmultiples(4) should return [7 9 19]. 5. In the style of List, implement a recursive data structure Polynomial that stores one-variable polynomials with integral powers. An example polynomial is x 2 + 3.2x 17 + 0.9. (Note that 0.9 = 0.9x 0.) The variable name is irrelevant you don t need to store it and don t worry about the order of the terms. In addition to the usual tostring method, write an eval method that evaluates 3

import java.io.*; public static List fromstring(string str) throws IOException { StreamTokenizer st = new StreamTokenizer(new StringReader(str)); st.resetsyntax(); // Let s keep it simple st.whitespacechars(, ); // Space is the only whitespace st.parsenumbers(); // We want numbers return fromstringtokenizer(st); private static List fromstringtokenizer(streamtokenizer st) throws IOException { switch (st.nexttoken()) { case StreamTokenizer.TT_EOF: return new Empty(); case StreamTokenizer.TT_NUMBER: return new Cons((int) st.nval, fromstringtokenizer(st)); default: throw new IOException("Bad input string"); Listing 3: An implementation of fromstring. the polynomial at a given value for x. Test your code in DrJava, and submit it as Polynomial.java. Think about how you could implement addition of polynomials or store more general mathematical expressions. Exercise: What are the advantages of implementing List recursively rather than with arrays? What are the advantages of array-based implementations like java. util.vector? Exercise: Write an append method. Exercise: Write a reverse method. Can you do it without calling append? Exercise: Since a recursive function calls itself, how do we know that it will not loop forever? Structural Induction As we defined it, the set of all possible Lists is an inductively defined set because we can define it as Let the set of all possible Lists be the smallest set such that: Base case: Empty is a List Inductive step: If e is a List, then Cons(e) also is a List In general, an inductively defined set S is one that can be expressed as 4

Let the set S be the smallest set such that: Base case: Some base elements are in the set S Inductive step: If an element e is in the set S, then some manipulation of e according to some rules also is in the set S Exercise: Inductively define the set of natural numbers. Exercise: Inductively define the set of mathematical expressions involving natural numbers and the addition, subtraction, multiplication, and division operators. By inductively defining a set, we succinctly and precisely describe a set that may contain an infinite number of elements. For example, the set of mathematical expressions described above is infinite and appears very difficult to enumerate (unlike the natural numbers), but it is very succinctly defined. Exercise: Are all inductively defined sets infinite? Conversely, each element of an inductively defined set can be derived from one of the base elements using only a finite number of applications of the inductive step. Exercise: Given that 2, 3, and 5 are natural numbers, prove in terms of your definition that 2 + (3 5) is a mathematical expression of the kind you defined earlier. A proof by induction is a proof that some predicate is true for every element of an inductively defined set. There are different kinds of proof by induction, so to be specific we ll call this proof by structural induction. The basic form of such a proof looks like: P (e): State the predicate you want to prove about each element e. Proof by structural induction: Base case: Prove that the base elements of the set satisfy P (e). Induction step: Assume that P (e) is true for any element e. (This is called the induction hypothesis.) Prove that the rules of the inductively defined set preserve the property P (e). Try to convince yourself that this makes sense. Exercise: A common confusion is to think that we re trying to prove P (e) from itself. Why is this not true? Exercise: Prove that your List sum method is correct. Notice how the structure of the proof parallels the structure of the code. Exercise: Prove that your length method is correct. In your proofs, always be careful to follow the same basic structure: state exactly what you re trying to prove, how you re proving it (by structural induction over some set), what the base cases are, what the induction steps are, and where you use the induction hypothesis. Induction proofs are very straightforward when you do them correctly. Imprecise words like eventually have no place in induction proofs. Problems 6. Inductively define the set of natural numbers not divisible by 5. 7. Prove that your List append method is correct. 5