COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

Similar documents
CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

61A Lecture 16. Wednesday, October 5

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

Announcements. Homework 1 due Monday 10/12 (today) Homework 2 released next Monday 10/19 is due 11/2

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

CS61A Lecture 15 Object Oriented Programming, Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 12, 2012

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 14 Object Oriented Programming TODAY 7/11/2012 WHERE WE WERE: FUNCTIONAL PROGRAMMING

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

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

CS 314 Principles of Programming Languages. Lecture 16

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

CS61A Lecture 9 Immutable Data Structures. Jom Magrotker UC Berkeley EECS July 2, 2012

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

(scheme-1) has lambda but NOT define

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

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

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

61A Lecture 3. Friday, September 5

SCHEME AND CALCULATOR 5b

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

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

YESTERDAY: DISTRIBUTED COMPUTING

CS61A Lecture 28 Distributed Computing. Jom Magrotker UC Berkeley EECS August 6, 2012

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

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

CS61A Lecture 29 Parallel Computing. Jom Magrotker UC Berkeley EECS August 7, 2012

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 24 Infinite Sequences LAZY EVALUATION AGENDA 8/7/2012. Jom Magrotker UC Berkeley EECS July 30, 2012

CS61A Lecture 24 Infinite Sequences. Jom Magrotker UC Berkeley EECS July 30, 2012

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Structure and Interpretation of Computer Programs

Berkeley Scheme s OOP

User-defined Functions. Conditional Expressions in Scheme

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

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

CS61A Midterm 2 Review (v1.1)

Documentation for LISP in BASIC

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

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


CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

What Would Python Print? Tuples, Lists, Dictionaries

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

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

STRUCTURE AND INTERPRETATION COMPUTER PROGRAMS >>> COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 23 Py TODAY REVIEW: INTERPRETATION 7/26/2012 READ PRINT

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

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

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

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

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

CS61A Lecture 27 Logic Programming. Jom Magrotker UC Berkeley EECS August 2, 2012

STRUCTURE AND INTERPRETATION COMPUTER PROGRAMS COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 23 Py TODAY 7/26/2012

INTERPRETATION AND SCHEME LISTS 12

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

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

SCHEME INTERPRETER GUIDE 4

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

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

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

Structure and Interpretation of Computer Programs

Discussion 4. Data Abstraction and Sequences

CSE413 Midterm. Question Max Points Total 100

CS61A Lecture 26 Logic Programming. Jom Magrotker UC Berkeley EECS August 1, 2012

Structure and Interpretation of Computer Programs

CS61A Lecture 23 Py. Jom Magrotker UC Berkeley EECS July 26, 2012

Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Functional Programming. Pure Functional Programming

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

INTERPRETATION AND SCHEME LISTS 12

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

Functional Programming

CSE : Python Programming

Lecture #2 Kenneth W. Flynn RPI CS

TUPLES AND RECURSIVE LISTS 5

A brief tour of history

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

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

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

Administrivia. Simple data types

Structure and Interpretation of Computer Programs

Sample Final Exam Questions

Programming Paradigms in Python

Common LISP Tutorial 1 (Basic)

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

Functional programming in LISP

A Brief Introduction to Scheme (II)

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

& Object-Oriented Programming (POOP) Modified from CS61A Summer 2012 Lecture at UC Berkeley

April 2 to April 4, 2018

Structure and Interpretation of Computer Programs

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

61A Lecture 21. Friday, March 13

Transcription:

COMPUTER SCIENCE IN THE NEWS CS6A Lecture 2 Scheme Jom Magrotker UC Berkeley EECS July 24, 202 http://spectrum.ieee.org/tech talk/robotics/artificial intelligence/a texas hold em tournament for ais 2 TODAY Review: Dispatch Dictionaries Review: OOP Implementation Scheme, a new language REVIEW: DISPATCH DICTIONARIES Idea: We will allow ourselves one kind of data structure. In particular, we will represent an object by a dispatch dictionary, where the messages are the keys. 4 REVIEW: DISPATCH DICTIONARIES def make_pair(first, second): pair_dict = { first : first, second : second } return pair_dict DISPATCH DICTIONARIES >>> def make_pair(first, second):... >>> p = make_pair(, 2) G make_pair p How do we create and use pair objects now? E pair_dict first second 2 params: (first, second) body: pair_dict = {... } first second 2 6

>>> p[ first ] DISPATCH DICTIONARIES DISPATCH DICTIONARIES >>> p[ first ] = G G make_pair p make_pair p E pair_dict first second 2 params: (first, second) body: pair_dict = {... } first second 2 E pair_dict first second 2 params: (first, second) body: pair_dict = {... } first second 2 7 8 REVIEW: OOP IMPLEMENTATION: INHERITANCE Modified solution: def make_class(attributes={}, base_class=none): def get_value(name): To find the value of a class attribute if name in attributes: check if it is already in the return attributes[name] dictionary of attributes. elif base_class is not None: return base_class[ get ](name) def set_value(name, value): attributes[name] = value Otherwise, if there is a parent def set_value(name, value): class, check if the parent class has attributes[name] = value the class attribute. attributes = {} Dictionary of instance attributes instance = { get : get_value, set : set_value} A class is still a dictionary! The two new return instance An instance is a dictionary! The two messages get and set allow us to use the messages get and set allow us to use the general getter and setter functions. 9 general getter and setter functions. 0 cls = { get : get_value, set : set_value} return cls REVIEW: OOP IMPLEMENTATION: OBJECTS Just as we did with classes, we use dictionaries to represent objects: What class is this object an instance of? def make_instance(cls): def get_value(name): if name in attributes: return attributes[name] else: return cls[ get ](name) To find the value of an instance attribute check if it is already in the dictionary of attributes. Otherwise, check if the class has a value for the attribute. REVIEW: OOP IMPLEMENTATION: OBJECTS REVIEW: OOP IMPLEMENTATION: OBJECTS def make_instance(cls): def get_value(name): if name in attributes: return attributes[name] else: value = cls[ get ](name) return bind_method(value, instance) def bind_method(value, instance): if callable(value): else: def method(*args): return value(instance, *args) return method return value If the value provided is callable (for example, a function),... make the new bound method where the first argument is bound to the instance given, and all the other arguments remain the same and return this bound method. If the value provided is not callable, return it as is. 2 2

REVIEW: OOP IMPLEMENTATION: INSTANTIATION AND INITIALIZATION We add an extra message that our classes can understand, which will allow us to create new objects: def make_class(attributes={}, base_class=none):... def new(*args): return init_instance(cls, *args) cls = { get : get_value, set : set_value, new : new} return cls REVIEW: OOP IMPLEMENTATION: USAGE def make_pokemon_class(): def init (self, name, owner, hp): self[ set ]( name, name) self[ set ]( owner, owner) self[ set ]( hp, hp) def increase_hp(self, amount): old_hp = self[ get ]( hp ) self[ set ]( hp, old_hp + amount)... return make_class({ init : init, increase_hp : increase_hp,...}) 4 REVIEW: OOP IMPLEMENTATION: USAGE >>> Pokemon = make_pokemon_class() >>> ashs_pikachu = Pokemon[ new ]( Pikachu, Ash, 00) >>> ashs_pikachu[ get ]( hp ) 00 >>> ashs_pikachu[ get ]( owner ) Ash >>> ashs_pikachu[ get ]( increase_hp )(0) >>> ashs_pikachu[ get ]( hp ) 0 ANNOUNCEMENTS: MIDTERM 2 Midterm 2 is on Wednesday, July 2. Where? 200 VLSB. When? 7PM to 9PM. How much? Material covered until, and including, July 9. Closed book and closed electronic devices. One 8. x cheat sheet allowed. Group portion is minutes long. If you have a conflict, please let us know by the end of today, July 2. 6 ANNOUNCEMENTS Homework 0 is due Today. Project is due Thursday, July 26. Homework is due Friday, July 27. This will be released sometime tonight. Please ask for help if you need to. There is a lot of work in the weeks ahead, so if you are ever confused, consult (in order of preference) your study group and Piazza, your TAs, and Jom. Don t be clueless! SCHEME: A DIALECT OF LISP The language you will implement for Project 4. One of the oldest languages still in use today! Easiest way to learn Scheme is to start by seeing some examples and then play with it, so that s what most of today will be. 7 http://www.lisperati.com/logo.html 8

SCHEME: SIMPLE EXAMPLES SCHEME: SIMPLE EXAMPLES STk> STk> hello hello STk> (hello tom) (hello tom) STk> (+ 7) 2 STk> (* 22) 66 STk> (= 22) #f STk> (= ) #t STk> (equal? foo bar) #f STk> (equal? foo foo) #t STk> (if (= ) true false) true STk> (if (= 4) true) okay STk> (first word) w STk> (butfirst word) ord STk> (last word) d STk> (butlast word) wor STk> (word race car) racecar STk evaluates Scheme expressions to okay if there is no value to the expression. 9 20 SCHEME: SIMPLE EXAMPLES Scheme: Prefix Notation STk> (cond ((= 0) zero) ((= (remainder 2) 0) even) (else odd)) odd STk> In Python: 4 + 7 In Scheme (+ 4 7) Scheme uses prefix notation where all operations appear before the expressions used by the operation. There is absolutely no exceptions to this notation. This makes the language much simpler, since we don t have a bunch of different special cases for each type of operation. 2 SCHEME: VARIABLES STk> (define toms number ) toms number STk> toms number Variable names can have hyphens in Scheme STk> y *** Error: unbound variable: y... STk> (+ toms number 66) SCHEME: FUNCTIONS STk> (define (square x) (* x x)) square STk> (square ) 2 STk> square #[closure arglist=(x) 2a8a8] Notice that we didn t have to say return or anything like that! In Scheme, every function MUST return a value. This is always the last expression of the body. 2 24 4

SCHEME: FUNCTIONS STk> (lambda (x) (+ x )) Scheme has lambdas! #[closure arglist=(x) 2bbc8] Unlike Python, anything STk> ((lambda (x) (* x x)) ) you can do in a named 9 function you can also do STk> (define (call with 2 fn) in a lambda. (fn 2)) call with 2 STk> (call with 2 (lambda (x) (+ x ))) STk> (lambda () ) #[closure arglist=() blahblahblah] STk> ((lambda () )) 2 SCHEME: REPETITION STk> (define (fact n) (if (<= n ) (* n (fact ( n ))))) fact STk> (fact ) 20 Traditionally, even if the version of Scheme you re running has loops, it s much more common to stick to recursion. 26 SCHEME: MORE EXAMPLES BREAK: SOME LISP COMICS STk> (define (twice f) (lambda (x) (f (f x)))) twice STk> (((twice (twice twice)) +) 0) 6 STk> (define (make adder x) (lambda (y) (+ x y))) make adder STk> ((make adder 7) 22) 29 27 28 SCHEME: PAIRS AND LISTS STk> (cons 2 ) (2. ) STk> (list 2 4 6) ( 2 4 6) STk> (car ( 2 )) STk> (cdr ( 2 )) (2 ) STk> (cons ()) () STk> (cons (cons 2 (cons ()))) ( 2 ) SCHEME: LISTS STk> (append ( 2 ) (4 6)) ( 2 4 6) STk> (define (map fn lst) (if (null? lst) () (cons (fn (car lst)) (map fn (cdr lst))))) STk> (map (lambda (x) (* x )) ( 2 )) ( 0 ) STk> (map (lambda (x) (* x 22)) ( 2 )) (22 44 66) 29 0

PRACTICE: SCHEME What does each expression evaluate to? STk> (square (square )) STk> (square ) STk> (cons (car (. 4)) (cdr (. 6))) STk> (cons (car (. 4)) (cdr ( 6))) PRACTICE: SCHEME What does each expression evaluate to? STk> (square (square )) 62 STk> (square ) (square ) STk> (cons (car (. 4)) (cdr (. 6))) (. 6) STk> (cons (car (. 4)) (cdr ( 6))) ( 6) 2 PRACTICE: SCHEME Write the function filter which takes a predicate and a list and produces a new list only including the elements for which the predicate is true. CONCLUSION Scheme: It s awesome, it s simple, it s what you re implementing in project 4. STk> (filter (lambda (x) (= (remainder x 2) 0)) ( 2 4 6 7 8 9 0)) (2 4 6 8 0) STk> (filter (lambda (x) (< x )) ( 0 2 9 8)) ( 2 ) Preview: We will start to talk about how we implement an interpreter. 4 6