Rules of evaluation The value of a number is itself.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Conditional Statements If these were easy then everyone would them!
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Learn to identify and evaluate expressions. WARM-UP Please answer the questions below showing all your work! = – 189 = 3.46 x 847=
Control in LISP More on Predicates & Conditionals.
Lesson 1.4 Equations and Inequalities Goal: To learn how to solve equations and check solutions of equations and inequalities.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Operational Semantics of Scheme
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Discussion 4 eecs 183 Hannah Westra.
CS314 – Section 5 Recitation 10
Defining Macros in Lisp
Boolean expressions and if-else statements
Class 11: Two-argument recursion
Short Circuits Design Recipe Redux Structures
Evaluating Algebraic Expressions
Logic Gates.
Debugging and Random Numbers
IF statements.
Boolean expressions Conditional statements and expressions
Aberdeen Grammar School
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Case Study: Undefined Variables
CSE341: Programming Languages Section 1
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Computers & Programming Languages
3.4 Computer systems Boolean logic Lesson 2.
CSE341: Programming Languages Section 1
Procedural versus Functional Programming
Lesson 1.1 How do you evaluate algebraic expressions and powers?
The Metacircular Evaluator

The Metacircular Evaluator
Logic Gates.
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.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
A look at Python Programming Language 2018.
Lecture 12: Message passing The Environment Model
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.
Learning Intention I will learn about programming using selection (making choices) with one condition.
Using Lists and Functions to Create Dialogue
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
2. Second Step for Learning C++ Programming • Data Type • Char • Float
CSCI N207 Data Analysis Using Spreadsheet
Announcements Quiz 5 HW6 due October 23
Peter Seibel Practical Common Lisp Peter Seibel
topics interpreters meta-linguistic abstraction eval and apply
Types, Truth, and Expressions
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Rules of evaluation The value of a number is itself.
Defining Macros in Scheme
Control 9 / 25 / 19.
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. Something is missing… Lambda expressions! (lambda (x) (+ 1 x) They don’t fit the list rule: lambda is not an procedure. Even if it were, the other items in the list are not evaluated.

Special forms Some lists are evaluated … in a special way. Which lists? How can you tell? We say a list is a special form if the first item of the list is one of the following symbols: lambda quote cond if and or For each of these symbols, we will have a special rule. Special forms are considered expressions, so we have to amend our definition of expressions too.

lambda special rule: The value of the list is a procedure whose arg list is the second item in the list, and whose body is the third item in the list.

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 special rule: The value of the list is a procedure whose arg list is the second item in the list, and whose body is the third item in the list. Quiz: What are the values of the following expressions? ((lambda (x) (+ x 2)) ((lambda (y) (* y 2)) 3)) ((lambda (f) (f 3 2)) +)

Your next special form: The quote form We mostly use symbols as variables, but sometimes we will use them as data objects within a program. This poses a problem: How can we get our hands on a symbol in the code? In particular, we need an expression whose value is the symbol. Say you want an expression to check if variable x is bound to the symbol rainy. (equal? x rainy) Nope. According to the rules of evaluation, this will test equality between the data objects x and rainy are bound to. How to keep the voracious evalpig from trying to eat the symbol? Need a protective sheath: that’s what a quote form is. (equal? x (quote rainy))

The quote form rule The value of a list whose first item is the symbol quote is the second item. evaluation (quote rainy) rainy (quote (rainy cold)) (rainy cold) (quote (+ rainy cold)) (+ rainy cold) (quote (+ 2 3)) (+ 2 3) Lesson: Just because something looks like an expression doesn’t mean it will get evaluated. Conversely: Just because something doesn’t look like an expression doesn’t mean it won’t get evaluated.

The last special form you’ll ever need: cond ((= x 10) 1) ((= x 20) 2) ((= x 30) 3) ) If x = 10, the value is 1. If x = 20, the value is 2. If x = 30, the value is 3. If x is none of those, the value is … BLARGH! (error) (cond ((equal? x (quote rainy)) (+ 2 5)) ((equal? x (quote cold)) (/ (+ 3 7) 2)) ((equal? x (quote hot)) (/ (+ 3 7) 0)) )

The cond rule: To evaluate a list whose first item is cond, Each of the remaining items (2, 3, …n) must be a two-element list. Evaluate the first element of item 2; the value must be a Boolean. If it is true, the value of the cond is the value of the second element. Evaluate the first element of item 3; the value must be a Boolean. If it is true, the value of the bond is the value of the second element. Evaluate the first element of item 4; the value must be a Boolean. If it is true, the value of the bond is the value of the second element. …. Evaluate the first element of item n; the value must be a Boolean. If it is true, the value of the bond is the value of the second element. (cond ((equal? x (quote rainy)) (+ 2 5)) ((equal? x (quote cold)) (/ (+ 3 7) 2)) ((equal? x (quote hot)) (/ (+ 3 7) 0)) )

(cond ((equal? x (quote rainy)) (+ 2 5)) ((equal? x (quote cold)) (/ (+ 3 7) 2)) ((equal? x (quote hot)) (/ (+ 3 7) 0)) ) The cond rule: To evaluate a list whose first item is cond, Each of the other items must be a two-element list: (question answer) Both elements must be expressions; question must be Boolean-valued. The value of the cond is the value of the answer corresponding to the first question whose value is true. Key point: the later question and answer expressions are not evaluated. (cond (true 3) ((= x 1) (/ 1 0)))

Example of cond If none of the question expressions evaluates to true, BLARGH! Often last question is just true. (cond ((> temp 80) (quote hot)) ((< temp 60) (quote cold)) (true (quote moderate)) )

We don’t need any more special forms… … but we will give two more. (and expr-1 expr-2 … expr-n) (or expr-1 expr-2 … expr-n) These are Boolean operations: the inputs are Boolean and the output is Boolean. and x y true false Or x y true false and is true if all its expressions evaluate to true, else false. or is false if all its expressions evaluate to false, else true.

The and and or special forms (and expr-1 expr-2 … expr-n) (or expr-1 expr-2 … expr-n) and is true if all its expressions evaluate to true, else false. or is false if all its expressions evaluate to false, else true. What makes them special? Just enough expressions are evaluated to decide the answer—and no more. (and (= 1 1) (= 2 0) (= 5 (/ 1 0))) successfully evaluates to false. We call this short-circuiting: evaluate expressions in order, but stop when you know the answer.