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

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

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 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

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

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

SCHEME AND CALCULATOR 5b

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

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

An introduction to Scheme

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

Functional Programming. Pure Functional Languages

Documentation for LISP in BASIC

Functional Programming. Pure Functional Languages

Functional Programming

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

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

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming. Pure Functional Programming

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

Chapter 1. Fundamentals of Higher Order Programming

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

Lisp. Versions of LISP

LECTURE 16. Functional Programming

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

Functional programming with Common Lisp

April 2 to April 4, 2018

Organization of Programming Languages CS3200/5200N. Lecture 11

ITERATORS AND STREAMS 9

Introduction to Scheme

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

Lecture #24: Programming Languages and Programs

University of Massachusetts Lowell

Programming Languages

Functional Programming - 2. Higher Order Functions

CS450 - Structure of Higher Level Languages

CS 314 Principles of Programming Languages

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

MetacircularScheme! (a variant of lisp)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

A Brief Introduction to Scheme (II)

Scheme as implemented by Racket

Principles of Programming Languages

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

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

User-defined Functions. Conditional Expressions in Scheme

Principles of Programming Languages 2017W, Functional Programming

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

CSCC24 Functional Programming Scheme Part 2

INTERPRETERS AND TAIL CALLS 9

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

Functional abstraction

6.001: Structure and Interpretation of Computer Programs

CSE 341 Section Handout #6 Cheat Sheet


Discussion 4. Data Abstraction and Sequences

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Programming Languages

RECURSION AND LINKED LISTS 3

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

CSC 533: Programming Languages. Spring 2015

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

Functional Programming Languages (FPL)

Chapter 15 Functional Programming Languages

Common LISP-Introduction

Structure and Interpretation of Computer Programs

A LISP Interpreter in ML

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

An Introduction to Scheme

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

CS 360 Programming Languages Interpreters

CS 275 Name Final Exam Solutions December 16, 2016

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

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

Functional Programming. Big Picture. Design of Programming Languages

Common LISP Tutorial 1 (Basic)

Reasoning About Programs Panagiotis Manolios

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

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

FP Foundations, Scheme

Chapter 15. Functional Programming Languages

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

Streams and Evalutation Strategies

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

Scheme Quick Reference

Structure and Interpretation of Computer Programs

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

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?

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

CPS 506 Comparative Programming Languages. Programming Language Paradigm

A Small Interpreted Language

Repetition Through Recursion

CS61A Midterm 2 Review (v1.1)

More About Recursive Data Types

Transcription:

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction 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 1958. The popularity of Scheme within the programming language community stems from its simplicity in fact, previous versions of CS 61A were taught in the Scheme language. 2 Primitives Scheme has a set of atomic primitive expressions. Atomic means that these expressions cannot be divided up. scm> 12 12 scm> 12.12 12.12 scm> #t True scm> #f False Unlike in Python, the only primitive in Scheme that is a false value is #f and its equivalents, false and False. The define special form defines variables and procedures

DISCUSSION 8: SCHEME Page 2 by binding a value to a variable, just like the assignment statement in Python. When a variable is defined, the define special form returns a symbol of its name. A procedure is what we call a function in Scheme! The syntax to define a variable and procedure are: (define <variable name> <value> (define (<function name> <parameters><function body> scm> (define a ; a = a scm> a scm> (define (foo x x ; procedure named foo foo scm> (foo a 2.1 Questions 1. What would Scheme print? scm> (define a 1 scm> a scm> (define b a scm> b scm> (define c 'a scm> c

DISCUSSION 8: SCHEME Page Call Expressions Scheme call expressions follow prefix notation, where an operator is followed by zero or more operand subexpressions. Operators may be symbols, such as + and * or more complex expressions, as long as they evaluate to procedure values. scm> (- 1 1 ; 1-1 0 scm> (/ 8 4 2 ; 8 / 4 / 2 1 scm> (* (+ 1 2 (+ 1 2 ; (1 + 2 * (1 + 2 9 To call a function in Scheme, you first need a set of parentheses. Inside the parentheses, you specify a function, then the arguments (remember the spaces!. Evaluating a Scheme function call works just like Python: 1. Evaluate the operator (the first expression after the (, then evaluate each of the operands. 2. Apply the operator to those evaluated operands. When you evaluate (+ 1 2, you evaluate the + symbol, which is bound to a built-in addition function. Then, you evaluate 1 and 2, which are primitives. Finally, you apply the addition function to 1 and 2. Some important built-in functions you ll want to know are: +, -, *, / equal?, =, >, >=, <, <=.1 Questions 1. What would Scheme print? scm> (+ 1 scm> (* scm> (+ (* (* 4 4 scm> (define a (define b scm> a scm> b

DISCUSSION 8: SCHEME Page 4 4 Special Forms There are certain expressions that look like function calls, but don t follow the rule for order of evaluation. These are called special forms. You ve already seen one define, where the first argument, the variable name, doesn t actually get evaluated to a value. 4.1 If Statements Another common special form is the if form. An if expression looks like: (if <condition> <then> <else> where <condition>, <then> and <else> are expressions. First, <condition> is evaluated. If it evaluates to #t, then <then> is evaluated. Otherwise, <else> is evaluated. Remember that only False and #f are false-y values; everything else is truth-y. scm> (if (< 4 5 1 2 1 scm> (if #f (/ 1 0 42 42 4.2 Boolean Operators Scheme also has boolean operators and, or, and not like in Python! In addition, and and or are also special forms because they are short-circuiting operators. scm> (and 1 2 scm> (or 1 2 1 scm> (or True (/ 1 0 True scm> (and False (/1 0 False scm> (not False scm> (not True False

DISCUSSION 8: SCHEME Page 5 4. Questions 1. What does Scheme print? scm> (if (or #t (/ 1 0 1 (/ 1 0 scm> (if (> 4 (+ 1 2 4 (+ 4 (* 2 scm> ((if (< 4 + - 4 100 scm> (if 0 1 2 4.4 Lambdas and Defining Functions Scheme has lambdas too! The syntax is (lambda (<PARAMETERS><EXPR> Like in Python, lambdas are function values. Also like in Python, when a lambda expression is called in Scheme, a new frame is created where the parameters are bound to the arguments passed in. Then, <EXPR> is evaluated in this new frame. Note that <EXPR> is not evaluated until the lambda function is called. scm> (define x x scm> (define y 4 y scm> ((lambda (x y (+ x y 6 7 1 Like in Python, lambda functions are also values! So you can do this to define functions: scm> (define square (lambda (x (* x x square scm> (square 4 16 When you do (define (<FUNCTION NAME> <PARAMETERS> <EXPR>, Scheme will automatically transform it to (define <FUNCTION NAME> (lambda (<PARAMETERS> <EXPR>. In this way, lambdas are more central to Scheme than they are to Python.

DISCUSSION 8: SCHEME Page 6 4.5 Let There is also a special form based around lambda: let. The structure of let is as follows: (let ( (<SYMBOL1> <EXPR1>... (<SYMBOLN> <EXPRN> <BODY> This special form is really just equivalent to: ( (lambda (<SYMBOL1>... <SYMBOLN> <BODY> <EXPR1>... <EXPRN> let effectively binds symbols to expressions, then runs the body of the let form. This can be useful if you need to reuse a value multiple times, or if you want to make your code more readable. For example, we can use the approximation sin(x x (which is true for small x and the trigonometric identity sin(x = sin(x/ 4 sin (x/ to approximate sin(x for any x. (define (sin x (if (< x 0.000001 x (let ( (recursive-step (sin (/ x (- (* recursive-step (* 4 (expt recursive-step 4.6 Questions 1. Write a function that calculates factorial. (Note we have not seen any iteration yet. (define (factorial x 2. Write a function that calculates the n th Fibonacci number. (define (fib n (if (< n 2 1

DISCUSSION 8: SCHEME Page 7 5 Pairs and Lists To construct a (linked list in Scheme, you can use the constructor cons (which takes two arguments. nil represents the empty list. If you have a linked list in Scheme, you can use selector car to get the first element and selector cdr to get the rest of the list. (car and cdr don t stand for anything anymore, but if you want the history go to http://en.wikipedia.org/wiki/car_and_cdr. scm> nil ( scm> (null? nil #t scm> (cons 2 nil (2 scm> (cons (cons 2 nil ( 2 scm> (define a (cons (cons 2 nil a scm> (car a scm> (cdr a (2 scm> (car (cdr a 2 scm> (define (len a (if (null? a 0 (+ 1 (len (cdr a len scm> (len a 2

DISCUSSION 8: SCHEME Page 8 If a list is a good looking list, like the ones above where the second element is always a linked list, we call it a well-formed list. Interestingly, in Scheme, the second element does not have to be a linked list. You can give something else instead, but cons always takes exactly 2 arguments. These lists are called malformed list. The difference is a dot: scm> (cons 2 (2. scm> (cons 2 (cons nil (2 scm> (cdr (cons 2 scm> (cdr (cons 2 (cons nil ( In general, the rule for displaying a pair is as follows: use the dot to separate the car and cdr fields of a pair, but if the dot is immediately followed by an open parenthesis, then remove the dot and the parenthesis pair. Thus, (0. (1. 2 becomes (0 1. 2 There are many useful operations and shorthands on lists. One of them is list special form list takes zero or more arguments and returns a list of its arguments. Each argument is in the car field of each list element. It behaves the same as quoting a list, which also creates the list. scm> (list 1 2 (1 2 scm> '(1 2 (1 2 scm> (car '(1 2 1 scm> (equal? '(1 2 (list 1 2 #t scm> '(1. (2 (1 2 scm> '(define (square x (* x x (define (square x (* x x

DISCUSSION 8: SCHEME Page 9 1. Define a function that takes 2 lists and concatenates them together. Notice that simply calling (cons a b would not work because it will create a deep list. Instead, think recursively! (define (concat a b scm> (concat '(1 2 '(2 4 (1 2 2 4 2. Define replicate, which takes an element x and a non-negative integer n, and returns a list with x repeated n times. (define (replicate x n scm> (replicate 5 (5 5 5

DISCUSSION 8: SCHEME Page 10. A run-length encoding is a method of compressing a sequence of letters. The list (a a a b a a a a can be compressed to ((a (b 1 (a 4, where the compressed version of the sequence keeps track of how many letters appear consecutively. Write a Scheme function that takes a compressed sequence and expands it into the original sequence. Hint: try to use functions you defined earlier in this worksheet. (define (uncompress s scm> (uncompress '((a 1 (b 2 (c (a b b c c c

DISCUSSION 8: SCHEME Page 11 4. Define deep-apply, which takes a nested list and applies a given procedure to every element. deep-apply should return a nested list with the same structure as the input list, but with each element replaced by the result of applying the given procedure to that element. Use the built-in list? procedure to detect whether a value is a list. The procedure map has been defined for you. (define (map fn lst (if (null? lst nil (cons (fn (car lst (map fn (cdr lst (define (deep-apply fn nested-list scm> (deep-apply (lambda (x (* x x '(1 2 (1 4 9 scm> (deep-apply (lambda (x (* x x '(1 ((4 5 9 (1 ((16 25 81 scm> (deep-apply (lambda (x (* x x 2 4

DISCUSSION 8: SCHEME Page 12 6 Extra Questions 1. Fill in the following to complete an abstract tree data type: (define (make-tree root branches (cons root branches (define (root tree (define (branches tree 2. Using the abstract data type above, write a function that sums up the entries of a tree, assuming that the entries are all numbers. Hint: you may want to use the map function you defined above, as well as an additional helper function. (define (tree-sum tree. Using the abstract data type above, write a Scheme function that creates a new tree where the entries are the product of the entries along the path to the root in the original tree. Hint: you may want to write helper functions. 0 8 2 0 24 6 10 2 (define (path-product-tree t 0 0 18