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

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

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

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

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

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

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

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

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

Functional Programming. Pure Functional Languages

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

Functional Programming. Pure Functional Languages

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

An introduction to Scheme

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

Documentation for LISP in BASIC

Lisp. Versions of LISP

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

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

Functional Programming

Functional Programming. Pure Functional Programming

Functional Programming - 2. Higher Order Functions

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Principles of Programming Languages 2017W, Functional Programming

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

Lecture #24: Programming Languages and Programs

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

CS450 - Structure of Higher Level Languages

Introduction to Scheme

Chapter 15 Functional Programming Languages

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

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

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

Functional programming with Common Lisp

Programming Languages

CS61A Midterm 2 Review (v1.1)

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS 314 Principles of Programming Languages


A Brief Introduction to Scheme (II)

6.001: Structure and Interpretation of Computer Programs

Organization of Programming Languages CS3200/5200N. Lecture 11

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

LECTURE 16. Functional Programming

CS 275 Name Final Exam Solutions December 16, 2016

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

INTERPRETERS AND TAIL CALLS 9

April 2 to April 4, 2018

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSCC24 Functional Programming Scheme Part 2

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

Chapter 1. Fundamentals of Higher Order Programming

by the evening of Tuesday, Feb 6

FP Foundations, Scheme

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

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

CS 360 Programming Languages Interpreters

CSC 533: Programming Languages. Spring 2015

ITERATORS AND STREAMS 9

Programming Languages

Scheme: Strings Scheme: I/O

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

Discussion 4. Data Abstraction and Sequences

An Explicit Continuation Evaluator for Scheme

CSE 341 Section Handout #6 Cheat Sheet

Scheme as implemented by Racket

Functional programming in LISP

A LISP Interpreter in ML

Project 2: Scheme Interpreter

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?

Common LISP-Introduction

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

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

University of Massachusetts Lowell

Scheme: Expressions & Procedures

Structure and Interpretation of Computer Programs

Chapter 15. Functional Programming Languages

6.001 Notes: Section 8.1

User-defined Functions. Conditional Expressions in Scheme

MetacircularScheme! (a variant of lisp)

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

CS 314 Principles of Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Common LISP Tutorial 1 (Basic)

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

CSc 520 Principles of Programming Languages

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

CS 314 Principles of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

CSCI337 Organisation of Programming Languages LISP

Structure and Interpretation of Computer Programs

Midterm Examination (Sample Solutions), Cmput 325

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

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

RECURSION AND LINKED LISTS 3

Transcription:

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 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 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 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>)

2 Scheme Questions 2.1 What would Scheme display? scm> (define a 1) scm> a scm> (define b a) scm> b scm> (define c 'a) scm> c 3 Call Expressions To call a function in Scheme, you first need a set of parentheses. Inside the parentheses, you specify an operator expression, then zero or more oprand subexpressions (remember the spaces!). 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 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.

Scheme 3 Questions 3.1 What would Scheme display? scm> (+ 1) scm> (* 3) scm> (+ (* 3 3) (* 4 4)) scm> (define a (define b 3)) scm> a scm> b 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 Expression 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 #f is a false-y value (also False for our interpreter); everything else is truth-y. scm> (if (< 4 5) 1 2) 1 scm> (if #f (/ 1 0) 42) 42

4 Scheme 4.2 Boolean Operators Much like Python, Scheme also has the boolean operators and, or, and not. In addition, and and or are also special forms because they are short-circuiting operators. scm> (and 25 32) 32 scm> (or 1 2) 1 Questions 4.1 What would Scheme display? 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.3 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. Like in Python, lambda functions are also values! functions: So you can do this to define scm> (define (square x) (* x x)) ; Create function square using define special form square scm> (define square (lambda (x) (* x x))) ; Equivalently, bind the name square to a lambda function square scm> (square 4) 16

Scheme 5 let is another special form based around lambda. The structure of let is as follows: (let ( (<SYMBOL1> <EXPR1>)... (<SYMBOLN> <EXPRN>) ) <BODY> ) This binds the results of evaluating expressions 1 through n to their associated symbols, creating temporary variables. Finally, the body of the let is evaluated. This special form is really just equivalent to: ( (lambda (<SYMBOL1>... <SYMBOLN>) <BODY>) <EXPR1>... <EXPRN>) Think of the temporary variables as being the parameters of a lambda function. Then, the arguments are the values of the expressions, which we bind to the temporary variables by calling the lambda. Consider the following example: (let ((x 1) (y 2)) (+ x y)) This is equivalent to: ((lambda (x y) (+ x y)) 1 2) Questions 4.1 Write a function that returns the factorial of a number. (define (factorial x) 4.2 Write a function that returns the n th Fibonacci number. (define (fib n) (if (or (= n 0) (= n 1)) n

6 Scheme 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 3 (cons 2 nil)) (3 2) scm> (define a (cons 3 (cons 2 nil))) a scm> (car a) 3 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 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 supply something else instead, creating a malformed list. The difference is shown with a dot: scm> (cons 2 3) (2. 3) scm> (cons 2 (cons 3 nil)) (2 3) scm> (cdr (cons 2 3)) 3 scm> (cdr (cons 2 (cons 3 nil))) (3) 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

Scheme 7 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. list takes zero or more arguments and returns a list of its arguments. This typically behaves much like quoting a list, except that quoting will not evaluate the list you have quoted which can result in some different outcomes. scm> (list 1 2 3) (1 2 3) scm> '(1 2 3) (1 2 3) scm> (car '(1 2 3)) 1 scm> (equal? '(1 2 3) (list 1 2 3)) #t scm> '(1. (2 3)) (1 2 3) scm> '(define (square x) (* x x)) (define (square x) (* x x)) scm> square ; We didn't actually define square above because of the quote Error scm> (list (cons 1 2)) ((1. 2)) scm> '((cons 1 2)) ((cons 1 2)) =, eq?, equal? = can only be used for comparing numbers. eq? behaves like == in Python for comparing two non-pairs (numbers, booleans, etc.). Otherwise, eq? behaves like is in Python. equal? compares pairs by determining if their cars are equal? and their cdrs are equal?(that is, they have the same contents). Otherwise, equal? behaves like eq?. scm> (define a '(1 2 3)) a scm> (= a a) Error scm> (equal? a '(1 2 3)) #t scm> (eq? a '(1 2 3)) #f scm> (define b a) b scm> (eq? a b) #t

8 Scheme Questions 5.1 Write a function which takes two lists and concatenates them. Notice that simply calling (cons a b) would not work because it will create a deep list. (define (concat a b) scm> (concat '(1 2 3) '(2 3 4)) (1 2 3 2 3 4) 5.2 Write a function that 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)

Scheme 9 5.3 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 function that takes a compressed sequence and expands it into the original sequence. Hint: You may want to use concat and replicate. (define (uncompress s) scm> (uncompress '((a 1) (b 2) (c 3))) (a b b c c c) 5.4 Write a function that takes a procedure and applies it to every element in a given list. (define (map fn lst) scm> (map (lambda (x) (* x x)) '(1 2 3)) (1 4 9) 5.5 Write a function that takes a procedure and applies to every element in a given nested list. The result should be a nested list with the same structure as the input list, but with each element replaced by the result of applying the procedure to that element. Use the built-in list? procedure to detect whether a value is a list. (define (deep-map fn lst) scm> (deep-map (lambda (x) (* x x)) '(1 2 3)) (1 4 9) scm> (deep-map (lambda (x) (* x x)) '(1 ((4) 5) 9)) (1 ((16) 25) 81)

10 Scheme 6 Extra Questions 6.1 Fill in the following to complete an abstract tree data type: (define (make-tree label branches) (cons label branches)) (define (label tree) (define (branches tree) 6.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, and also write a helper function for summing up the entries of a list. (define (tree-sum tree) 6.3 Using the abstract data type above, write a 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 a helper function that keeps track of the current product. 3 3 0 8 2 0 24 6 10 2 3 0 0 18 (define (path-product-tree t)