Download presentation
Presentation is loading. Please wait.
Published byChristine Kjær Modified over 5 years ago
1
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?
2
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 The symbol e is bound to
3
More built-in procedures
Procedures for which the output is Boolean: > >= < <= = equal? Show more bindings Many others.
4
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!
5
Why bind a symbol? Simplify code Reduce computation time Simplify future changes Perhaps you need to compute You could do it like this: (+ (* (/ 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))
6
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!
7
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.
8
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
9
(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
10
(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.)
11
(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.
12
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.
13
(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.)
14
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 ) 70 and two-thirds
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.