Rules of evaluation The value of a number is itself.

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Slide Copyright © 2009 Pearson Education, Inc. Topics An introduction to number theory Prime numbers Integers, rational numbers, irrational numbers,
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Lesson 7-1 Warm-Up.
Exponents and Powers Power – the result of raising a base to an exponent. Ex. 32 Base – the number being raised to the exponent. Ex is the base.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Chapter P Prerequisites: Fundamental Concepts of Algebra 1 Copyright © 2014, 2010, 2007 Pearson Education, Inc. 1 P.1 Algebraic Expressions, Mathematical.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Variables and Expressions Order of Operations Real Numbers and the Number Line Objective: To solve problems by using the order of operations.
Objective The student will be able to: translate verbal expressions into math expressions and vice versa. SOL: A.1.
1.1 Variable in Algebra 1.2 Exponents & Powers. A variable expression contains: 1) one or more numbers or variables, and 2) one or more operations. Examples:
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
The interpreter.
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Order of Operations &The Distributive Property
Introduction to Programming
Algebra Review Radical Expressions page 280
First Programs CSE 1310 – Introduction to Computers and Programming
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Functions, Procedures, and Abstraction
Procedural versus Functional Programming
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
6.001 SICP Environment model
Rules of evaluation The value of a number is itself.
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Objective The student will be able to:
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Environment model
Objective The student will be able to:
6.001 SICP Variations on a Scheme
A mathematical phase containing numbers.
Exponents and Order of Operations
A mathematical phase containing numbers.
Announcements Quiz 5 HW6 due October 23
Objective The student will be able to:
Lecture 2 מבוא מורחב.
6.001 SICP Environment model
CSE S. Tanimoto Lambda Calculus
Introduction to the Lab
Lecture 2 מבוא מורחב.
Objective The student will be able to:
Translating Words into Math
An algebraic expression contains:
Good programming practices
Functions, Procedures, and Abstraction
Presentation transcript:

Rules of evaluation The value of a number is itself. The value of a Boolean is itself. The value of a symbol is the data object it is bound to. The value of a list is obtained as follows: First evaluate each item in the list. The first item should evaluate to a procedure. Apply the procedure to the values of the rest of the items. The result of the procedure application is the value of the list. Questions: What does bound to mean? What is a procedure? What does it mean to apply a procedure?

We call these built-in procedures. Bindings When Dr. Racket starts, some symbols are bound to data objects: The symbol + is bound to the addition procedure. The symbol * is bound to the multiplication procedure. The symbol / is bound to the division procedure. The symbol - is bound to the subtraction procedure. Other symbols bound to procedures: The symbol abs is bound to the absolute-value procedure. The symbol sqrt is bound to the square-root procedure. The symbol cos is bound to the cosine procedure. The symbol sin is bound to the sine procedure. The symbol tan is bound to the tangent procedure. Show bindings We call these built-in procedures. There are also symbols bound to numbers: The symbol pi is bound to 3.141592653589793 The symbol e is bound to 2.718281828459045

More built-in procedures Procedures for which the output is Boolean: > >= < <= = equal? Show more bindings Many others.

Creating new bindings with define (define twice-pi (* 2 pi)) A definition is a request that asks Dr. Racket to bind a symbol to the value of some expression. (define twice-pi (* 2 pi)) Form of a definition: Show new binding A list with three parts: the symbol define the new symbol being bound an expression To execute the definition, Dr. Racket evaluates the expression, and binds the new symbol to the value of the expression. You cannot define the same symbol twice!

Why bind a symbol? Simplify code Reduce computation time Simplify future changes Perhaps you need to compute 6 3+4 2 + 6 3+4 3 You could do it like this: (+ (* (/ 6 (+ 3 4)) (/ 6 (+ 3 4)) (* (/ 6 (+ 3 4)) (/ 6 (+ 3 4)) (/ 6 + 3 4))) or you could first make a definition (define y (/ 6 (+ 3 4)) after which the expression can be written as (+ (* y y) (* y y y))

Simplify code Reduce computation time Simplify future changes Could have code that depends on, e.g. the number of students at Brown University. Want to be able to reuse the same code for Dartmouth College. Bind a symbol num_students to the number of students. Can make one change in the code. When a symbol is bound to a value, we call that symbol a variable in analogy to algebra. But do variables vary? Remember: You cannot define the same symbol twice!

Bindings made using define are called top-level bindings. (define y (/ 6 (+ 3 4)) The collection of top-level bindings is called the top-level environment. Expressions are said to be evaluated in the context of that environment. (+ (* y y) (* y y y)) We’ll soon see another kind of binding and another kind of environment.

Procedure-valued expressions How to create a new data object of type procedure? There is no text whose denotation is a procedure. …but there are expressions whose values are new procedures: lambda expressions Why lambda? A lambda expression is a list with exactly three items: the symbol lambda a list whose items are symbols (the argument list) an expression (the body) (lambda (x y) (* (+ x 1) (+ y 1))) arg list body

(lambda (x y) (* (+ x 1) (+ y 1))) A lambda expression is a list with exactly three items: the symbol lambda a list whose items are symbols (the argument list) an expression (the body) (lambda (x y) (* (+ x 1) (+ y 1))) arg list body

(lambda (x y) (* (+ x 1) (+ y 1))) A lambda expression is a list with exactly three items: the symbol lambda a list whose items are symbols (the argument list) an expression (the body) (lambda (x y) (* (+ x 1) (+ y 1))) arg list body Type it into dr scheme. Symbols in the arg list are called the procedure’s formal arguments. A procedure can be applied to data objects, in which case those data objects are called actual arguments. Number of actual arguments must be same as number of formal arguments (else ERROR!) (Exception: some built-ins accept an arbitrary number of actual arguments.)

(lambda (x y) (* (+ x 1) (+ y 1))) arg list body Symbols in the arg list are called the procedure’s formal arguments. A procedure can be applied to data objects, in which case those data objects are called actual arguments. Show it happening Applying a procedure: Create new bindings: each formal argument is bound to the corresponding actual argument. Evaluate the body in the context of that environment and the top-level environment. Remember the value of the body. Remove the new bindings The value of the procedure application is the value found in Step 2.

How to express a procedure application? The value of a number is itself. The value of a Boolean is itself. The value of a symbol is the data object it is bound to. The value of a list is obtained as follows: First evaluate each item in the list. The first item should evaluate to a procedure. Apply the procedure to the values of the rest of the items. The result of the procedure application is the value of the list. ((lambda (x y) (* (+ x 1) (+ y 1))) 2 3) The result of the procedure application is called the return value, And the procedure is said to return that data object. Later we’ll discuss why that term.

(lambda (x y) (* (+ x 1) (+ y 1))) arg list body Application ((lambda (x y) (* (+ x 1) (+ y 1))) 2 3) Quiz Write (a text denoting) an expression whose value is a procedure that takes three arguments, all numbers, and returns their average (also known as mean) Write (a text denoting) the application of that procedure to the actual arguments 5, 7, and 200. (Hint: Part of the second text is the first text.)

Procedure data objects Try evaluating a lambda expression in Dr. Racket. The value it prints in response is not a text denoting the expression. It’s convenient to be able to reuse a procedure over and over without giving the same lambda expression over and over… but how? Try it out in Dr. Racket A procedure is a data object, like a number. We can create a top-level binding of some symbol to that data object, using define. (define average (lambda (a b c)(/ (+ a b c) 3)) ) evaluates to (average 5 7 200) 70 and two-thirds