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

Similar documents
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

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

SCHEME AND CALCULATOR 5b

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

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

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

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

Lisp. Versions of LISP

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

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

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

Documentation for LISP in BASIC

An introduction to Scheme

Functional Programming. Pure Functional Programming

Functional Programming

CS 275 Name Final Exam Solutions December 16, 2016

User-defined Functions. Conditional Expressions in Scheme

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

Scheme as implemented by Racket

More About Recursive Data Types

Introduction to Scheme

A Brief Introduction to Scheme (II)

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

ITERATORS AND STREAMS 9

April 2 to April 4, 2018

CSCC24 Functional Programming Scheme Part 2

Principles of Programming Languages 2017W, Functional Programming

CS450 - Structure of Higher Level Languages

CS 314 Principles of Programming Languages

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

Lecture #24: Programming Languages and Programs

Organization of Programming Languages CS3200/5200N. Lecture 11

Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

CSC 533: Programming Languages. Spring 2015

CSE 341 Section Handout #6 Cheat Sheet

Functional programming with Common Lisp

LECTURE 16. Functional Programming

An Introduction to Scheme

MetacircularScheme! (a variant of lisp)

Chapter 1. Fundamentals of Higher Order Programming

FP Foundations, Scheme

University of Massachusetts Lowell

Project 1: Scheme Pretty-Printer

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


Administrivia. Simple data types

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

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

Chapter 15 Functional Programming Languages

CS 314 Principles of Programming Languages

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

Scheme Quick Reference

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

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

Programming Languages

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

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

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

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

Discussion 4. Data Abstraction and Sequences

Repetition Through Recursion

ENVIRONMENT DIAGRAMS AND RECURSION 2

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CS 314 Principles of Programming Languages

Chapter 15. Functional Programming Languages

Functional Programming - 2. Higher Order Functions

INTERPRETATION AND SCHEME LISTS 12

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

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Scheme Quick Reference

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

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

A LISP Interpreter in ML

Functional Programming. Big Picture. Design of Programming Languages

Putting the fun in functional programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS 314 Principles of Programming Languages. Lecture 16

Scheme: Strings Scheme: I/O

INTERPRETERS AND TAIL CALLS 9

CSc 520 Principles of Programming Languages

CS 360 Programming Languages Interpreters

Building a system for symbolic differentiation

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

Transcription:

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 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> 123 123 scm> 123.123 123.123 scm> #t True scm> #f False scm> 'a a ; this is a symbol

DISCUSSION 7: SCHEME Page 2 To define variables: scm> (define a 3 a scm> a 3 The define statement binds a value to a variable (just like the assignment operator in Python; in addition, define returns the variable name (in this case, a. More precisely, define returns the symbol a. As you saw above, when you type 'a, you also get the symbol 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. 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 7: SCHEME Page 3 3 Call Expressions Now, just defining variables and printing out primitives isn t very useful. You want to call functions too: scm> (+ 1 2 3 scm> (/ 5 2 2.5 scm> (+ 1 (* 3 4 13 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: +, -, *, / eq?, =, >, >=, <, <= 3.1 Questions 1. What would Scheme print? scm> (+ 1 scm> (* 3 scm> (+ (* 3 3 (* 4 4 scm> (define a (define b 3 scm> a scm> b

DISCUSSION 7: 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 False, then <ELSE> is evaluated. Otherwise, <THEN> is evaluated. Only False and #f evaluate to False; everything else is truth-y. scm> (if (< 4 5 1 2 1 scm> (if False (/ 1 0 42 42 4.2 Boolean operators Boolean operators (and and or are also special forms because they are short-circuiting operators (just like in Python. scm> (and 1 2 3 3 scm> (or 1 2 3 1 scm> (or True (/ 1 0 True scm> (and False (/1 0 False scm> (not 3 False scm> (not True False

DISCUSSION 7: SCHEME Page 5 4.3 Questions 1. What does Scheme print? scm> (if (or #t (/ 1 0 1 (/ 1 0 scm> (if (> 4 3 (+ 1 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 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 3 x scm> (define y 4 y scm> ((lambda (x y (+ x y 6 7 13 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 This can be a bit tedious though. Luckily Scheme has a shortcut: our old friend define: scm> (define (square x (* x x square scm> (square 5 25 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 7: 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 really just gets transformed 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 = 3 sin(x/3 4 sin 3 (x/3 to approximate sin(x for any x. (define (sin x (if (< x 0.000001 x (let ( (recursive-step (sin (/ x 3 (- (* 3 recursive-step (* 4 (expt recursive-step 3 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 7: SCHEME Page 7 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 in Scheme is the pair. A pair is an abstract data type with the constructor cons (which takes two arguments, and two selectors, car and cdr (which get the first and second argument respectively. 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> (define a (cons 1 2 a scm> a (1. 2 scm> (car a 1 scm> (cdr a 2 Note that when a pair is printed, the car and cdr elements are separated by a period. Remember, cons always takes in exactly two arguments. A common data structure that you build out of pairs is the list. A list is either the empty list, which is another primitive represented as ( or nil, or a cons pair where the cdr is a list. (Note the similarity to Links! scm> '( ( scm> nil ( scm> (cons 1 (cons 2 nil (1 2 Note that there are no dots here. When a dot is followed by a left parenthesis, the dot, left parenthesis, and matching right parenthesis are deleted. You can check if a list is nil with the null? function. A shorthand for writing out a list is: scm> '(1 2 3 (1 2 3 scm> '(define (square x (* x x (define (square x (* x x You might notice that the evaluation of the second expression looks a lot like Scheme code. That s because Scheme code is made up of lists! When you quote an expression (like a list, you re telling Scheme not to evaluate the expression, but instead keep it as is. This is one of the reasons why Scheme is cool it can be defined within itself!

DISCUSSION 7: SCHEME Page 8 5.1 Questions 1. Define map, which takes function fn and a list lst. It applies the function fn to every element of lst and returns a list containing the results. (define (map fn lst scm> (map (lambda (x (* x x '(1 2 3 (1 4 9 2. Define concat, which takes a list of lists, and constructs a list by concatenating all the elements together into one list. Use the built-in append function to concatenate two lists. (define (concat lsts scm> (append '(1 4 7 '(2 5 8 (1 4 7 2 5 8 scm> (concat '((1 4 7 (2 5 8 (3 6 9 (1 4 7 2 5 8 3 6 9

DISCUSSION 7: SCHEME Page 9 3. 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 3 (5 5 5 4. 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 3 (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 3 (a b b c c c

DISCUSSION 7: SCHEME Page 10 5. Define deep-apply, which takes a nested list and applies a given function to every element. For the purposes of this question, a nested list is either a single element (e.g. 4 a list of nested lists (e.g. (1 ((4 5 9. deep-apply should return a nested list with the same shape as the input list, but with each element replaced by the result of applying the given function to that element. Use the built-in list? function to detect whether a value is a list. (define (deep-apply fn nested-list scm> (deep-apply (lambda (x (* x x '(1 2 3 (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 7: SCHEME Page 11 6 Extra Questions 1. Fill in the following to complete an abstract tree data type: (define (make-tree entry children (cons entry children (define (entry tree (define (children 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

DISCUSSION 7: SCHEME Page 12 3. 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. 3 3 0 8 2 0 24 6 10 2 (define (path-product-tree t 3 0 0 18