;; CMPU101, Spring 2013 ;; Lecture #3 ;;; Consider the following expressions. What happens when you type ;;; each one in the interactions window? ;;; ;;; NUM ;;; LAB ;;; THING ;;; THING2 ;;; XXX ;;; YYY ;;; Next, consider the following expressions. They do not generate ;;; any "return" value; but they do have side-effects. Uncomment ;;; the following lines and then click on the "Run" button. ;;; (define NUM 1000) ;;; (define LAB 'now) ;;; (define THING 'NUM) ;;; (define THING2 NUM) ;;; (define XXX (+ 2 3)) ;;; (define YYY '(+ 2 3)) ;;; Nothing happened, right? But now re-type the earlier expressions ;;; (NUM, LAB, etc.) into the Interactions window. You don't get any ;;; errors this time because the define statements wrote the names ;;; and associated values into the global environment. ;;; Question: What happens to the value of THING2 when NUM is changed? ; ; The DEFINE special form: (SPECIAL FORM 1) ; ; The keyword define is called a special form because it is not ; evaluated according to the default rule. The word define is read, ; but is not looked up in the global environment (there is no entry ; for keywords in that table). ; ; The purpose of the define special form is to create a new entry ; in the global environment (GE). Specifically, for the expression ; ; (define X (+ 5 (* 4 2 3))) ; ; the symbol X is written into the name column of the GE and the ; value 29 is written as the value for X in the GE. The only part of ; the expression that is evaluated is the last subexpression, and ; the result of that evaluation is written in the GE as the value ; of X. ; ; Evaluation of a define statement returns nothing...its side- ; effect is the creation of a new name/value pair in the GE. ; ; ----------------------------------------- ; ; But how do we define our own function names? We need another ; specialform called LAMBDA. (SPECIAL FORM 2) ; ;;; LAMBDA expressions are SPECIAL FORMS that evaluate to PROCEDURES! ;;; Their form is inspired by Alonzo Church's Lambda Calculus. ;;; ;;; The form is: ;;; ;;; (lambda ( ... ) ;;; ;;; ) ;;; ;;; The result of EVALUATING such a LAMBDA expression is a procedure ;;; that expects N inputs (represented by the list of N parameters ;;; ... ). Each of these parameters has a unique name. ;;; ;;; Here's how to call this nameless procedure on N input arguments ;;; ... : (Notice that there must be N arguments since the ;;; lambda expression specifies N parameters.) ;;; ;;; ((lambda ( ... ) ;;; ) ;;; ... ) ;;; ;;; This function invocation obeys the Default Rule because the first ;;; entry after the leftmost parenthesis is a function, albeit a nameless ;;; one. ;;; ;;; When evaluating the expression in the of the lambda, any ;;; occurrence of a symbol that appears in the parameter list will ;;; evaluate to the corresponding argument. The arguments ... ;;; are matched, in order, with the parameters ... ;;; ;;; ;;; Thus, the evaluation of symbols in the parameter list does NOT ;;; use the Global Environment! ;;; ;;; In effect, the names ... are names that exist ;;; only inside the ()s of the lambda expression. ;;; ;;; The lambda expression returns the value that results from evaluating ;;; its body. ; ; What is the return value of the calls to unnamed lambda expressions given ; in (a) through (c) (the last one is a bit tricky): ; ; (a) ((lambda (x) (+ x 2)) 5) ; ; (b) ((λ (x y) (+ x (* y 3))) 7 4) ; ; (c) ((λ (x) ((λ (y) (* y 2)) (+ x 3))) 4) ; ; ; The following are examples of well-formed (i.e., valid) lambda ; expressions: ; ; 1) (λ () 44) ; ; 2) (λ (x) (* x x)) ; ; 3) (λ (w h) (* w h)) ; ; 4) (λ (r h) (* 1/3 3.14159 r r h)) ; ; 5) (λ (x y z)(+ x(− y z))) ; ; In contrast, the following are examples of malformed lambda ; expressions: ; ; 1) (λ (x y x) (* x y)) ; ; 2) (λ (x 10) (* x 10)) ; ; 3) (λ x) ; ; Why are these last 3 lambda expressions invalid? ; ; Example 1: Write a lambda expression that returns the value of its ; single numeric argument cubed. ; ; Call this expression on the input values 5, 4 and -1 (this ; will require 3 different calls). ; ; Example 2: Write a lambda expression that takes 2 numeric arguments and ; returns the result of adding the square of the first argument ; to the cube of the second. ; ; Call this expression on input values 1, 2, and -3 (this ; will require 3 different calls). ; ; Example 3: Write a lambda expression that takes 4 numeric arguments ; representing the (x,y) coordinates of 2 points in the plane ; and calculates the Euclidean distance between these points. ; The formula to calculate Euclidean distance between points ; (x1, y1) and (x2, y2) is (sqrt ((x1 - x2)^2 + (y1 - y2)^2)) ; ; Call this expression on input values x1=0, y1=0, x2=4, y2=6. ; This will require only one call. ; Wait a minute! Wouldn't this whole process be simpler if we could just ; use the functions we've already written instead of repeating the ; calculations? ; ; YES! We can allow easy access to our functions as helpers, or ; subsidiary functions, inside another function by using the DEFINE ; special form to give each lambda expression a name. ; ;; Use the define special form to name the procedures you wrote in examples ;; 1-3. ; Example 1: Define a function that returns the value of its ; single numeric argument cubed. ; Example 2: Define a function that takes 2 numeric arguments and ; returns the result of adding the square of the first ; argument to the cube of the second. ; Example 3: Define a function that takes 4 numeric arguments ; representing the (x,y) coordinates of 2 points in the plane ; and calculates the Euclidean distance between these points. ; The formula to calculate Euclidean distance between points ; (x1, y1) and (x2, y2) is (sqrt ((x1 - x2)^2 + (y1 - y2)^2))