; CS101, Spring 2013 ; Lab 2 Racket starter file ; Defining unnamed procedures using the lambda special form. ; NOTE: All parts that are to be completed by you are inside comment ; boxes. Read everything up to the comment box and then write ; your answers and choose Uncomment under the Racket menu. If ; you are reading this file as a text file, the comment boxes ; are not shown. (display "\n CS101 Lab 2, Spring 2013\n") (display "\n PLEASE WRITE YOUR NAME HERE\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. ; ; UNCOMMENT THE PRINTF BELOW AND WRITE THE LAMBDA EXPRESSION IN THE ; ; PRINTF STATEMENT. WRITE THE LAMBDA EXPRESSION IN A STRING AS THE ; ; SECOND ARGUMENT TO PRINTF AS INDICATED BELOW. ; ; (printf "Solution is: ~a~%~%" "WRITE LAMBDA EXPRESSION AS A STRING HERE") ; (newline) ;; CALL THE LAMBDA EXPRESSION YOU WROTE ABOVE ON THE 3 INPUTS ;; f=32, f=212, f=-40. ;; ;; THERE SHOULD BE 3 SEPARATE CALLS TO PRINTF, AS STARTED 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 ;; (GIVEN), ;; 3RD ARG: a call to the lambda function you wrote above, passing in the ;; degrees Fahrenheit as an argument to the lambda function. ; ;; 3 INPUTS f=32, f=212, f=-40 ; ; (printf "~a degrees F = ~a degrees C~%~%" ...) ; (newline) ; (printf "~a degrees F = ~a degrees C~%~%" ...) ; (newline) ; (printf "~a degrees F = ~a degrees C~%~%" ...) ; (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 the fahr->cels procedure on the following examples: ;; ;; f = 32, f = 212, and f = -40. ;; ; ;; DEFINE THE FAHR->CELS FUNCTION HERE. ; ; ; (newline) ;; CALL FAHR->CELS WITH THE SAME INPUTS YOU USED IN PART (a) USING THREE ;; PRINTF STATEMENTS WITH THE SAME NUMBER OF ARGUMENTS AS THE ONES IN 1(a). ;; (3 SEPARATE PRINTF STATEMENTS). ; ;; 3 INPUTS f=32, f=212, f=-40 ; ; (printf "~a degrees F = ~a degrees C~%~%" ...) ; (newline) ; (printf "~a degrees F = ~a degrees C~%~%" ...) ; (newline) ; (printf "~a degrees F = ~a degrees C~%~%" ...) ; (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. WRITE THE ; ; LAMBDA EXPRESSION IN A STRING AS THE SECOND ARGUMENT TO PRINTF, ; ; AS INDICATED BELOW. ; ; (printf "Solution is: ~a~%~%" "WRITE LAMBDA EXPRESSION HERE") ; (newline) ;; 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 (GIVEN) ;; 6TH ARG: a call to the lambda function you wrote above, passing in ;; x1 x2 y1 and y2. ; ;; 3 INPUTS x1 = 0, y1 = 0, x2 = 1, y2 = 1 ; ;; x1 = 0, y1 = 0, x2 = 3, y2 = 12 ; ;; x1 = 1, y1 = 2, x2 = 8, y2 = 15 ; ; (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ; ...) ; (newline) ; (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ; ...) ; (newline) ; (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ; ...) (newline) (newline) (display "Problem 2(b): Naming the lambda expression from part 2(a):\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. ; ; (newline) ;; CALL THE SLOPE FUNCTION WITH THE SAME INPUTS YOU USED IN PART (a) ;; (3 SEPARATE PRINTF STATEMENTS WITH 6 ARGUMENTS EACH). ; ;; 3 INPUTS x1 = 0, y1 = 0, x2 = 1, y2 = 1 ; ;; x1 = 0, y1 = 0, x2 = 3, y2 = 12 ; ;; x1 = 1, y1 = 2, x2 = 8, y2 = 15 ; ; (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ; ...) ; (newline) ; (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ; ...) ; (newline) ; (printf "Slope when x1=~a, y1=~a, x2=~a, y2=~a is ~a~%~%" ; ...) (newline) (newline)