SCHEME AND CALCULATOR 5b

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

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

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

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

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

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

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

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

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

INTERPRETERS AND TAIL CALLS 9

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

EXCEPTIONS, CALCULATOR 10

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

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

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

Documentation for LISP in BASIC

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

INTERPRETATION AND SCHEME LISTS 12

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

INTERPRETATION AND SCHEME LISTS 12

CS61A Midterm 2 Review (v1.1)

CALCULATOR Calculator COMPUTER SCIENCE 61A. July 24, 2014

(scheme-1) has lambda but NOT define

Lecture #24: Programming Languages and Programs

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

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

Lisp. Versions of LISP

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

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

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

Functional Programming. Pure Functional Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

CS 314 Principles of Programming Languages

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

An introduction to Scheme

Principles of Programming Languages 2017W, Functional Programming

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Principles of Programming Languages

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

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

Project 2: Scheme Interpreter

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Administrivia. Simple data types

Functional Programming. Pure Functional Languages

Functional Programming

FP Foundations, Scheme

An Explicit Continuation Evaluator for Scheme

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

6.001 Notes: Section 15.1

CSCC24 Functional Programming Scheme Part 2

CS 314 Principles of Programming Languages. Lecture 16

Scheme: Strings Scheme: I/O

MetacircularScheme! (a variant of lisp)

A Small Interpreted Language

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

CSc 520 Principles of Programming Languages

Discussion 11. Streams

Scheme as implemented by Racket


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

6.001 Notes: Section 8.1

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

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

CSE 341 Section Handout #6 Cheat Sheet

CS 360 Programming Languages Interpreters

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

User-defined Functions. Conditional Expressions in Scheme

An Explicit-Continuation Metacircular Evaluator

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

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

CSc 520 Principles of Programming Languages

Streams and Evalutation Strategies

Discussion 12 The MCE (solutions)

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Structure and Interpretation of Computer Programs

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

ITERATORS AND STREAMS 9

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

LECTURE 16. Functional Programming

Programming Languages

Imperative, OO and Functional Languages A C program is

RECURSION AND LINKED LISTS 3

Functional Programming. Pure Functional Programming

Discussion 4. Data Abstraction and Sequences

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

April 2 to April 4, 2018

Programming Project #4: A Logo Interpreter Summer 2005

CS61A Notes Week 13: Interpreters

Building a system for symbolic differentiation

Transcription:

SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually write a Scheme interpreter in Project 4. This discussion will give you some more practice with Scheme and introduce you to interpreting computer programs. 0. Warmup What Would Scheme Do STk> (define a ) STk> a STk> (define b a) STk> b STk> (define c a) STk> c STk> (define (foo x) (+ x )) STk> (define (bar x) (* x (+ x ))) STk> (bar (foo 3))

DISCUSSION 5B: SCHEME AND CALCULATOR Page 2 Evaluating Function Calls and Special Forms. Functions Now, you might notice that Scheme does function calls differently. To call a function in Scheme, first give the symbol for the function name, then give the arguments (remember the spaces!). But just as in Python, you evaluate the operator (the leftmost expression between the parentheses) before evaluating the arguments left to right. Then, apply the operator to the evaluated arguments. So when you evaluate (+ 2), first evaluate the + symbol which is bound to a builtin addition function. Then, evaluate the primitives and 2. Finally, apply the addition function to the arguments. Some important functions you ll want to use are: +, -, *, / eq, =, >, >=, <, <=.2 Questions. What do the following return (+ ) (* 3) (= (+ 2 ) (*.5 2)).3 Special Forms However, there are certain things that look like function calls that aren t. These are called special forms and have their own rules for evaluation. You ve already seen one- define where, of course, the first argument can t be evaluated (or else it d search for unbound variables!). Another one we ll use for this class is if. An if expression looks like this: (if <CONDITION> <THEN> <ELSE>), where <CONDITION>, <THEN>, and <ELSE> are expressions. It s evaluated exactly as it is in Python. First, the <CONDITION> is evaluated. If it evaluates to #f, then <ELSE> is evaluated. Otherwise, <THEN> is evaluated. Everything that is not #f is a true expression. CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 3 STk> (if this-evaluates-to-true 2) STk> (if #f (/ 0) this-is-returned) this-is-returned There are also special forms for the boolean operators which exhibit the same shortcircuiting behavior that you see in Python. The return values are either the value that lets you know the expression evaluates to a true value or #f. STk> (and 2 3) 3 STk> (or 2 3) STk> (or #t (/ 0)) #t STk> (and #f (/ 0)) #f STk> (not 3) #f STk> (not #t) #f.4 Questions STk> (if (or #t (/ 0)) (/ 0)) STk> (if (> 4 3) (+ 2 3 4) (+ 3 4 (* 3 2))) STk> ((if (< 4 3) + -) 4 00) 2 Lambdas, Environments, and Defining Functions Scheme has lambdas too! In fact, lambdas are more powerful in Scheme than in Python. The syntax is (lambda (<PARAMETERS>) <EXPR>). Like in Python, lambdas are function values. Likewise, in Scheme, when a lambda expression is called, a new frame is created where the symbols defined in the <PARAMETERS> section are bound to the arguments passed in. Then, <EXPR> is evaluated under this new frame. Note that <EXPR> is not evaluated until the lambda value is called. CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 4 STk> (define x 3) x STk> (define y 4) y STk> ((lambda (x y) (+ x y)) 6 7) 3 Like in Python, lambda functions are also values! So you can do this to define functions: STk> (define square (lambda (x) (* x x))) square STk> (square 4) 6 Luckily Scheme has a way out- You might notice that this is a little tedious though. define: STk> (define (square x) (* x x)) square STk> (square 5) 25 When you do (define (<FUNCTIONNAME> <PARAMETERS>) <EXPR>), Scheme will automatically transform it to (define <FUNCTIONNAME> (lambda (<PARAMETERS>) <EXPR>) for you. In this way, lambdas are more foundational to Scheme than they are to Python. Unlike Python lambdas, Scheme lambdas can have more than one statement. There is also another special form based around lambda- let. The structure of let is as follows: (let ( (<SYMBOL> <EXPR>)... (<SYMBOLN> <EXPRN>) ) <BODY> ) This special form really just gets transformed to: ( (lambda (<SYMBOL>... <SYMBOLN>) <BODY>) <EXPR>... <EXPRN>) You ll notice that what let does then is bind symbols to expressions. For example, this is useful if you need to reuse a value multiple times, or if you want to make your code more readable: (define (sin x) (if (< x 0.00000) x CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 5 (let ( (recursive-step (sin (/ x 3))) ) (- (* 3 recursive-step) (* 4 (expt recursive-step 3)))))) 2. Questions. Write a function that calculates factorial. (Note how you haven t been told any methods for iteration.) (define (factorial x) 2. Write a function that calculates the Nth fibonacci number (define (fib n) (if (< n 2) ) ) 3 Pairs and Lists So far, we have lambdas and a few atomic primitives. How do we create larger more complicated data structures Well, the most important data-structure from which you ll build most complex data structures out of is the pair. A pair is an abstract data type that has the constructor cons which takes two arguments, and it has two accessors car and cdr which get the first and second argument respectively. car and cdr don t stand for anything really now but if you want the history go to http://en.wikipedia.org/ wiki/car_and_cdr STk> (define a (cons 2)) a STk> a (. 2) STk> (car a) STk> (cdr a) 2 CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 6 Note that when a pair is printed, the car and cdr element are separated by a period. A common data structure that you build out of pairs is the list. A list is either the empty list whose literal is (), also known as nil, another primitive, or it s a cons pair where the cdr is a list. (Note the similarity to Rlists!) STk> () () STk> nil () STk> (cons (cons 2 nil)) ( 2) STk> (cons (cons 2 (cons 3 nil))) ( 2 3) Note that there are no dots here. That is because when a dot is followed by the left parenthesis, the dot and the left parenthesis are deleted; when a left parenthesis is deleted, its matching right parenthesis is deleted also. You can check if a list is nil by using the null function. A shorthand for writing out a list is: STk> ( 2 3) ( 2 3) STk> (define (square x) (* x x)) (define (square x) (* x x)) You might notice that the return value of the second expression looks a lot like Scheme code. That s because Scheme code is made up of lists. When you use the single quote, you re telling Scheme not to evaluate the list, but instead keep it as just a list. This is one of the reasons why Scheme is so cool it can be defined within itself! 3. Questions. Define map where the first argument is a function and the second a list. This should work like Python s map. (define (map fn lst) ) CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 7 2. Define reduce where the first argument is a function that takes two arguments, the second a default value and the third a list. This should work like Python s reduce. (define (reduce fn s lst) ) 4 Calculator In lecture, you saw how to implement the Calculator language using regular Python. The Calculator is a Scheme-like language that can handle the four basic arithmetic operations. These operations can be nested and can take varying numbers of arguments. Our goal now is to prepare for Project 4 by understanding the pieces of the Calculator interpreter. 4. Representing Expressions There are two kinds of expressions. A call expression is a Scheme list where the first element is the operator and each of the remaining elements is an operand. A primitive expression is an operator symbol or number. When we type a line at the Calculator prompt and hit enter, we ve just sent an expression to the interpreter. To represent Scheme lists in Python, we ll be using Pair objects. Pairs are just like the Rlists you ve come to know and love! 4.2 Questions. Translate the following Python representation of Calculator expressions into the proper Scheme representation: >>> Pair( +, Pair(, Pair(2, Pair(3, Pair(4, nil))))) (+ 2 3 4) >>> Pair( +, Pair(, Pair(Pair( *, Pair(2, Pair(3, nil))), nil))) >>> Pair( *, Pair(Pair( *, Pair(, Pair(2, nil))), Pair(3, nil))) 2. Translate the following Calculator expression into calls to the Pair constructor: > (+ 2 (- 3 4)) CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 8 4.3 Evaluating and Applying Evaluation discovers the form of an expression and executes the corresponding evaluation rule. Primitive expressions are evaluated directly. Call expressions are evaluated recursively as we ve seen before. First, evaluate the operator, then the operands left to right. Then, collect their values as a list of arguments and apply the operator to those arguments. If you refer to the minicalc.py file, you can see that all we ve done in the calc eval function is follow the rules of evaluation that were just outlined! If the expression is primitive (not a Pair), simply return it. Otherwise, evaluate the operands and apply the operator to the evaluated operands. We apply the operator with the calc apply function, which is a dispatch function that will dispatch on the operator name. Depending on what the operator is, we can match it to a corresponding Python call. Each conditional clause above handles the application of one operator. One way to remember the two functions: calc eval deals with expressions, whereas calc apply deals with values. 4.4 Questions. Suppose we typed each of the following expressions into the Calculator interpreter. How many calls to calc eval would they each generate How about calc apply > (+ 2 4 6 8) > (+ 2 (* 4 (- 6 8))) 2. We also want to be able to perform division, as in (/ 4 2). Supplement the existing code to handle this. If division by 0 is attempted, you should raise a ZeroDivision- Error. (Hint: a helper function that does something like Python s in operator for a Scheme list may be helpful here!) def scheme_in(elem, scheme_lst): CS6A Summer 203: Steven Tang and Eric Tzeng, with

DISCUSSION 5B: SCHEME AND CALCULATOR Page 9 def calc_apply(op, args):... if op == / : 3. Alyssa P. Hacker and Ben Bitdiddle are also tasked with implementing the and operator, as in (and (= 2) (< 3 4)). Ben says this is easy: they just have to follow the same process as in implementing * and /. Alyssa is not so sure. Who s right 4. Now that you ve had a chance to think about it, you decide to try implementing and yourself. You may assume the conditional operators (e.g. <, >, =, etc) have already been implemented for you. CS6A Summer 203: Steven Tang and Eric Tzeng, with