; CS101, Spring 2013 ; Lab 2 ; Defining unnamed procedures using the lambda special form. (display "\n CS101 Lab 2, Spring 2013\n") (display "\n SOLUTIONS \n\n") (newline) (display "Problem 1(a): Writing a Fahrenheit to Celsius converter:\n\n") ; ; Define an unnamed Racket procedure that consumes a single ; input (representing the temperature in Fahrenheit) and returns ; as output the corresponding temperature in Celsius. ; ; The appropriate conversion formula is: c = 5(f-32)/9. ; (You'll have to change this expression a little to get it ; into the form of a Racket expression.) ; ; Make sure that you test your unnamed procedure on the ; following examples: ; ; f = 32, f = 212, and f = -40. ; ;; WRITE THE LAMBDA EXPRESSION HERE IN A PRINTF STATEMENT. PUT THE LAMBDA ;; EXPRESSION IN A STRING TO PRINT. (printf "Solution is: ~a~%~%" "(lambda (f) (/ (* 5 (- f 32)) 9))") ;; THERE SHOULD BE 3 SEPARATE CALLS TO PRINTF, AS SHOWN BELOW. ;; ;; EACH PRINTF EXPRESSION SHOULD HAVE 3 ARGUMENTS: ;; 1ST ARG: the string "~a degrees F = ~a degrees C~%~%" (GIVEN), ;; 2ND ARG: a number representing the temperature in degrees Fahrenheit, ;; 3RD ARG: a call to the lambda function you wrote above, passing in the ;; degrees Fahrenheit as an argument to the lambda function. (printf "~a degrees F = ~a degrees C~%~%" 32 ((lambda (f) (/ (* 5 (- f 32)) 9)) 32)) (newline) (printf "~a degrees F = ~a degrees C~%~%" 212 ((lambda (f) (/ (* 5 (- f 32)) 9)) 212)) (newline) (printf "~a degrees F = ~a degrees C~%~%" -40 ((lambda (f) (/ (* 5 (- f 32)) 9)) -40)) (newline) (newline) (display "Problem 1(b): Naming the lambda expression from part 1(a):\n\n") ; ; Use a define special form to name the lambda expression you wrote ; for part (a). Call the function fahr->cels. ; ; Test your procedure on the following examples: ; ; f = 32, f = 212, and f = -40. ; ;; DEFINE THE FAHR->CELS FUNCTION HERE. (define fahr->cels (lambda (f) (/ (* 5 (- f 32)) 9))) ;; CALL FAHR->CELS WITH THE SAME INPUTS YOU USED IN PART 1(a), USING THREE ;; PRINTF STATEMENTS WITH THE SAME NUMBER OF ARGUMENTS AS THE PRINTFS IN 1(a) ;; (3 SEPARATE PRINTF STATEMENTS). (printf "~a degrees F = ~a degrees C~%~%" 32 (fahr->cels 32)) (newline) (printf "~a degrees F = ~a degrees C~%~%" 212 (fahr->cels 212)) (newline) (printf "~a degrees F = ~a degrees C~%~%" -40 (fahr->cels -40)) (newline) (newline) (display "Problem 2(a): Writing a function to calculate slope:\n\n") ; ; Define an unnamed Racket procedure that consumes four ; inputs--x1, y1, x2, y2--representing the coordinates of ; two points in the xy-plane: (x1, y1) and (x2, y2), and ; returns as output the slope of the line segment joining ; these two points. ; ; The appropriate formula is: (y2 - y1) / (x2 - x1). ; (You'll have to change this expression a little to get ; it into the form of a Racket expression.) You may assume ; that x1 and x2 are different. ; ; Make sure that you test the unnamed procedure on the ; following examples: ; ; x1 = 0, y1 = 0, x2 = 1, y2 = 1 ; ; x1 = 0, y1 = 0, x2 = 3, y2 = 12 ; ; x1 = 1, y1 = 2, x2 = 8, y2 = 15 ; ;; WRITE THE LAMBDA EXPRESSION IN A PRINTF STATEMENT. PUT THE LAMBDA ;; EXPRESSION IN A STRING TO PRINT. (printf "Solution is: ~a~%~%" "(lambda (x1 y1 x2 y2) (/ (- y2 y1) (- x2 x1)))") ;; CALL THE LAMBDA EXPRESSION ON THE 3 SETS OF INPUTS GIVEN ABOVE ;; EACH PRINTF EXPRESSION SHOULD HAVE 6 ARGUMENTS: ;; 1ST ARG: the string "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ;; (STRING IS GIVEN BELOW FOR EACH SET OF ARGUMENTS), ;; 2ND THROUGH 5TH ARGS: values for x1, x2, y1, and y2 ;; 6TH ARG: a call to the lambda function you wrote above, passing in ;; x1 x2 y1 and y2. (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" 0 0 1 1 ((lambda (x1 y1 x2 y2) (/ (- y2 y1) (- x2 x1))) 0 0 1 1)) (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" 0 0 3 12 ((lambda (x1 y1 x2 y2) (/ (- y2 y1) (- x2 x1))) 0 0 3 12)) (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" 1 2 8 15 ((lambda (x1 y1 x2 y2) (/ (- y2 y1) (- x2 x1))) 1 2 8 15)) (newline) (newline) (display "Problem 2(b): Naming the lambda expression from part 2(a):\n\n") ; ; Use a define special form to name the lambda expression you wrote ; for part 2(a). Call the function slope. ; ; Test the slope procedure on the same examples as you did in part ; 2(a). ; ;; DEFINE THE SLOPE FUNCTION HERE. (define slope (lambda (x1 y1 x2 y2) (/ (- y2 y1) (- x2 x1)))) (newline) ;; CALL FAHR->CELS WITH THE SAME INPUTS YOU USED IN PART 2(a) ;; (3 SEPARATE PRINTF STATEMENTS WITH 6 ARGUMENTS EACH). (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" 0 0 1 1 (slope 0 0 1 1)) (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" 0 0 3 12 (slope 0 0 3 12)) (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" 1 2 8 15 (slope 1 2 8 15)) (newline) (newline)