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

Similar documents
April 2 to April 4, 2018

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

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

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

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

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

Sample Final Exam Questions

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

CS61A Midterm 2 Review (v1.1)

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

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

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Project 2: Scheme Interpreter

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

Fall 2017 December 4, Scheme. Instructions

SCHEME AND CALCULATOR 5b

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

Structure and Interpretation of Computer Programs

Fall 2017 December 4, 2017

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

Structure and Interpretation of Computer Programs

CS 314 Principles of Programming Languages. Lecture 16

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

CSE413 Midterm. Question Max Points Total 100

Functional Programming. Pure Functional Programming

Structure and Interpretation of Computer Programs

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?

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

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

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

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

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

Functional Programming. Pure Functional Languages

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

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

User-defined Functions. Conditional Expressions in Scheme

GENERATORS AND STREAMS

Documentation for LISP in BASIC

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

Lisp Basic Example Test Questions

Functional Programming. Pure Functional Languages

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

ITERATORS AND STREAMS 9

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

Structure and Interpretation of Computer Programs

Turtles All The Way Down

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

CS 314 Principles of Programming Languages

Introductory Scheme. Revision 1

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

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

TAIL RECURSION, SCOPE, AND PROJECT 4 11

YOUR NAME PLEASE: *** SOLUTIONS ***

A Brief Introduction to Scheme (II)

Functional programming with Common Lisp

LINKED LISTS AND MIDTERM REVIEW 6

Midterm Examination (Sample Solutions), Cmput 325

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

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

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

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

Functional Programming - 2. Higher Order Functions

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

FP Foundations, Scheme

University of Massachusetts Lowell

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

Structure and Interpretation of Computer Programs

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

SQL AND FINAL REVIEW

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

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

Project 5 - The Meta-Circular Evaluator

LINKED LISTS AND MIDTERM REVIEW

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

Lexical vs. Dynamic Scope

CSE 341 Section Handout #6 Cheat Sheet

Structure and Interpretation of Computer Programs

Building a system for symbolic differentiation

An Explicit-Continuation Metacircular Evaluator

6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

Structure and Interpretation of Computer Programs

by the evening of Tuesday, Feb 6

Principles of Programming Languages 2017W, Functional Programming

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

(scheme-1) has lambda but NOT define

MIDTERM EXAMINATION - CS130 - Spring 2005

INTERPRETERS AND TAIL CALLS 9

Building a system for symbolic differentiation

CSC 533: Programming Languages. Spring 2015

6.001 recitation 3/21/07

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

CS 314 Principles of Programming Languages

Programming Languages

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


CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

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

UMBC CMSC 331 Final Exam

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

Transcription:

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help determine this. 1

GROUP TUTORING HANDOUT 7: MORE SCHEME Page 2 (a (cons (cons 1 nil (cons 2 (cons (cons 3 (cons 4 5 (cons 6 nil ((1 2 (3 4. 5 6 (b (define a 4 ((lambda (x y (+ a 1 2 4 (c ((lambda (x y z (y x 2 / 2 0.5 (d ((lambda (x (x x (lambda (y 4 4 (e (define boom1 (/ 1 0 Error: Zero Division (f boom1 Error: boom1 not defined (g (define boom2 (lambda ( (/ 1 0 boom2 (h (boom2 Error: Zero Division

GROUP TUTORING HANDOUT 7: MORE SCHEME Page 3 (i Why/How are the two boom definitions above different? The first line is setting boom1 to be equal to the value (/ 1 0, which turns out to be an error. On the other hand, boom2 is defined as a lambda that takes in no arguments that, when called, will evaluate (/ 1 0. (j How can we rewrite boom2 without using the lambda operator? (define (boom2 (/ 1 0 2. What will Scheme output? (a (if (/ 1 0 1 0 Error: Zero Division (b (if 1 1 (/ 1 0 1 (c (if 0 (/ 1 0 1 Error: Zero Division (d (and 1 (/ 1 0 (e (and 1 2 3 3

GROUP TUTORING HANDOUT 7: MORE SCHEME Page 4 (f (or 0 (/ 1 0 0 (g (or (/ 1 0 3 4 Error: Zero Division (h (and (and (or (i Given the lines above, what can we say about interpreting if expressions and booleans in Scheme? if functions and boolean expressions will short-circuit, just like in Python. All values have a boolean value of #t unless they are specifically. This means that unlike in Python, 0 and 1 are both considered #t! 3. The following line of code does not work. Why? Write the lambda equivalent of the let expressions. (let ((foo 3 (bar (+ foo 2 (+ foo bar The above function will error because it is equivalent to: ((lambda (foo bar (+ foo bar 3 (+ foo 2 In other words, foo has not been defined in the global frame. When bar is being assigned to (+ foo 2, it will error. The assignment of foo to 3 happens in the lambda?s frame when it s called, not the global frame (this is relevant to the Scheme project when the interpreter sees lambda, it will call a function to start a new frame. If we had the line (define foo 3 before the call to let, then it would return 8, because within let, foo would be 3 and bar would be (+ 3 2, since it would use the foo in the Global frame.

GROUP TUTORING HANDOUT 7: MORE SCHEME Page 5 2 Scoping 4. What is the difference between dynamic and lexical scoping? Lexical: The parent of a frame is the frame in which a procedure was defined (used in Python. Dynamic: The parent of a frame is the frame in which a procedure is called (keep an eye out for this in the Scheme project. 5. What would this print using lexical scoping? What would it print using dynamic scoping? a = 2 def foo(: a = 10 return lambda x: x + a bar = foo( bar(10 Lexical: 20 Dynamic: 12 6. How would you modify and environment diagram to represent dynamic scoping? Assign parents when you create a frame (do not set parents when defining functions!. The parent in this case is the frame in which you called this function.

GROUP TUTORING HANDOUT 7: MORE SCHEME Page 6 7. Implement waldo. waldo returns #t if the symbol waldo is in a list. You may assume that the list passed in is well-formed. scm> (waldo '(1 4 waldo #t scm> (waldo '( scm> (waldo '(1 4 9 Extra challenge: Define waldo so that it returns the index of the list where the symbol waldo was found (if waldo is not in the list, return. scm> (waldo '(1 4 waldo 2 scm> (waldo '( scm> (waldo '(1 4 9 (define (waldo lst (cond ((null? lst ((eq? (car lst 'waldo #t (else (waldo (cdr lst Challenge solution: (define (waldo lst (define (helper lst index (cond ((null? lst ((eq? (car lst 'waldo index (else (helper (cdr lst (+ index 1 (helper lst 0

GROUP TUTORING HANDOUT 7: MORE SCHEME Page 7 3 Challenge Question 8. (Optional From CS61A Fall 2017 Discussion 6: The quicksort sorting algorithm is an efficient and commonly used algorithm to order the elements of a list. We choose one element of the list to be the pivot element and partition the remaining elements into two lists: one of elements less than the pivot and one of elements greater than the pivot. We recursively sort the two lists, which gives us a sorted list of all the elements less than the pivot and all the elements greater than the pivot, which we can then combine with the pivot for a completely sorted list. Implement quicksort in Scheme. Choose the first element of the list as the pivot. You may assume that all elements are distinct. Hint: you may want to use a helper function. scm> (quicksort (list 5 2 4 3 12 7 (2 3 4 5 7 12 (define (quicksort lst (define (helper lst pivot less greater (cond ((null? lst (append (qs less (list pivot (qs greater ((> pivot (car lst (helper (cdr lst pivot (append (list (car lst less greater ((< pivot (car lst (helper (cdr lst pivot less (append (list (car lst greater (if (or (null? lst (null? (cdr lst lst (helper (cdr lst (car lst nil nil