GENERATORS AND STREAMS

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

April 2 to April 4, 2018

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

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

ITERATORS AND STREAMS 9

LINKED LISTS AND MIDTERM REVIEW

Streams and Evalutation Strategies

Fall 2017 December 4, Scheme. Instructions

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

Structure and Interpretation of Computer Programs

CS450 - Structure of Higher Level Languages

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

SQL AND FINAL REVIEW

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

Structure and Interpretation of Computer Programs

Fall 2017 December 4, 2017

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

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

Tree-Structured Data. A tree can contains other trees: [5, [6, 7], 8, [[9], 10]] (+ 5 (- 6 7) 8 (* (- 9) 10))

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

Calculus Limits Images in this handout were obtained from the My Math Lab Briggs online e-book.

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

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

Structure and Interpretation of Computer Programs

Introduction to Functional Programming

Structure and Interpretation of Computer Programs

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

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

CS61A Notes Disc 11: Streams Streaming Along

Problem Set 4: Streams and Lazy Evaluation

Discussion 11. Streams

6.001 Notes: Section 17.5

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

Structure and Interpretation of Computer Programs

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

Streams and Lazy Evaluation in Lisp

CSCE 110: Programming I

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

Introduction to Logic Programming. Ambrose

Lexical vs. Dynamic Scope

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

Structure and Interpretation of Computer Programs

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

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

An introduction to Scheme

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

Structure and Interpretation of Computer Programs

CS 61A Orders of Growth & Linked Lists Spring 2018 Discussion 6: March 7, Warmup

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

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

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

OOP 1 OOP CS 61A GROUP MENTORING. July 19, 2017

Chapter 2 Writing Simple Programs

Structure and Interpretation of Computer Programs

STREAMS, ITERATORS, AND BINARY TREES 10

Structure and Interpretation of Computer Programs

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

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

Structure and Interpretation of Computer Programs

SEQUENCES AND TREES 4

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

Functional Data Structures for Typed Racket. Hari Prashanth and Sam Tobin-Hochstadt Northeastern University

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

Supplemental Worksheet Problems To Accompany: The Pre-Algebra Tutor: Volume 2 Section 12 Variables and Expressions

Lazy Evaluation and Parameter Transfer

UNIT 2B An Introduction to Programming ( for -loops) Principles of Computing, Carnegie Mellon University

Typed Racket: Racket with Static Types

What Would Python Print? Tuples, Lists, Dictionaries

Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees

Structure and Interpretation of Computer Programs

CS 275 Name Final Exam Solutions December 16, 2016

Organization of Programming Languages CS3200/5200N. Lecture 11

Structure and Interpretation of Computer Programs

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

FINAL REVIEW 13. December 4, Sample Final Questions

LINKED LISTS AND MIDTERM REVIEW 6

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

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

Sample Final Exam Questions

SI Networked Computing: Storage, Communication, and Processing, Winter 2009

Singly linked lists in C.

CSE341 Autumn 2017, Final Examination December 12, 2017

Reading Assignment. Lazy Evaluation

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

TREES AND ORDERS OF GROWTH 7

Functional Programming

CSCI 2041: Lazy Evaluation

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

Functional Programming

First-class continuations. call/cc, stack-passing CEK machines

A programming language requires two major definitions A simple one pass compiler

CS 61AS Fall 2013 Final

Accelerating Ruby with LLVM

COP4020 Programming Assignment 1 - Spring 2011

Lecture Transcript While and Do While Statements in C++

SCHEME AND CALCULATOR 5b

Calculus Limits Images in this handout were obtained from the My Math Lab Briggs online e-book.

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

Functional Programming - 2. Higher Order Functions

Transcription:

GENERATORS AND STREAMS COMPUTER SCIENCE MENTORS 61A November 13 to November 17, 2017 1 Generators 1. What does the following code block output? def foo(: a = 0 if a < 10: print("hello" yield a print("world" for i in foo(: print(i Hello 0 World 1

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 2 2. How can we modify foo so that list(foo(== [1, 2, 3,..., 10]? (It s okay if the program prints along the way. Change the if to a while statement, and make sure to increment a. This looks like: def foo(: a = 0 while a < 10: a += 1 yield a

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 3 3. Define hailstone_sequence, a generator that yields the hailstone sequence. Remember, for the hailstone sequence, if n is even, we need to divide by two, otherwise, we multiply by 3 and add by 1. def hailstone_sequence(n: >>> hs_gen = hailstone_sequence(10 >>> hs_gen. next ( 10 >>> next(hs_gen #equivalent to previous 5 >>> for i in hs_gen: >>> print(i 16 8 4 2 1 while n!= 1: yield n if n % 2 == 0: n = n // 2 else: n = n*3 + 1 yield n

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 4 4. Define tree_sequence, a generator that iterates through a tree by first yielding the root value and then yielding the values from each branch. def tree_sequence(t: >>> t = Tree(1, [Tree(2, [Tree(5], Tree(3, [Tree(4]] >>> print(list(tree_sequence(t [1, 2, 5, 3, 4] yield t.label for branch in t.branches: for value in tree_sequence(branch: yield value

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 5 2 Streams 1. What s the advantage of using a stream over a linked list? Lazy evaluation. We only evaluate up to what we need. 2. What s the maximum size of a stream? Infinity 3. What s stored in first and rest? What are their types? First is a value, rest is another stream (either a method to calculate it, or an already calculated stream. In the case of Scheme, this is called a promise. 4. When is the next element actually calculated? Only when it s requested (and hasn t already been calculated

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 6 3 What Would Scheme Print? 5. For each of the following lines of code, write what Scheme would output. scm> (define x 1 x scm> (if 2 3 4 3 scm> (define p (delay (+ x 1 p scm> p #[promise] scm> (force p 2 scm> (define (foo x (+ x 10 foo scm> (define bar (cons-stream (foo 1 (cons-stream (foo 2 bar bar

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 7 scm> (car bar 11 scm> (cdr bar #[promise] scm> (define (foo x (+ x 1 foo scm> (cdr-stream bar (3. #[promise] scm> (define (foo x (+ x 5 foo scm> (car bar 11 scm> (cdr-stream bar (3. #[promise]

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 8 4 Code Writing for Streams 6. Write out double naturals, which is a stream that evaluates to the sequence 1, 1, 2, 2, 3, 3, etc. (define (double_naturals (double_naturals_helper 1 0 (define (double_naturals_helper first go-next (define (double_naturals_helper first go-next (if (= 1 go-next (cons-stream first (double_naturals_helper (+ 1 first 0 (cons-stream first (double_naturals_helper first 1 ;Alternative Solutions (define (double_naturals_helper first go-next (cons-stream first (double_naturals_helper (+ go-next first (- 1 go-next

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 9 7. Write out interleave, which returns a stream that alternates between the values in stream1 and stream2. Assume that the streams are infinitely long. (define (interleave stream1 stream2 (define (interleave stream1 stream2 (cons-stream (car stream1 (interleave stream2 (cdr-stream stream1 ;Alternative solution (define (interleave stream1 stream2 (cons-stream (car stream1 (cons-stream (car stream2 (interleave (cdr-stream stream1 (cdr-stream stream2

GROUP TUTORING HANDOUT 9: GENERATORS AND STREAMS Page 10 5 Challenge Question 8. (Optional Write a generator that takes in a tree and yields each possible path from root to leaf, represented as a list of the values in that path. Use the object-oriented representation of trees in your solution. def all_paths(t: >>> t = Tree(1, [Tree(2, [Tree(5], Tree(3, [Tree(4]] >>> print(list(all_paths(t [[1, 2, 5], [1, 3, 4]] if t.is_leaf(: yield [t.label] for b in t.branches: for subpath in all_paths(b: yield [t.label] + subpath