#lang racket (provide hours celsius->fahrenheit display-price string-version introduction) ; Please do not modify the lines above this one. ; ******************************************************** ; CS 2010 HW #0 DUE Friday 1/23/2026, 11:59 pm ; ** submit online using gradescope. ; ******************************************************** ; Name: Your name here ; Email address: Your Yale email here ; ******************************************************** ; This file may be loaded into Racket. Lines beginning with ; semicolons are comments. ; Homework #0 will be worth 20 points -- other homeworks will be worth ; 100 points (or so). ; You will be submitting one file (this one) for homework #0. ; Please make sure it is named hw0.rkt when you submit. ; ******************************************************** ; ** problem 0 ** (1 easy point) ; Replace the number 0 in the definition below to indicate how many ; hours you spent doing this assignment. Fractions are fine, eg, ; 3.14159. You will receive no credit for this problem if you leave ; the number as 0. (define hours 0) ; ******************************************************** ; ** problem 1 ** (3 points) ; Write a procedure ; celsius->fahrenheit : number -> number ; which converts a temperature in Celsius to the equivalent ; temperature in Fahrenheit. Recall that the Fahrenheit temperature ; can be obtained by multiplying the Celsius temperature by 9/5, then ; adding 32. ; Examples: ; (celsius->fahrenheit 0) => 32 ; (celsius->fahrenheit 100) => 212 ; (celsius->fahrenheit 28) => 82.4 ; (celsius->fahrenheit -10.3) => 13.46 ; (celsius->fahrenheit 5/9) => 33.0 ; ******************************************************** ; Replace "empty" in the code below with your procedure. Make sure it ; is named celsius->fahrenheit and has one argument. The name of the argument is ; not important. (define celsius->fahrenheit empty) ; ******************************************************** ; ** problem 2 ** (3 points) ; Write a procedure ; display-price : number -> string ; that takes in a price in cents (as a positive whole number), ; and returns a string representing the price in the ; usual $X.YY format. (You might find the `quotient : number number -> number` ; and `remainder : number number -> number` functions useful. For example, ; `(quotient 150 4)` yields 37, and `(remainder 150 4)` yields 2, because ; 150 = 4 * 37 + 2.) ; (display-price 32) => "$0.32" ; (display-price 520) => "$5.20" ; (display-price 143198) => "$1431.98" ; ******************************************************** ; Replace "empty" in the code below with your procedure. Make sure it ; is named display-price and has three arguments. ; The names of the arguments are not important. (define display-price empty) ; ******************************************************** ; ** problem 3 ** (3 points) ; The "string version" of a numeric function `f` behaves just ; like `f`, but it acts on strings instead of numbers. For ; example, if `f` is the function that adds 3 to its input ; (i.e., f(4)=7, f(8.2)=11.2, and so on), the string version ; of f would map the *string* "4" to "7", the *string* "8.2" ; to "11.2", and so on. ; In this problem, we would like you to write a procedure ; string-version : (number -> number) -> (string -> string) ; that takes in a function which operates on numbers, and ; returns a new function--its string version--that has the same behavior, ; but for strings. Note that `string-version` itself only has one ; argument (a number->number function). ; You can assume that the string version of a function will only be ; called on strings that represent numbers. That is, no one will try ; to run code like `((string-version sqrt) "Hello")`. ; Examples: ; ((string-version -) "4") => "-4" ; ((string-version abs) "-3") => "3" ; ((string-version add1) "2.1") => "3.1" ; ((string-version sqr) "4") => "16" ; ((string-version sqrt) "25") => "5" ; ((string-version (lambda (n) (* 2 n))) "-4.2") => "-8.4" ; ******************************************************** ; Replace "empty" in the code below with your procedure. Make sure it ; is named string-version and has one argument. The name of the argument ; is not important. (define string-version empty) ; ******************************************************** ; ** problem 4 ** (10 points) ; For this problem, you are asked to write a brief self-introduction ; as a string. Your introduction should answer the following questions: ; a. What name do you go by, and do you have any tips for how it's pronounced? ; b. Why have you chosen to take CPSC 2010? Is there anything you're especially hoping ; to get out of the class? ; c. What is one thing you like to do outside of class? ; You can answer this by telling me about an extracurricular you participate in, ; a hobby you have, or just something you're into (an author or book, filmmaker or movie, ; a video game, a song or musical artist, a play or playwright, a visual artist, a YouTube ; channel or TikTok creator, etc. -- anything goes!). ; To type a particularly long string, you could break it up into multiple strings, ; and use string-append. ; You will get all 10 points for this problem if the variable `introduction` is ; set to a string of length at least 100 characters. ; The goal of this problem is to help us get acquainted with you ; and your interests -- you won't receive feedback on your answers. (define introduction empty) ; ******************************************************** ; ******** testing, testing. 1, 2, 3 .... ; ******************************************************** ; Press Run in DrRacket, and then type (runtests) in the ; Interactions area to run automated tests on your code. (define (format-expected-actual expected actual) (format "Correct answer should be ~a. Your answer was: ~a." expected actual)) (define (approx=? a b [eps 1e-9]) (and (real? a) (real? b) (<= (abs (- (exact->inexact a) (exact->inexact b))) eps))) (struct test-result (name ok? got expected output) #:transparent) (define (test name got-thunk expected [expected-text #f]) (define expected-display (cond [expected-text expected-text] [(procedure? expected) "a value satisfying the expected predicate"] [else expected])) (with-handlers ([exn:fail? (lambda (e) (test-result name #f (list 'error (exn-message e)) expected-display (format "Error while running: ~a" (exn-message e))))]) (define got (got-thunk)) (define ok? (if (procedure? expected) (with-handlers ([exn:fail? (lambda (_e) #f)]) (expected got)) (equal? got expected))) (test-result name ok? got expected-display (format-expected-actual expected-display got)))) (define (print-test-result r) (define status (if (test-result-ok? r) "OK" "FAIL")) (printf "[~a] ~a\n" status (test-result-name r)) (when (not (test-result-ok? r)) (printf " expected: ~a\n" (test-result-expected r)) (printf " got: ~a\n" (test-result-got r)) (printf " ~a\n" (test-result-output r)))) (define (runtests) (define results (list (test 'hours (lambda () hours) (lambda (x) (> x 0)) "a positive number") ; problem 1 (test 'celsius->fahrenheit-0 (lambda () (celsius->fahrenheit 0)) (lambda (x) (= x 32)) 32) (test 'celsius->fahrenheit-100 (lambda () (celsius->fahrenheit 100)) (lambda (x) (= x 212)) 212) (test 'celsius->fahrenheit-28 (lambda () (celsius->fahrenheit 28)) (lambda (x) (approx=? x 82.4)) 82.4) (test 'celsius->fahrenheit-neg (lambda () (celsius->fahrenheit -10.3)) (lambda (x) (approx=? x 13.46)) 13.46) (test 'celsius->fahrenheit-frac (lambda () (celsius->fahrenheit 5/9)) (lambda (x) (approx=? x 33.0)) 33.0) ; problem 2 (test 'display-price-32 (lambda () (display-price 32)) "$0.32") (test 'display-price-520 (lambda () (display-price 520)) "$5.20") (test 'display-price-143198 (lambda () (display-price 143198)) "$1431.98") (test 'display-price-5 (lambda () (display-price 5)) "$0.05") (test 'display-price-50 (lambda () (display-price 50)) "$0.50") (test 'display-price-0 (lambda () (display-price 0)) "$0.00") (test 'display-price-509 (lambda () (display-price 509)) "$5.09") (test 'display-price-100 (lambda () (display-price 100)) "$1.00") (test 'display-price-999 (lambda () (display-price 999)) "$9.99") ; problem 3 (test 'string-version-returns-procedure (lambda () (string-version abs)) procedure? "a procedure (string -> string)") (test 'string-version-- (lambda () ((string-version -) "4")) "-4") (test 'string-version-abs (lambda () ((string-version abs) "-3")) "3") (test 'string-version-add1 (lambda () ((string-version add1) "2.1")) "3.1") (test 'string-version-sqr (lambda () ((string-version sqr) "4")) "16") (test 'string-version-sqrt (lambda () ((string-version sqrt) "25")) "5") (test 'string-version-lambda (lambda () ((string-version (lambda (n) (* 2 n))) "-4.2")) "-8.4") ; problem 4 (test 'introduction-is-string (lambda () introduction) string? "a string") (test 'introduction-is-long-enough (lambda () introduction) (lambda (x) (>= (string-length x) 100)) "a string of length at least 100"))) (displayln "Running tests...") (for-each print-test-result results) (define passed (length (filter test-result-ok? results))) (printf "\nPassed ~a / ~a tests.\n" passed (length results)))