### 2015-09-28

posted Sep 28, 2015, 6:24 AM by Samuel Konstantinovich   [ updated Sep 28, 2015, 6:24 AM ]
 Goal: Keeping the fun in functions.We have already seen the define statement:(define  label   v)   ; where v can be any value, expression, or functionMath notation of functions:We can define functions like in math, the math notation is as follows:f(x) = x + 1  f is the name of the function(x) is the parameterx+1 is what to do with the parametersorsum(a,b) = a + bsum is the namea and b are parametersa + b is what to do with the functionSCHEME notation of functions:(define  f ???? )we need a way to create a function(lambda (parameters) expression )(lambda (x) (+ x 1) )   ; this is scheme for the x+1 function(define f   (lambda (x) (+ x 1) ) ) ; this is like f(x) = x + 1(define add (lambda (x y) (+ x y)));this is like add(x,y) = x + y ;Try to make it readable:(define label (lambda   (parameters)   v  ) );Break up v into new lines as well if needed.(define add    (lambda       (x y)       (+ x y)));Lambda is a function that creates a function.;the first parameter is a list of labels that the function will use as parameters;the second parameter is the expression that uses the label to calculate the result;This is one way to write a distance function that takes;two sets of coordinates (x1,y1) and (x2,y2)(define dist  (lambda  (x1 y1 x2 y2)  (sqrt (+ (sq (- x1 x2)  ) (sq (- y1 y2))))));test cases to see if your function works(dist 0 0 3 4)(dist 1 1 2 2);A better way is to break it up into smaller pieces;and use new lines to help readability(define dist  (lambda      (x1 y1 x2 y2)      (sqrt (+              (* (- x1 x2) (- x1 x2))              (* (- y1 y2) (- y1 y2)) ))))(dist 0 0 3 4)(dist 1 1 2 2)