! The simplest building blocks of a language. ! Compound elements are built from simpler ones

Similar documents
61A Lecture 2. Wednesday, September 4, 2013

61A Lecture 2. Friday, August 28, 2015

Lecture #3: Environments

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

Chapter 1. Fundamentals of Higher Order Programming

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

An introduction to Scheme

61A Lecture 3. Friday, September 5

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Discussion 12 The MCE (solutions)

61A Lecture 4. Monday, September 9

61A Lecture 9. Friday, September 20

CS61A Lecture 1. Amir Kamil UC Berkeley January 23, 2013

Chapter 1: Building Abstractions with Functions

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

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

functional programming in Python, part 2

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

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

61A Lecture 26. Monday, October 31

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

Introduction to Functional Programming (Python) John R. Woodward

SCHEME AND CALCULATOR 5b

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

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

Computer Science 102. Into to Computational Modeling Special Topics: Programming in Matlab

6.009 Fundamentals of Programming

CS61A Notes Week 13: Interpreters

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Recursive Functions. Recursive functions are built up from basic functions by some operations.

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

61A Lecture 6. Friday, September 7

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

Administrivia. Simple data types

Fall Lecture 3 September 4. Stephen Brookes

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Defining Functions III: Nested Definitions

DRAWING ENVIRONMENT DIAGRAMS

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Lecture 2: Big-Step Semantics

DATA ABSTRACTION 5. February 21, 2013

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

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

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

6.001: Structure and Interpretation of Computer Programs

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

DATA ABSTRACTION 4. September 25, 2014

What is AI? Knowledge-based systems in Bioinformatics, 1MB602, Definitions of AI. Acting humanly: Turing test. AI techniques. Intelligent agents

Functional Programming

Introduction to Functional Programming and Scripting (Exam Revision) John Woodward

6.001 Notes: Section 8.1

6.001 Notes: Section 6.1

MAT121: SECTION 2.7 ANALYZING GRAPHS AND PIECEWISE FUNCTIONS

ENVIRONMENT DIAGRAMS AND RECURSION 2

Recursion in Python. Recursive Functions. >>> 'pig'[0] 'p' >>> 'pig'[1:] 'ig'

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

Automating the Tedious Stuff (Functional programming and other Mathematica magic)

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

Execution order. main()

6.001 Notes: Section 15.1

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Functional Programming. Pure Functional Programming

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

CS1110. Lecture 6: Function calls

CS1110. Lecture 6: Function calls

Functional Programming. Big Picture. Design of Programming Languages

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

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

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

Lecture 13: Expression Evaluation

Built-in Types of Data

Python Programming Exercises 1

Lecture 2: SML Basics

PYTHON. Values and Variables

Hog Contest Rules. Fall 2011 Winners Kaylee Mann Yan Duan & Ziming Li Brian Prike & Zhenghao Qian Parker Schuh & Robert Chatham

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Introduction and Functions Summer 2018 Discussion 0: June 19, 2018

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Test #2 October 8, 2015

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

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

Structure and Interpretation of Computer Programs

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CS1 Lecture 5 Jan. 25, 2019

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

CS195H Homework 1 Grid homotopies and free groups. Due: February 5, 2015, before class

Principles of Programming Languages

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

Introduction to Programming II W4260. Lecture 2

C++ Programming Lecture 1 Software Engineering Group

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Transcription:

The Elements of Programming Primitive Expressions and Statements! The simplest building blocks of a language 61 Lecture Monday, ugust 9 Means of Combination! Compound elements are built from simpler ones Means of bstraction! Compound elements can be named and manipulated as units Programming languages allow us to communicate, too Functions and Data Types of expressions Data: Stuff we want to manipulate The rt of Computer Programming This slide Donald Knuth (Ka-NOOTH) Functions: Rules for manipulating data Count the words in a line of text dd numbers Load the next slide Pronounce someone s name 18 + 69 f(x) 1869 n expression describes a computation and evaluates to a value 6 sin π 3 393161 100 i i=1 69 18 3 Call Expressions in Python natomy of a Call Expression add (, 3 ) Operator Operand 0 Operand 1 Operators and operands are expressions ll expressions can use function call notation So they evaluate to values Evaluation procedure for call expressions: 1. Evaluate the operator and operand subexpressions. pply the function that is the value of the operator subexpression to the arguments that are the values of the operand subexpression 5 6

Evaluating Nested Expressions The Print Function 08 mul(add(, mul(, 6)), add(3, 5)) mul 6 add(, mul(, 6)) 8 add(3, 5) add mul(, 6) add 3 5 mul 6 7 8 Pure Functions & Non-Pure Functions Pure Functions -, 100 Function signature: how many parameters abs(number): pow(x, y): Only produces return values 16765060089019670305376 Nested Expressions with Print, display print(print(1), print()) Non-Pure Functions print print(1) print() - May create side effects 1 display - display 1 display 9 10 Names and ssignment Built-in function abs: abs(x): Imported value ssigned value max: max(a, b, c, ): pi: 3.1 tau: 6.8 from math import pi tau = * pi The environment does not track where names came from 11 1

User-Defined Functions frame holds name bindings abs: abs(x): max: max(a, b, c, ): Named values are a simple means of abstraction Named expressions are a more powerful means of abstraction def expressions: Create a new function pi: 3.1 tau: 6.8 Binding to a numeric value Bind a name to it from math import pi tau = * pi The environment does not track where names came from >>> def <name>(<formal parameters>): return <return expression> 13 1 User-Defined Functions Named values are a simple means of abstraction Named expressions are a more powerful means of abstraction mul(a,b): def expressions: Create a new function Bind a name to it mul(a,b): >>> def def 15 16 Bind formal parameters mul(a,b): Eval return expression x: - (-) & Values Expressions def (-) The environment created for the function body The existing environment in which the call expression is evaluated (-) def (-) 17 18

Evaluating a Name in an Environment mul(a,b): mul(a,b): name evaluates to the value bound to that name x: - (-) def (-) x: - Points to an environment, which starts with a frame in the earliest frame of the current environment in which that name is found 19 0 Intrinsic Function Names Don t Play a Role mul(a,b): Does not get inspected sum_s: add, mul, sum_s(x, y): return add((x), (y)) x: - Witness Protection Program Functions aren t accessed by their intrinsic names! def def sum_s(x, y): return add((x),(y)) 1 sum_s: add, mul, sum_s: add, mul, y: 1 sum_s(x, y): return add((x), (y)) y: 1 sum_s(x, y): return add((x), (y)) sum_s sum_s sum_s(5,1) sum_s(5,1) add((x),(y)) add((x),(y)) return def def sum_s(x,y): return add() sum_s(5, 1) 3 def def sum_s(x,y): return add() sum_s(5, 1)

sum_s: add, mul, sum_s: add, mul, y: 1 sum_s sum_s(5,1) sum_s(x, y): add((x),(y)) 5 (x) mul(x, x) return add((x), (y)) def def sum_s(x,y): return add() sum_s(5, 1) 5 y: 1 sum_s x: 1 sum_s(5,1) sum_s(x, y): 169 add((x),(y)) (x) 5 return add((x), (y)) 1 (y) mul(x, x) mul(x, x) def def sum_s(x,y): return add() sum_s(5, 1) 6