Talk tonight Richard Stallman Norton 112 6:00 PM.

Slides:



Advertisements
Similar presentations
The Singleton Pattern II Recursive Linked Structures.
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
A list = a Linear Recursive Structure (LRS or LRStruct) What is a list? 1.the empty list is a list 2.a pair whose tail is a list is itself a list This.
Preliminaries Attendance sheets –I remembered! Survey links –HW1 time survey –Anonymous feedback survey HW discussion (4PM, Commons 9)
Preliminaries Stage 1 due tonight by 9:00 PM HW1 discussion (4PM, Commons 9) –may try for next week? HW2 available later today, due Monday by 9:00 PM.
Map, filter and reduce Programming by composition.
No homework this week Stage 2 starts next week. Code review Team with N members is assigned N submissions to review Discuss submissions within team Everyone.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
How do visitors work? This set of slides traces through the execution of a visitor we’ve seen before (the lengthVisitor) on a short list. It shows how.
Discrete Structures Chapter 2 Part A Sequences Nurul Amelina Nasharuddin Multimedia Department.
Lists We’ve seen an array-based list implementation, the ArrayList. Advantage of an array-based implementation: –fast access to a specific index –typically.
Programming with Recursion
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Lists We’ve seen an array-based list implementation, the ArrayList.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
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.
Binary Trees Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRS You are.
Binary Trees Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRStruct You.
CS116 – Tutorial 3 Accumulative Recursion/Runtime.
C++ fundamentals.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Patterns for Decoupling Data Structures and Algorithms Dung “Zung” Nguyen Pepperdine University / University of Houston Stephen Wong Oberlin College
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Questions 4) What type of algorithmic problem-solving technique (greedy, divide-and-conquer, dynamic programming)
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Section 2.5 Introduction to Linked Lists
2.5 Another Java Application: Adding Integers
University of Washington Computer Programming I
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Announcements Quiz 5 HW6 due October 23
EE 312 Final Exam Review.
Chapter 9: Pointers and String
Week 7 - Monday CS 121.
String Objects & its Methods
Presentation transcript:

Talk tonight Richard Stallman Norton 112 6:00 PM

SumVisitor exercise Assume you are given an LRS containing Integer objects (all non-null). Write a visitor which traverses the list structure and calculates the sum of all the int values. Ex: If the list contains Integers for 1, 2, 3, 4 and 5, the visitor must return an Integer containing 15.

SumVisitor exercise Done interactively, from scratch, in Eclipse. Final version in lrstruct.visitors package in SP10-CSE116-LRStruct project.

SumVisitor solution public class SumVisitor implements IAlgo { // The answer in the case of an empty list is zero // (because zero is the identity element of addition). public Integer emptyCase(LRStruct host, Object _) { return 0; } // The answer in the case of a non-empty list is the sum of // the first item in the list and the sum of the rest of the // items in the list. public Integer nonEmptyCase(LRStruct host, Object _) { return host.getDatum() + host.getRest().execute(this, _); }

How do visitors work? This set of slides traces through the execution of a visitor we’ve seen before (the lengthVisitor) on a short list. It shows how the runtime stack makes recursion possible. First let’s recall: –how the visitor is defined –how ‘execute’ is defined in the two list state classes

Length visitor in the empty case the answer is zero in the non-empty case the answer is one more than the length of the rest of the list public Integer emptyCase(LRStruct host, Object _){ return 0; } public Integer nonEmptyCase(LRStruct host, Object _){ return 1 + host.getRest().execute(this,_); }

‘execute’ definition in states Empty state: public Object execute(LRS host, IAlgo algo, Object argut){ return algo.emptyCase(host, input); } NonEmpty state: public Object execute(LRS host, IAlgo algo, Object argut){ return algo.nonEmptyCase(host, input); }

Now let’s trace execution of the visitor LRStruct list = new LRStruct (); list.insertFront(“Wilma”).insertFront(“Fred”); LengthVisitor visitor = new LengthVisitor (); int len = list.execute(visitor,null); System.out.println(“Length is ” + length); Focus on the evaluation of: list.execute(visitor,null)

list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…}  space for return value visitor arg (null) this Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…}  space for return value visitor arg (null) this Polymorphic method call Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this Polymorphic method call Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) algo arg (null) this Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) algo arg (null) this Polymorphic method call Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host 0 _ (null) this Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host 0 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) algo arg (null) this 0 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) 0 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host this host _ (null) 0 1 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this algo arg (null) host 0 1 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) algo arg (null) this 0 1 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) 0 1 Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

this algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host this host _ (null) Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…} visitor arg (null) this algo arg (null) host Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

algo list Tracing the execution of a visitor _state _datum _rest “Fred” _state _datum _rest “Wilma” _state public Object emptyCase(…){…} public Object nonEmptyCase(…){…}  space for return value visitor arg (null) this Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, arg, this); } EmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public O execute(IAlgo algo, LRStruct host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct host, Object _) { return 1 + host.getRest().execute(this, _); }

That’s it! Questions?

A variable used at runtime which was not explicitly declared in the program Used to hold the value returned by the recursive call, so that 1 can be added to it.

Variables: code vs. runtime In code, we declare instance variables once: public class Foo { private Bar _b; } In code, we declare parameters/local variables once: public int someFunction(int x, int y) { int z = x + y; return (z * x) / 2; } These names can represent many variables at runtime. Variable defined not just by name, but name and location in memory.

Additional exercises Write a visitor for an LRS of Integers which returns the largest Integer. Write a visitor for an LRS of Integers, which you can assume appear in order from smallest to largest, which inserts its input in the correct (in order) position within the LRS.