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

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

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

YOUR NAME PLEASE: *** SOLUTIONS ***

CS 314 Principles of Programming Languages

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

CS 314 Principles of Programming Languages

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

User-defined Functions. Conditional Expressions in Scheme

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

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

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

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

Discussion 4. Data Abstraction and Sequences

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer 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.

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


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

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

CS 61A Discussion 8: Scheme. March 23, 2017

April 2 to April 4, 2018

CS61A Notes Disc 11: Streams Streaming Along

Functional Programming - 2. Higher Order Functions

CS 314 Principles of Programming Languages. Lecture 16

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

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

Discussion 11. Streams

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

CSE413 Midterm. Question Max Points Total 100

(scheme-1) has lambda but NOT define

A Brief Introduction to Scheme (II)

Problem Set 4: Streams and Lazy Evaluation

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Functional Programming. Pure Functional Languages

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

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

CS 152 / SE 152 Programming Language Paradigms

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

Documentation for LISP in BASIC

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

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

Introduction to Scheme

Richard Feynman, Lectures on Computation

Turtles All The Way Down

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

SCHEME AND CALCULATOR 5b

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

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

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

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

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

Laboratory: Recursion Basics

Sample Final Exam Questions

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

University of Virginia, Department of Computer Science cs150: Computer Science Spring 2007 Out: 5 February 2007 Problem Set 3: L-System Fractals

T λx (λy. x) F λx (λ y. y)) if λp (λc (λa. (pc)a)

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

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Administrivia. Simple data types

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

CS3: Introduction to Symbolic Programming. Lecture 14: Lists Scheme vs. other programming languages.

CS61A Midterm 2 Review (v1.1)

Berkeley Scheme s OOP

Scheme: Strings Scheme: I/O

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

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Repetition Through Recursion

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


CS 360 Programming Languages Day 14 Closure Idioms

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

PROBLEM SOLVING 11. July 24, 2012

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

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

Programming Languages

CS150: Exam 2 Due: Mon Apr 20th by 3:30pm (in class) or by 6:30pm (OLS 219) UVA ID: Directions

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

CS450 - Structure of Higher Level Languages

Class 22: Inheritance

CS 61B, Spring 1999 MT3 Professor M. Clancy

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

More About Recursive Data Types

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

Lisp. Versions of LISP

Structure and Interpretation of Computer Programs

Project 2: Scheme Interpreter

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

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Principles of Programming Languages 2017W, Functional Programming

Streams and Evalutation Strategies

Not really what happens in the computer. The Environment Model. (set-car! l 'z) (car l) z

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

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

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

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Transcription:

Lecture 8: Recursing Lists CS15: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Defining Recursive Procedures 1. Be optimistic! Assume you can solve it. problem, something you can already solve. (This is the case.) 3. Consider how you would solve an instance of the problem using the result for a slightly smaller instance. (recursive case) 4. Combine them to solve the problem. 2 Defining Recursive Procedures on Lists 1. Be very optimistic! problem (almost always ), something you can already solve. ( case) 3. Consider how you would solve an instance of the problem using the result for a slightly smaller instance (the cdr of the list). (recursive case) 4. Combine them to solve the problem. list? (if (pair? p) (list? (cdr p)) #f))) 3 4 Tracing list? sumlist To enable tracing: (require (lib "trace.ss")) > (trace list?) (list?) > (list? ) (list? ()) > (list? (list 1 2 3)) (list? (1 2 3)) (list? (2 3)) (list? (3)) (list? ()) (+ (car p) (sumlist (cdr p))))) 5 6 1

Tracing sumlist (+ (car p) (sumlist (cdr p))))) > (trace +) > (sumlist (list 1 2 3 4)) (+ 4 ) 4 (+ 3 4) 7 (+ 2 7) 9 (+ 1 9) 1 1 map 7 8 Tracing map > (map (lambda (x) (* x 2)) (list 1 2 3)) (map #<procedure> (1 2 3)) (map #<procedure> (2 3)) (map #<procedure> (3)) (map #<procedure> ()) () (6) (4 6) (2 4 6) (2 4 6) Similarities and Differences (+ (car p) (sumlist (cdr p))))) (define (list-cruncher?...? lst) result (combiner (car lst) (recursive-call... (cdr lst)))) 9 1 Similarities and Differences (+ (car p) (sumlist (cdr p))))) (define (list-cruncher?...? lst) result (combiner (car lst) (recursive-call... (cdr lst)))) list-cruncher (define (list-cruncher proc combiner lst) (list-cruncher proc combiner (list-cruncher (lambda (x) x) + p)) (list-cruncher f cons p)) 11 12 2

Can list-cruncher crunch length? (define (list-cruncher proc combiner lst) (list-cruncher proc combiner (+ 1 (length (cdr p))))) Can list-cruncher crunch length? (define (list-cruncher proc combiner lst) (list-cruncher proc combiner (+ 1 (length (cdr p))))) (list-cruncher (lambda (x) 1) + p)) 13 14 Can list-cruncher crunch list?? (define (list-cruncher proc combiner lst) (list-cruncher proc combiner (if (pair? p) (list? (cdr p)) #f))) list-or-not-cruncher (define (list-or-not-cruncher nonlist proc combiner lst) (if (not (pair? lst)) nonlist (list-or-not-cruncher nonlist proc combiner ) (list-or-not-cruncher #f (lambda (x) x) (lambda (f r) r) p)) This works, but is not an elegant or simple way of defining list?! 15 16 find-closest-number Define find-closest-number that takes two parameters, a goal and a list of numbers, and produces the number in the list numbers list that is closest to goal: > (find-closest-number 15 (list 11 11 12 157 34 588)) 157 > (find-closest-number 12 (list 1 11 21)) 11 > (find-closest-number 12 (list 95)) 95 Find Closest Number Be optimistic! Assume you can define: (find-closest-number goal numbers) that finds the closest number to goal from the list of numbers. What if there is one more number? Can you write a function that finds the closest number to match from newnumber and numbers? 17 18 3

Find Closest Strategy: If the new number is better, than the best match with the other number, use the new number. Otherwise, use the best match of the other numbers. Optimistic Function (define (find-closest goal-number numbers) ;; case missing for now (if (< )) (find-closest-number goal (cdr numbers))))) (find-closest-number goal (cdr numbers)))) 19 2 Defining Recursive Procedures problem (almost always ), something you can already solve. ( case) Is the case for find-closest-number? The Base Case (define (find-closest-number goal numbers) (if (= 1 (length numbers)) (if (< (first numbers))) (find-closest-number goal (cdr numbers))))) (find-closest-number (cdr numbers)))) 21 22 (define (find-closest-number goal numbers) (if (= 1 (length numbers)) (if (< )) (find-closest-number goal (cdr numbers))))) (find-closest-number goal (cdr numbers))))) > (find-closest-number 15 (list 11 11 12 157 34 588)) 157 > (find-closest-number (list 1)) 1 > (find-closest-number (list )) first: expects argument of type <non-empty list>; given () Generalizing find-closest-number How would we implement find-closest-number-without-going-over? What about find-closest-word?... The closeness metric should be a procedure parameter 23 24 4

Charge Read GEB Little Harmonic Labyrinth and Chapter 5 before Monday s class PS3 accepted until beginning of Monday s class 25 5