Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Similar documents
Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Homework 12: Tail Recursion and Analysis Due: 11:59 PM, Dec 4, 2018

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

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

Lab 7: OCaml 12:00 PM, Oct 22, 2017

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

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

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

The Design Recipe Fall 2018

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

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

The Design Recipe Fall 2017

CS 314 Principles of Programming Languages

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

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

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

Lab 1: Setup 12:00 PM, Sep 10, 2017

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

CS1 Recitation. Week 2

User-defined Functions. Conditional Expressions in Scheme

More About Recursive Data Types

Homework 4: Hash Tables Due: 5:00 PM, Mar 9, 2018

15-110: Principles of Computing, Spring 2018

Module 3: New types of data

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

6.001 Notes: Section 4.1

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

YOUR NAME PLEASE: *** SOLUTIONS ***

Racket Style Guide Fall 2017

Lecture 3: Recursion; Structural Induction

Module 8: Local and functional abstraction

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Structure and Interpretation of Computer Programs

Discussion 4. Data Abstraction and Sequences

Case by Case. Chapter 3

Lecture 31: Graph Search & Game 10:00 AM, Nov 16, 2018

CSU211 Exam 2 Fall 2007

Lesson 13: Exploring Factored Form

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

RECURSION: n. SEE RECURSION 3

Lecture 8: Iterators and More Mutation

Organization of Programming Languages CS3200/5200N. Lecture 11

15-122: Principles of Imperative Computation, Fall 2015

Lecture 23: Priority Queues, Part 2 10:00 AM, Mar 19, 2018

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

Project 2: Scheme Interpreter

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

Lesson 19. Opening Discussion

Homework 5: Fun with Scala Due: 5:00 PM, Mar 16, 2018

CS 051 Homework Laboratory #2

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

(Provisional) Lecture 20: OCaml Fun!

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

9-1: Slope NAME: 1. What do you think is meant by the terms rise and run?

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

CS 314 Principles of Programming Languages. Lecture 16

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

CMSC 201 Spring 2017 Project 1 Number Classifier

ENVIRONMENT DIAGRAMS AND RECURSION 2

Halting Measures and Termination Arguments

Class Structure. Prerequisites

CS116 - Module 5 - Accumulative Recursion

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

Lecture 7: Lists, Version 2 (with Mutation)

Repetition Through Recursion

Lecture 5: Implementing Lists, Version 1

The Art of Recursion: Problem Set 10

CS2500 Exam 2 Fall 2011

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

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

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

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

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

Streams and Evalutation Strategies

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

SCHEME AND CALCULATOR 5b

Lecture 4 CSE July 1992

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

CS 314 Principles of Programming Languages

Homework 2: IP Due: 11:59 PM, Oct 20, 2016

CS 4349 Lecture September 13th, 2017

More Complicated Recursion CMPSC 122

ITERATORS AND STREAMS 9

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS61A Lecture 9 Immutable Data Structures. Jom Magrotker UC Berkeley EECS July 2, 2012

CS3: Introduction to Symbolic Programming. Lecture 5:

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

Case Study: Undefined Variables

Transcription:

CS17 Integrated Introduction to Computer Science Klein Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018 Contents 1 Factorial 3 2 Fibonacci 4 3 Odds Only 5 4 Increment All 6 5 Frequency 6 6 Sublist 7 6.1 Take............................................. 7 6.2 Drop............................................. 7 6.3 Putting it Together..................................... 8 Objectives By the end of this homework, you will be able to: 1. Visualize recursive calls in diagram form 2. Use recursion to filter out a list 3. Use recursion to apply the same procedure to every element of a list How to Hand In To hand in your solutions to these problems, you must store them in appropriately-named files. Each should be named for the corresponding problem, as follows: ˆ factorial-table.txt ˆ fibonacci-table.txt ˆ fibonacci-diagram.png (or.pdf,.jpg,.gif) ˆ odds-only.rkt

ˆ increment-all.rkt ˆ frequency.rkt ˆ sublist.rkt For this assignment, all files you turn in that contain code must be Racket files, so they must end with extension.rkt For this and every future assignment, you should also have a README.txt file whose first line contains only your Banner ID, and optionally with a message to the person grading explaining anything peculiar about the handin. For example: README.txt: B01234567 There s nothing to say except that I m turning in some files plus this README the way the instructions say that I should. To hand in your solutions to these problems, you must upload them to Gradescope. Do not zip or compress them. If you choose to also store these files on department machines, all your solution files should reside in your /course/cs0170/homeworks/hw03 directory. Design Recipe In last week s homework, we introduced you to the Design Recipe. For each procedure you wrote, you wrote a procedure specification, listing the input and output of a procedure. This week, we will add two other pieces to the Design Recipe recursion diagrams and test cases. Whenever we ask you to write a procedure, you are expected to carry out each and every step of the design recipe. That is, you are to: ˆ Write a procedure specification (input/output) ˆ Write recursion diagrams for each possible behavior, including recursive input/output. Note, if the procedure is not recursive, you may leave this out. ˆ Write test cases, testing both base and recursive cases. ˆ Write the procedure Additionally, you are to follow the Racket/Scheme style guide on our website. You should not test undefined behavior, nor should your goal be to write as many tests as possible. Instead, you should write high quality tests that differentiate good and bad implementations. To accomplish this, you should test both base cases and recursive cases, as well as any special cases you can think of. To receive full credit, you must also define all procedures to take in arguments in the same order as described in the problem. We ve included an example below: 2

;; input: alon, a list of numbers ;; output: the sum of all numbers in alon ;; recursion diagrams ;; input: (quote (5)) ;; recursive input: (quote ()) ;; recursive output: 0 ;; output: 5 ;; input: (quote (1 2 3)) ;; recursive input: (quote (2 3)) ;; recursive output: 5 ;; output: 6 (define sum (lambda (alon) (cond ((empty? alon) 0) (#true (+ (first alon) (sum (rest alon))))))) ;; test cases (check - expect (sum empty) 0) (check - expect (sum (quote (1))) 1) (check - expect (sum (quote (5 4 3))) 12) Problems 1 Factorial In class, we introduced harmonic numbers and showed we can calculate the n th harmonic number using recursion. In this problem, we ll explore the n th factorial. As you may recall from a mathematics class, we write the n th factorial as n! It s equal to: F (n) = { 1 if n = 0 n F (n 1) if n > 0 As you can see, we have both a base case and a recursive case. To implement this in Scheme/Racket, we would create a procedure like this: ;; input: n, a non - negative integer ;; output the nˆth factorial ;; recursion diagrams ;; input: n = 1 ;; recursive input: 0 ;; recursive output: 1 ;; output: n = 1 ;; input: n = 4 3

;; recursive input: 3 ;; recursive output: 6 ;; output: n = 24 (define factorial (lambda (n) (cond ((= n 0) 1) ((> n 0) (* n (factorial ( - n 1))))))) Task: In a text file called factorial-table.txt, construct a table like the following, filling in the values for each factorial in the below table: ------------- n F(n) ------------- 0 1 1... 2... 3... 4... 5... In the next problem, we ask you to draw another type of diagram where each invocation is a node (circle) in a graph with an arrow going from one invocation to its recursive invocation. To demonstrate, we ve included a diagram for our factorial procedure. 4

2 Fibonacci In the harmonic number example presented in class and the factorial problem in the previous question, we ve written recursive procedures with one recursive call. That s not always the case. Next, we re going to look at Fibonacci numbers. The n th Fibonacci number is defined as: 0 if n = 0 F (n) = 1 if n = 1 F (n 1) + F (n 2) if n > 1 As you can see, we have two base cases, where both return a constant. Our recursive case (when n > 1) has two recursive calls. To implement this in Scheme, we would create a procedure like this: ;; input: n, a non - negative integer ;; output the nˆth fibonacci number ;; recursion diagrams ;; input: 2 ;; recursive input: 1 ;; recursive output: 1 ;; ;; recursive input: 0 ;; recursive output: 0 ;; output: 1 ;; input: 3 ;; recursive input: 2 ;; recursive output: 1 ;; ;; recursive input: 1 ;; recursive output: 1 ;; output: 2 (define fib (lambda (n) (cond ((= n 0) 0) ((= n 1) 1) ((> n 1) (+ (fib ( - n 1)) (fib ( - n 2))))))) Task: In a text file called fibonacci-table.txt, construct a table like the following, filling in the values for each fibonacci number in the below table: ------------- n F(n) ------------- 0 0 1... 2... 3... 4... 5... 5

Task: Draw another type of diagram where each invocation is a node (circle) in a graph with an arrow going from one invocation to its recursive calls(s). Scan, take a photo of, or draw on the computer the full diagram. Name it fibonacci-diagram.png. Hint: Here s a diagram to get you started! 3 Odds Only We can use builtins that return booleans (these are called predicates) to filter lists by removing elements for which the predicate returns false. Task: Write a procedure, odds-only, that consumes (i.e., takes as input) a list of integers, aloi, and produces (i.e., outputs) another list of integers that contains only the odd numbers in aloi, in their input order. Use the predicate odd? to determine whether the number is odd or not. (odds - only (quote (1 2 3))) => (1 3) (odds - only (quote (2 18))) => () Note: Be sure to include recursion diagram(s) (input, recursive input, recursive output, and output) as part of the design recipe. You should include two recursive diagrams that model two distinct cases. Be thoughtful in your choice of inputs so that the two recursive diagrams show distinct behavior. 4 Increment All Task: Write a procedure, increment-all, that takes as input a list of integers, aloi, and outputs a new list where each element is the result of adding one to the corresponding number in the old list. (increment - all empty) 6

=> () (increment - all (quote (1))) => (2) (increment - all (quote 1 2 5)) => (2 3 6) (increment - all (quote ( - 1 5-3))) => (0 6-2) Note: Be sure to include recursion diagram(s) (input, recursive input, recursive output, and output) as part of the design recipe. You should include two recursive diagrams that model two distinct cases. Be thoughtful in your choice of inputs so that the two recursive diagrams show distinct behavior. 5 Frequency Task: Write a procedure, freq, that takes as input a list, L, a data object x, and outputs the number of times that x occurs in L. (freq (quote ()) 7) => 0 (freq (quote (#false #true #false)) #true) => 1 (freq (quote (1 2 1 5)) 1) => 2 Note: Be sure to include recursion diagram(s) (input, recursive input, recursive output, and output) as part of the design recipe. You should include two recursive diagrams that model two distinct cases. Be thoughtful in your choice of inputs so that the two recursive diagrams show distinct behavior. 6 Sublist The next problem will be fully coded on the next homework assignment. week, we re asking you to get started by writing the design recipe. For this 6.1 Take Task: Write the design recipe including procedure specification, test cases and recursive diagrams(s) - for a procedure take that takes in a non-negative number n and a list L, and outputs a list consisting of the first n elements of L. 7

Note: You may assume that the input n will be between 0 and the length of L. (inclusive) (take 3 (quote (1 2 3 4))) => (1 2 3) (take 0 (quote (1 2 3 4))) => () 6.2 Drop Task: Write the design recipe including procedure specification, test cases and recursive diagrams(s) - for a procedure drop that takes in a non-negative number n and a list L, and outputs a list consisting of all but the first n elements of L. Note: You may assume that the input n will be between 0 and the length of L. (inclusive) (drop 2 (quote (1 2 3 4))) => (3 4) (drop 0 (quote (1 2 3 4))) => (1 2 3 4) 6.3 Putting it Together Task: Write the design recipe including procedure specification and test cases - for a procedure sublist that takes in a list L, a non-negative number start, and a non-negative number len. The return value should be the list of elements in L, starting with the one whose index is start and going for len elements. Note: The index of the first element of a list is 0. Also note that this procedure is not recursive itself, and as such, no recursive examples are required. Note: As this procedure itself is not recursive, you need not submit a recursive diagram. (sublist (quote (1 2 3 4 5)) 2 3) => (3 4 5) (sublist (quote (1 2 3 4 5)) 1 2) => (2 3) (sublist (quote (1 2 3 4 5)) 0 3) => (1 2 3) 8

Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS 17 document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/csci0170/feedback. 9