Laboratory: Recursion Basics

Similar documents
Representing Images as Lists of Spots

Repetition Through Recursion

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

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

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

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

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

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

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

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

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

How to Design Programs

Discussion 4. Data Abstraction and Sequences

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

Problem Set 4: Streams and Lazy Evaluation

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

ormap, andmap, and filter

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

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

Functional abstraction

MIDTERM EXAMINATION - CS130 - Spring 2005

15100 Fall 2005 Final Project

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

Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.

Typed Racket: Racket with Static Types

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

Discussion 11. Streams

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

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

More About Recursive Data Types

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

User-defined Functions. Conditional Expressions in Scheme

Documentation for LISP in BASIC

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

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

A Brief Introduction to Scheme (II)

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

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS61A Notes Disc 11: Streams Streaming Along

PROBLEM SOLVING 11. July 24, 2012

Functional Programming

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

CSU211 Exam 2 Fall 2007

YOUR NAME PLEASE: *** SOLUTIONS ***

From Templates to Folds

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

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSC 533: Programming Languages. Spring 2015

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

CS450 - Structure of Higher Level Languages

CS2500 Exam 2 Fall 2011

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Project 2: Scheme Interpreter

Administrivia. Simple data types

Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

NAME EET 2259 Lab 3 The Boolean Data Type

SCHEME AND CALCULATOR 5b

Scheme Quick Reference

Functional Programming - 2. Higher Order Functions

Functional Programming. Pure Functional Languages

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

Fall 2017 December 4, Scheme. Instructions

Scheme Quick Reference

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

Typed Scheme: Scheme with Static Types

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

ITERATORS AND STREAMS 9

CS 314 Principles of Programming Languages

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

CS 152 / SE 152 Programming Language Paradigms

Programming Languages

Printing with Profiles - Adobe Photoshop

Lisp Basic Example Test Questions

CS Lecture 19: Loop invariants

The Typed Racket Guide

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


Working with recursion

Functions (4A) Young Won Lim 5/8/17

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

CS 61B, Spring 1999 MT3 Professor M. Clancy

Putting the fun in functional programming

Functional Programming. Pure Functional Languages

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

CSE 341 Section Handout #6 Cheat Sheet

Case Study: Undefined Variables

1.3. Conditional expressions To express case distinctions like

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

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

Midterm Examination (Sample Solutions), Cmput 325

Stat405. Tables. Hadley Wickham. Tuesday, October 23, 12

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

Fall 2017 December 4, 2017

Solving Your Problem by Generalization

Data-Flow Analysis Foundations

Functions (4A) Young Won Lim 3/16/18

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

Transcription:

Fundamentals of Computer Science I: Media Computing (CS151.02 2007F) Laboratory: Recursion Basics Summary: In this laboratory, you will explore some basic concepts in recursing over lists. Contents: Preparation Exercises Exercise 1: Testing Sum Exercise 2: Removing Dark Colors Exercise 3: Counting Values Exercise 4: Product Exercise 5: Counting Special Values Exercise 6: Summing Components Exercise 7: Filtering Out Reds For Those With Extra Time Extra 1: Filtering Out Spots Notes Notes on Problem 5: Summing Components Reference: Here are the definitions from the reading. ;;; sum ;;; numbers, a list of numbers. ;;; Find the sum of the elements of a given list of numbers ;;; total, a number. ;;; Preconditions: ;;; All the elements of numbers must be numbers. ;;; Postcondition: ;;; total is the result of adding together all of the elements of numbers. ;;; If all the values in numbers are exact, total is exact. ;;; If any values in numbers are inexact, total is inexact. (define sum (lambda (numbers) (if (null? numbers) 0 (+ (car numbers) (sum (cdr numbers)))))) (define rgb.filter-out-dark 1

(cond ((null? colors) null) ((rgb.dark? (car colors)) (rgb.filter-out-dark (cdr colors))) (else (cons (car colors) (rgb.filter-out-dark (cdr colors))))))) The latter procedure requires rgb.dark?, which follows. ;;; rgb.dark? ;;; color, an RGB color ;;; Determine if the color appears dark. ;;; dark?, a Boolean (define rgb.dark? (lambda (color) (> 33 (rgb.brightness color)))) ;;; rgb.brightness ;;; color, an RGB color ;;; Computes the brightness of color on a 0 (dark) to 100 (light) scale. ;;; b, an integer ;;; Preconditions: ;;; color is a valid RGB color. That is, each component is between ;;; 0 and 255, inclusive. ;;; Postconditions: ;;; If color1 is likely to be perceived as lighter than color2, ;;; then (brightness color1) > (brightness color2). (define rgb.brightness (lambda (color) (round (* 100 (/ (+ (*.30 (rgb.red color)) (*.59 (rgb.green color)) (*.11 (rgb.blue color))) 255))))) Preparation In this laboratory, we will not be working with images (just with colors and with lists), so you need not create an image. a. Copy the sum and rgb.filter-out-dark procedures to your definitions pane. b. Copy the rgb.dark? and rgb.brightness procedures to your definitions pane. c. Create a list of a dozen or so colors (red, black, green, blue, yellow, orange, magenta, white, black, etc.). Name it my-colors. 2

Exercises Exercise 1: Testing Sum a. Read through sum so that you have a sense of its purpose. b. Verify that sum produces the same result as in the corresponding reading. c. What value do you expect sum to produce for the empty list? Check your answer experimentally. d. What value do you expect sum to produce for a singleton list? Check your answer experimentally. e. Try sum for a few other lists, too. Exercise 2: Removing Dark Colors a. Read through the definition of rgb.filter-out-dark to try to understand what it does. b. Determine which colors in my-colors are dark with (map rgb.dark? my-colors). c. Create a list of non-dark colors with (rgb.filter-out-dark my-colors). d. Verify that all the resulting colors are not dark. Exercise 3: Counting Values Suppose the length procedure were not defined. We could define it by recursing through the list, counting 1 for each value in the list. In some sense, this is much like the definition of sum, except that we use the value 1 rather than the value at each spot. a. Using this idea, write a procedure, (my-length lst) that finds the length of a list (without using length). b. Check your answer. Exercise 4: Product Write a procedure, (product nums), that, given a list of numbers, computes the product of those numbers. You should feel free to use sum as a template. However, you should think carefully about the base case. Exercise 5: Counting Special Values What if we don t want to count every value in a list? For example, what if we only want to count the dark values in a list of colors. In this case, we still recurse through the list, but we sometimes count 1 (when the color is dark) and sometimes count 1 (when the color is not dark). 3

a. Using this idea, write a procedure, (rgb.tally-dark colors), that, given a list of colors, counts how many are dark. b. Test your procedure. Exercise 6: Summing Components In the past, we ve found it useful to find the average of two colors. Let s consider how we might find the average of a list of colors. First, we would need to find the number of colors in the list. That s easy, we just use the length procedure. Next, we need to find the sum of each component. That s a bit harder, but let s suppose we can do it. We next divide each sum by the length, and get the average of that component. Finally, we put it all together with rgb.new. That is, we might write (define average-color (rgb.new (/ (rgb-list.sum-red colors) (length colors)) (/ (rgb-list.sum-green colors) (length colors)) (/ (rgb-list.sum-blue colors) (length colors))))) Of course, for this to work, we need to write rgb-list.sum-red, rgb-list.sum-blue, and rgb-list.sum-green. For now, we ll write one of the three. (The other two should be obvious.) a. Write a procedure, (rgb-list.sum-red colors), that computes the sum of the red components in a color. b. Test your procedure on a list of a single color. c. Test your procedure on the my-colors list you wrote earlier. d. It is possible to write rgb-list.sum-red without using direct recursion. How? By an appropriate combination of map and sum. Try doing so. (If you can t find a solution, look at the notes on the problem.) Exercise 7: Filtering Out Reds Write a procedure, (rgb-list.filter-reds colors), that filters out all elements of colors with a red component of at least 128. For Those With Extra Time Extra 1: Filtering Out Spots Write a procedure, (spots.bound spots left top right bottom), that filters out all elements of spots which do not appear in the rectangle bounded at the left by left, at the top by top, at the right by right, and at the bottom by bottom. 4

For example, if left is 5, top is 10, right is 8, and bottom is 20, the procedure should remove any spots whose column is less than 5 or more than 8 or whose row is less than 10 or more than 20. Notes Notes on Problem 5: Summing Components We can use map to extract the red component of each color. (map rgb.red colors) That gives us a list of numbers, which we can sum with sum. (sum (map rgb.red colors)) Putting it all together in a procedure (define rgb-list.sum-red (sum (map rgb.red colors)))) Copyright 2007 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.) This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. 5