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

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

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

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

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

Fall 2017 December 4, Scheme. Instructions

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

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

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

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

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

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

Fall 2017 December 4, 2017

April 2 to April 4, 2018

An introduction to Scheme

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

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

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

Functional Programming. Pure Functional Languages

User-defined Functions. Conditional Expressions in Scheme

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

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

CS61A Midterm 2 Review (v1.1)

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Structure and Interpretation of Computer Programs

A Brief Introduction to Scheme (II)

YOUR NAME PLEASE: *** SOLUTIONS ***

Structure and Interpretation of Computer Programs

Scheme: Strings Scheme: I/O

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

CS450 - Structure of Higher Level Languages

Functional Programming - 2. Higher Order Functions

Introduction to Scheme

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Structure and Interpretation of Computer Programs

Introduction to Functional Programming

Structure and Interpretation of Computer Programs

SCHEME AND CALCULATOR 5b

An Explicit-Continuation Metacircular Evaluator

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional programming with Common Lisp

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE 341 Section Handout #6 Cheat Sheet

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

Problem Set 4: Streams and Lazy Evaluation

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

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

Sample Final Exam Questions

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Structure and Interpretation of Computer Programs

Principles of Programming Languages 2017W, Functional Programming

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Scheme Quick Reference


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

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

INTERPRETERS AND TAIL CALLS 9

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Scheme Quick Reference

ITERATORS AND STREAMS 9

CS 314 Principles of Programming Languages

Functional Programming

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

INTRODUCTION TO SCHEME

Building a system for symbolic differentiation

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

Streams and Evalutation Strategies

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

Functional Programming. Big Picture. Design of Programming Languages

CSE413 Midterm. Question Max Points Total 100

Building a system for symbolic differentiation

An Introduction to Scheme

FP Foundations, Scheme

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017


TAIL CALLS, ITERATORS, AND GENERATORS 11

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

Guerrilla Section 7: Macros, SQL SQL

Structure and Interpretation of Computer Programs

Box-and-arrow Diagrams

Racket Pattern Matching

CS 360 Programming Languages Interpreters

LECTURE 16. Functional Programming

Structure and Interpretation of Computer Programs

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

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?

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

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

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

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

CSc 520 Principles of Programming Languages

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

Documentation for LISP in BASIC

Lisp Basic Example Test Questions

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

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

Transcription:

CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3 > (if 1 (+ 2 3) (/ 1 0)) 5 > (or #f (and (+ 1 2) `apple) (- 5 2)) apple > (define (add x y) (+ x y)) add > (add (- 5 3) (or 0 2)) 2

2 2 Tail Calls 2.1 For the following procedures, determine whether or not they are tail recursive. If they are not, write why not and rewrite the function to be tail recursive on the right. ; Multiplies x by y (define (mult x y) (if (= 0 y) 0 (+ x (mult x (- y 1)))) ; Always evaluates to true ; assume n is positive (define (true1 n) (if (= n 0) #t (and #t (true1 (- n 1))))) ; Always evaluates to true ; assume n is positive (define (true2 n) (if (= n 0) #t (or (true2 (- n 1)) #f))) ; Returns true if x is in lst (define (contains lst x) (cond ((null? lst) #f) ((equal? (car lst) x) #t) ((contains (cdr lst) x) #t) (else #f)))

3 2.2 Tail recursively implement sum-satisfied-k which, given an input list lst, a predicate procedure f which takes in one argument, and an integer k, will return the sum of the first k elements that satisfy f. If there are not k such elements, return 0. ; Doctests scm> (define lst `(1 2 3 4 5 6)) scm> (sum-satisfied-k lst even? 2) ; 2 + 4 6 scm> (sum-satisfied-k lst (lambda (x) (= 0 (modulo x 3))) 10) 0 scm> (sum-satisfied-k lst (lambda (x) #t) 0) 0 (define (sum-satisfied-k lst f k) ) 2.3 Tail-recursively implement remove-range which, given one input list lst, and two nonnegative integers i and j, returns a new list containing the elements of lst in order, without the elements from index i to index j inclusive. For example, given the list (0 1 2 3 4), with i = 1 and j = 3, we would return the list (0 4). You may assume j > i, and j is less than the length of the list. (Hint: you may want to use the built-in append function, which returns the result of appending the items of all lists in order into a single well-formed list.) ; Doctests scm> (remove-range (0 1 2 3 4) 1 3) (0 4) (define (remove-range lst i j)

4 3 Macros 3.1 What will Scheme display? If you think it errors, write Error > (define-macro (doierror) (/ 1 0)) > (doierror) > (define x 5) >(define-macro (evaller y) (list (list 'lambda '(x) 'x) y)) > (evaller 2) 3.2 Consider a new special form, when, that has the following structure: (when <condition> <expr1> <expr2> <expr3>... ) If the condition is not false (a truthy expression), all the subsequent operands are evaluated in order and the value of the last expression is returned. Otherwise, the entire when expression evaluates to okay. scm> (when (= 1 0)(/1 0) 'error) okay scm> (when (= 1 1) (print 6) (print 1) 'a) 6 1 a Create this new special form using a macro. Recall that putting a dot before the last formal parameter allows you to pass any number of arguments to a procedure, a list of which will be bound to the parameter, similar to (*args) in Python. ; implement when without using quasiquotes (define-macro (when condition. exprs) (list 'if ) ; implement when using quasiquotes (define-macro (when condition. exprs) `(if )

5 4 Streams 4.1 What Would Scheme Display? > (define a (cons-stream 4 (cons-stream 6 (cons-stream 8 a)))) > (car a) > (cdr a) > (cdr-stream a) > (define b (cons-stream 10 a)) > (cdr b) > (cdr-stream b) > (define c (cons-stream 3 (cons-stream 6))) > (cdr-stream c) 4.2 Write a function merge that takes in two sorted infinite streams and returns a new infinite stream containing all the elements from both streams, in sorted order. (define (merge s1 s2) )

6 5 Iterators 5.1 What is the definition of an iterable? What is the definition of an iterator? What is the definition of a generator? 5.2 What Would Python Display? >>> def g(n): while n > 0: if n % 2 == 0: yield n else: print('odd') n -= 1 >>> t = g(4) >>> t >>> n >>> t = g(next(t) + 5)

7 5.3 Write a generator function textbfgen inf that returns a generator which yields all the numbers in the provided list one by one in an infinite loop. >>> t = gen_inf([3, 4, 5]) 3 4 5 3 4 def gen_inf(lst): 5.4 Write a function nested gen which, when given a nested list of iterables (including generators) lst, will return a generator that yields all elements nested within lst in order. Assume you have already implemented is iter, which takes in one argument and returns True if the passed in value is an iterable and False if it is not. def nested_gen(lst): ''' >>> a = [1, 2, 3] >>> def g(lst): >>> for i in lst: >>> yield i >>> b = g([10, 11, 12]) >>> c = g([b]) >>> lst = [a, c, [[[2]]]] >>> list(nested_gen(lst)) [1, 2, 3, 10, 11, 12, 2] '''