CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Similar documents
CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

Module 10: General trees

CS115 - Module 10 - General Trees

Module 5: Lists. Readings: HtDP, Sections 9, 10.

Working with recursion

Module 8: Local and functional abstraction

Graphs. Directed graphs. Readings: Section 28

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

Module 8: Binary trees

Trees. Readings: HtDP, sections 14, 15, 16.

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

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

Functional abstraction

Module 9: Binary trees

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

CS 5010 Program Design Paradigms Lesson 6.1

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

How to Design Programs

Assignment: 7. Due: Language level: Allowed recursion:

Trees. Example: Binary expression trees. Example: Evolution trees. Readings: HtDP, sections 14, 15, 16.

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

CS115 - Module 8 - Binary trees

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

CS115 - Module 9 - filter, map, and friends

Module 10: Imperative Programming, Modularization, and The Future

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

Local defini1ons. Func1on mul1ples- of

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Module 3: New types of data

More About Recursive Data Types

CS 1101 Exam 3 A-Term 2013

CS115 - Module 4 - Compound data: structures

Expression Values Operators. (string-append a b c ) a, b, c string-append. (substring abcd 0 2) abcd, 0, 2 substring

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

(add1 3) 4 (check-expect (add1 3) 4)

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Chapter 20: Binary Trees

(add1 3) 4 (check-expect (add1 3) 4)

The Design Recipe Fall 2017

CS 1102, A05 Final Exam

Racket: Modules, Contracts, Languages

CS 115 Lecture Notes Winter 2019

CS2500 Exam 2 Fall 2011

YOUR NAME PLEASE: *** SOLUTIONS ***

Style and Submission Guide

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

Using Scala in CS241

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

The Design Recipe Fall 2018

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

The Typed Racket Guide

Use recursion to write a function that duplicates the following function: (def (f L) (map (lambda (x) (+ (sqr x) x)) L))

Hints for Exercise 4: Recursion

H2 Spring B. We can abstract out the interactions and policy points from DoDAF operational views

Typed Racket: Racket with Static Types

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Module 4: Compound data: structures

CS116 - Module 5 - Accumulative Recursion

Chapter 4: Control structures. Repetition

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Functional Programming. Pure Functional Languages

CSE413 Midterm. Question Max Points Total 100

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

CS 406: Syntax Directed Translation

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Typed Scheme: Scheme with Static Types

Homework 3 COSE212, Fall 2018

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CSCC24 Functional Programming Scheme Part 2

COMP520 - GoLite Type Checking Specification

Repetition Through Recursion

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Functional Programming. Pure Functional Programming

Chapter 4: Control structures

Priority queues. Priority queues. Priority queue operations

CSE 2123 Recursion. Jeremy Morris

COMP-520 GoLite Tutorial

Cribbage Count In Cribbage scoring, there are several ways of accumulating points in a pile of playing cards. Among these are

CSC148-Section:L0301

CS 314 Principles of Programming Languages

Box-and-arrow Diagrams

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Scheme Basics > (butfirst '(help!)) ()

Computer Science Foundation Exam

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Scheme as implemented by Racket

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

Welcome to CS 135 (Winter 2018)

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Transcription:

CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 1

Goals of this tutorial You should be able to... write mutually recursive functions based on the relevant templates. work with lists of arbitrary nesting. understand the syntax and semantics of local. use local when writing your own functions. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 2

Group Problem: Mutual Recursion For this problem, we will use the following structure and data definition to represent a general tree of people: (define-struct person (name children)) ;; A Person is a (make-person Str (listof Person)) ;; requires: the names are unique According to this data definition, we cannot have an empty tree of people. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 3

Group Problem: Mutual Recursion (define cs135-staff (make-person "Karen" (list (make-person "Paul" (make-person "Byron" (list (make-person "Troy" (list (make-person "Jean" empty) (list (make-person "Dustin" empty))) (make-person "Craig" empty) (make-person "Sandy" (list (make-person "Vincent" empty) (make-person "Jimmy" (make-person "Sana" empty))))) (list (make-person "Ben" empty))) (make-person "Zainab" empty)))))) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 4

Group Problem: Mutual Recursion Here is what cs135-staff looks like: Jean Paul Troy Sana Byron Dustin Karen Craig Vincent Sandy Jimmy Ben Zainab CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 5

Group Problem: Mutual Recursion Write templates for each of Person and (listof Person). (define-struct person (name children)) ;; A Person is a (make-person Str (listof Person)) ;; requires: the names are unique CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 6

Group Problem: add-person Using your templates, write a function add-person that consumes two strings, parent-name and new-name, and a Person. It produces the result of inserting a new person with the name new-name as an immediate child of the person with the name parent-name. The new person is inserted at the beginning of the corresponding list of children. You may assume that new-name does not exist as a name in the consumed tree. If parent-name does not exist, the function should produce the same tree that was originally consumed. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 7

Group Problem: add-person (add-person "Paul" "Ian" cs135-staff) Karen Paul Byron Craig Sandy Ian Troy Dustin Vincent Jimmy Zainab Jean Sana Ben CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 8

Group Problem: add-person (add-person "Vincent" "Mark" cs135-staff) Karen Paul Byron Craig Sandy Ian Troy Dustin Vincent Jimmy Zainab Jean Sana Mark Ben CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 9

Group Problem: add-person (add-person "Dave" "Josh" cs135-staff) cs135-staff Karen Paul Byron Craig Sandy Troy Dustin Vincent Jimmy Zainab Jean Sana Ben CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 10

Group Problem: remove-odds Consider the following data definition for a nested list of integers: ;; A Nested-List-Int is one of: ;; * empty ;; * (cons Int Nested-List-Int) ;; * (cons Nested-List-Int Nested-List-Int) Write a function remove-odds which consumes a Nested-List-Int and removes all odd integers from the consumed list. However, all empty lists and any nested lists that only contain empty lists after removing the odd integers should be removed as well. (remove-odds (1 (2 (3 (4) 5) 6) (7 (8 9)))) ((2 ((4)) 6) ((8))) (remove-odds (1 (2 (3 (5) 7) 6) (8 (4 3)))) ((2 6) (8 (4))) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 11

Review: Local Definitions Recall the special form local which allows us to create local definitions. The syntax for local is as follows: (local [definition 1... definition n] expression) where each definition is a define statement, and expression is a Racket expression that uses these definitions. CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 12

Clicker Question: Local Definitions In Intermediate Student, what would this expression produce? (define a 10) (define b (local [(define a 5)] (add1 a))) (+ a b) A 10 B 15 C 16 D 21 E An error CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 13

Clicker Question: Local Definitions In Intermediate Student, what would the following expression produce? (define (sum-lon alon) (local [(define (sum-lon/acc alon sum-so-far) (cond [(empty? alon) sum-so-far] [else (sum-lon/acc (rest alon) (+ (first alon) sum-so-far))]))] (sum-lon/acc alon 0))) (sum-lon/acc (list 2 3 5 7) 0) A 0 B 2 C 7 D 17 E An error CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 14

Stepping Problem - Local Provide a step-by-step evaluation of the following program. When renaming local definitions, append 0 if possible, or else 1, 2, etc. Do not recopy any line that is already in its simplest form. (define (f x y) (local [(define a (+ x 3)) (define y 4) (define (g x) (+ x a))] ( 2 (g y)))) (f 2 6) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 15

(define (f x y) (local [(define a (+ x 3)) (define y 4) (define (g x) (+ x a))] (local [(define a (+ 2 3)) (define y 4) (define (g x) (+ x a))] ( 2 (g y))) (f 2 6) ( 2 (g y)))) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 16

(local [(define a (+ 2 3)) (define y 4) (define (g x) (+ x a))] (define a 0 (+ 2 3)) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) ( 2 (g y))) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 17

(define a 0 (+ 2 3)) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) (define a 0 5) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 18

(define a 0 5) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) ( 2 (g 0 4)) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 19

(define a 0 5) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) ( 2 (g 0 4)) ( 2 (+ 4 a 0)) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 20

(define a 0 5) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) ( 2 (g 0 4)) ( 2 (+ 4 a 0)) ( 2 (+ 4 5)) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 21

(define a 0 5) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) ( 2 (g 0 4)) ( 2 (+ 4 a 0)) ( 2 (+ 4 5)) ( 2 9) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 22

(define a 0 5) (define y 0 4) (define (g 0 x) (+ x a 0)) ( 2 (g 0 y 0)) ( 2 (g 0 4)) ( 2 (+ 4 a 0)) ( 2 (+ 4 5)) ( 2 9) 18 CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 23

Group Discussion: Using local Let s revisit the solution for the remove-odds function. How could you use local constant definitions to avoid exponential blowups and improve readability, without using another helper function? (define (remove-odds nest-loints) (cond [(empty? nest-loints) empty] [(and (integer? (first nest-loints)) (odd? (first nest-loints))) (remove-odds (rest nest-loints))] [(integer? (first nest-loints)) (cons (first nest-loints) (remove-odds (rest nest-loints)))] [(empty? (remove-odds (first nest-loints))) (remove-odds (rest nest-loints))] [else (cons (remove-odds (first nest-loints)) (remove-odds (rest nest-loints)))])) CS 135 Winter 2018 Tutorial 8: Mutual recursion, nested lists, and local 24