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

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

CS61A Notes Disc 11: Streams Streaming Along

Discussion 11. Streams

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

CS450 - Structure of Higher Level Languages

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

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

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Sample Final Exam Questions

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

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

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

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

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

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

CS61A Midterm 2 Review (v1.1)

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Discussion 4. Data Abstraction and Sequences

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

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

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

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

Problem Set 4: Streams and Lazy Evaluation

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

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

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

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

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

SCHEME AND CALCULATOR 5b

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

CSE413 Midterm. Question Max Points Total 100

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

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

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

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

Streams and Evalutation Strategies

University of Massachusetts Lowell

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

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

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


A random five-digit number: a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3

Repetition Through Recursion

(scheme-1) has lambda but NOT define

Fall 2017 December 4, 2017

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

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

Administrivia. Simple data types

Lexical vs. Dynamic Scope

Lisp Basic Example Test Questions

How to Design Programs

Introduction to Functional Programming

Fall 2017 December 4, Scheme. Instructions

A Brief Introduction to Scheme (II)

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

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

CS 314 Principles of Programming Languages. Lecture 16

Lecture Transcript While and Do While Statements in C++

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

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

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?

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

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

GENERATORS AND STREAMS

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Functional Programming

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

4.2 Variations on a Scheme -- Lazy Evaluation

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

CS 275 Name Final Exam Solutions December 16, 2016

Project 1: Part 1. x = b± b 2 4ac., x 2 = b b 2 4ac.

Project 1: Part 1. Project 1 will be to calculate orthogonal polynomials. It will have several parts. x = b± b 2 4ac., x 2 = b b 2 4ac.

Project 2: Scheme Interpreter

CSE 341 Section Handout #6 Cheat Sheet

COP4020 Programming Assignment 1 - Spring 2011

More About Recursive Data Types

Reading Assignment. Lazy Evaluation

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Functional Programming. Pure Functional Programming

Simplifying Logical Formulas

Vectors in Scheme. Data Structures in Scheme

ITERATORS AND STREAMS 9

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

by the evening of Tuesday, Feb 6

Putting the fun in functional programming

An introduction to Scheme

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

Sequentialising a concurrent program using continuation-passing style

Building a system for symbolic differentiation

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

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

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

Structure and Interpretation of Computer Programs

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

Lecture #24: Programming Languages and Programs

CS 1101 Exam 3 A-Term 2013

CSE341 Spring 2017, Final Examination June 8, 2017

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Transcription:

STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? 2. How does memoization work? 3. Is a cons-stream a special form? Practice with Streams 1. Define a procedure (ones) that, when run with no arguments, returns a cons pair whose car is 1, and whose cdr is a procedure that, when run, does the same thing. Do NOT use cons-stream. 2. Define a procedure (integers-starting n) that takes in a number n and, when run, returns a cons pair whose car is n, and whose cdr is a procedure that, when run with no arguments, does the same thing for n+1. Again, do NOT use cons-stream for this part. 1

DISCUSSION 10: STREAMS Page 2 3. Describe what the following expressions define: a. (define s1 (add-stream (stream-map (lambda(x) (* x 2)) s1) s1)) b. (define s2 (cons-stream 1 (add-stream (stream-map (lambda(x) (* x 2)) s2) s2))) c. (define s3 (cons-stream 1 (stream-filter (lambda(x) (not (= x 1))) s3)))

DISCUSSION 10: STREAMS Page 3 d. (define s4 (cons-stream 1 (cons-stream 2 (stream-filter (lambda(x) (not (= x 1))) s4)))) e. (define s5 (cons-stream 1 (add-streams s5 integers))) 4. Define facts without defining any procedures; the stream should be a stream of 1!, 2!, 3!, 4!, etc. More specifically, it returns a stream with elements (1 2 6 24...). Hint: use the integers stream. 5. (HARD!) Define powers; the stream should be 1 1, 2 2, 3 3 or, (1 4 16 64...). You cannot use the exponents procedure.

DISCUSSION 10: STREAMS Page 4 Practice with Streams 1. Define a procedure (lists-starting n) that takes in n and returns a stream containing (n), (n n+1), (n n+1 n+2)... For example, (lists-starting 1) returns a stream containing like ((1) (1 2) (1 2 3) (1 2 3 4)) 2. Define a procedure, (list->stream ls) that takes in a list and converts it into a stream. Remember, streams don t have to be infinite, and finite streams end with the-empty-stream 3. Define a procedure (chocolate name) that takes in a name and returns a stream like so: STk>(chocolate chung) (chung really likes chocolate chung really really likes chocolate...) Youll want to use helper procedures.

DISCUSSION 10: STREAMS Page 5 Stream-processing 1. Define a procedure, (stream-censor s replacements) that takes in a stream s and a table replacements and returns a stream with all instances of all the car of entries in replacements replaced with the cadr of the entries. STk> (stream-censor (hello you weirdo...) ((you I-am) (weirdo an-idiot))) (hello I-am an-idiot...) 2. Define a procedure (make-alternating s) that takes in a stream of positive numbers and alternate their signs. So STk>(make-alternating ones) (1-1 1-1 1-1...) and

DISCUSSION 10: STREAMS Page 6 STk>(make-alternating integers) (1-2 3-4...). Assume s is an infinite stream. My Bodys Floating Down The Muddy Stream 1. Given streams ones, twos, threes and fours, write down the first ten elements of: (interleave ones (interleave twos (interleave threes fours))) 2. Construct a stream all-integers that includes 0 and both the negative and positive integers. You may use procedures that you have defined above. 3. Suppose we were foolish enough to try to implement stream-accumulate: (define (stream-accumulate combiner null-value s)

DISCUSSION 10: STREAMS Page 7 (cond ((stream-null? s) null-value) (else (combiner (stream-car s) (stream-accumulate combiner null-value (stream-cdr s)))))) 4. What happens when we do: a. (define foo (stream-accumulate + 0 integers)) b. (define bar (cons-stream 1 (stream-accumulate + 0 integers))) c. (define baz (stream-accumulate (lambda (x y) (cons-stream x y)) the-empty-stream integers)) 5. Louis Reasoner thinks that building a stream of pairs from three parts is unnecessarily complicated. Instead of separating the pair (S 0, T 0 ) from the rest of the pairs in the first row, he proposes to work with the whole first row, as follows:

DISCUSSION 10: STREAMS Page 8 (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t)))) Does this work? Consider what happens if we evaluate (pairs integers integers) using Louis s definition of pairs.