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

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

SCHEME AND CALCULATOR 5b

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

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

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

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

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

Lisp. Versions of LISP

Lecture #24: Programming Languages and Programs

Functional Programming

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

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

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

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

Documentation for LISP in BASIC

CS450 - Structure of Higher Level Languages

FP Foundations, Scheme

Functional Programming. Pure Functional Languages

CSCC24 Functional Programming Scheme Part 2

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming. Pure Functional Languages

User-defined Functions. Conditional Expressions in Scheme

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

Project 2: Scheme Interpreter

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

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

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

Chapter 15 Functional Programming Languages

CS 314 Principles of Programming Languages

Scheme as implemented by Racket

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

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

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

CS 275 Name Final Exam Solutions December 16, 2016

ITERATORS AND STREAMS 9

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Principles of Programming Languages 2017W, Functional Programming

A Brief Introduction to Scheme (II)

CS 360 Programming Languages Interpreters

An introduction to Scheme

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

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

CSCI337 Organisation of Programming Languages LISP

Streams and Evalutation Strategies

Programming Languages

Typed Scheme: Scheme with Static Types

Chapter 1. Fundamentals of Higher Order Programming

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

Chapter 15. Functional Programming Languages

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

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

Project 1: Scheme Pretty-Printer

Functional Programming - 2. Higher Order Functions

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

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

Scheme: Strings Scheme: I/O

Functional Programming. Big Picture. Design of Programming Languages

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

CPS 506 Comparative Programming Languages. Programming Language Paradigm

More About Recursive Data Types

LECTURE 16. Functional Programming

Functional programming in LISP

(scheme-1) has lambda but NOT define

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

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

A LISP Interpreter in ML

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

Discussion 11. Streams

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

6.001 Notes: Section 8.1

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

University of Massachusetts Lowell

Scheme Quick Reference

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

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

Concepts of programming languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Scheme Quick Reference

Functional Programming. Pure Functional Programming

A Small Interpreted Language

Administrivia. Simple data types

MetacircularScheme! (a variant of lisp)

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS 314 Principles of Programming Languages. Lecture 16

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

CS 314 Principles of Programming Languages

INTERPRETATION AND SCHEME LISTS 12

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

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

Functional abstraction

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

CS61A Midterm 2 Review (v1.1)

Common LISP Tutorial 1 (Basic)

CSc 520 Principles of Programming Languages

CS1102: Macros and Recursion

Transcription:

SCHEME COMPUTER SCIENCE 6A October 29th, 202 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. Scheme is a dialect of the Lisp programming language, a language dating back to 958. The popularity of Scheme within the programming language community stems from its simplicity in fact, previous versions of CS 6A were taught in the Scheme language. The Scheme Interpreter Like Python, the Scheme language also has an interactive interpreter. accounts, you can access this by typing in stk. On your class nova [46] # stk Warning: DISPLAY variable is not set. Graphics will NOT be initialized. Welcome to the STk interpreter version 4.0.-ucb.3.6 [SunOS-5.0-sun4u] Copyright (c 993-999 Erick Gallesio - I3S - CNRS / ESSI <eg@unice.fr> Modifications by UCB EECS Instructional Support Group Questions, comments, or bug reports to <inst@eecs.berkeley.edu>. STk> Keep this one thing in mind: Everything is an expression. 2 Primitives Scheme has a certain set of atomic primitive expressions. What atomic means is that these expressions cannot be divided up, or at least, not usually created out of smaller

DISCUSSION : SCHEME Page 2 expressions. The ones we ll use the most are ones representing numbers (i.e., integers and floats, the two boolean values and symbols. Scheme also has strings but we ll only use them a little. STk> 23 23 STk> 23.23 23.23 STk> a ; this is a symbol a STk> #t #t STk> #f #f STk> "asdf" "asdf" You ll notice that numbers work pretty much the same in Scheme as they do in Python. Also, instead of True and False, we have #t and #f. Before explaining symbols though, let s look at how we define variables: STk> (define a 3 a STk> a 3 You ll notice this is a lot different. In Scheme, you need to have parentheses and inside, have define, then the name of the variable and then the expression (which first gets evaluated that you want bound to the name. Notice that the space between each of the parts (except the parens matters. Also, notice that to refer back to a, I just typed in a just as in Python. The thing is- a is a symbol. When you type things into the interpreter, Scheme will evaluate it. The rule for evaluating a symbol is to get the value bound to that symbol. This is one difference between strings and symbols- symbols don t evaluate to themselves. However, as you saw above, when you type in a, you get a. This is because when you use the single quote, you re telling Scheme not to follow the normal rules of evaluation and just have the symbol return as itself. You ll also notice that you can use the single quotes on integers and floats, and the boolean values also. However they re unnecessary because those evaluate to themselves. Finally, let s revisit what you wrote when you did (define a 3. Both define and a are symbols! CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 3 2. Questions. What gets printed STk> (define a STk> a STk> (define b a STk> b STk> (define c a STk> c 3 Evaluating Function Calls and Special Forms Now just defining variables and printing out primitives isn t very useful. You want to call functions too: STk> (+ 2 3 STk> (- 2 3 - STk> (* 6 3 8 STk> (/ 5 2 2.5 STk> (+ (* 3 4 3 3. Functions Now you might notice that Scheme does function calls differently. In Scheme, to a call a function, you give the symbol for the function name first, then you give the arguments (remember the spaces. But just as in Python, what you do is you evaluate the operator (the first expression to the right of the (, then you evaluate each of the arguments and then apply those evaluated arguments to the thing returned by the first expression. So when you evaluate (+ 2, you evaluate the + symbol which is bound to a built-in CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 4 addition function, then you evaluate and 2. Finally, you apply and 2 to the function value bound to +. Some important functions you ll want to use are: +, -, *, / eq, =, >, >=, <, <= 3.2 Questions. What do the following return (+ (* 3 (+ (* 3 3 (* 4 4 (define a (define b 3 3.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. How it gets evaluated however is that first, <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. 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 short circuiting 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 CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 5 #t STk> (and #f (/ 0 #f STk> (not 3 #f STk> (not #t #f 3.4 Questions. What does the following do STk> (if (or #t (/ 0 (/ 0 STk> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 STk> ((if (< 4 3 + - 4 00 4 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. 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 CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 6 You might notice that this is a little tedious though. Luckily Scheme has a way outdefine: 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. 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 (let ( (recursive-step (sin (/ x 3 (- (* 3 recursive-step (* 4 (expt recursive-step 3 4. Questions. Write a function that calculates factorial. (Note how you haven t been told any methods for iteration. (define (factorial x CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 7 2. Write a function that calculates the Nth fibonacci number (define (fib n (if (< n 2 5 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 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 CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 8 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 why Scheme is cool. It can be defined within itself. 5. 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 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 3. Fill out the following to complete an abstract type for binary trees: (define (make-btree entry left right (cons entry (cons left right (define (entry tree CS6A Fall 202: John Denero, with

DISCUSSION : SCHEME Page 9 (define (left tree (define (right tree 4. Using the above definition, write a function that sums over the binary tree. For our purposes, assume that if there is no left or if there is no right branch, the values for those should return 0. (define (btree-sum tree CS6A Fall 202: John Denero, with