#lang racket #| This is a top-of-file comment Assignment: hw0pr2.rkt (Racket functions) Login(s): Time spent (roughly): Other comments: |# ;; the above line tells Racket to use the standard language subset ;; this is a single-line comment in Racket ; actually, you only need one semicolon for a comment... (require htdp/testing) ;; this enables the unit-testing interface, ;; which allows the use of check-expect and generate-report ;; we show off unit-testing with some built-in functions! ;; Expression Expected result (check-expect (* 20 3) 60) ;; *_* awake! (check-expect (+ 21 20 1) 42) ;; +_+ asleep... (check-expect (quotient 42 10) 4) ;; integer division (check-expect (modulo 42 10) 2) ;; remainder (define greeting "Hello from Racket! Welcome to CS 60...") greeting ;; this will print out the above string, just for fun ;; here are a few examples of thoroughly-commented functions: ;; example scheme function ;; avg: averages two numbers ;; inputs: two integers, x and y ;; outputs: the average of x and y rounded down to the nearest integer (define (avg x y) (quotient (+ x y) 2)) ; provided tests (check-expect (avg 1 1) 1) (check-expect (avg 1 2) 1) ;; additional tests (check-expect (avg 3 5) 4) ;; example scheme function ;; add42: adds 42 ;; inputs: an integer, N ;; outputs: the integer forty-two larger than N (define (add42 N) (+ 42 N)) ; provided tests (check-expect (add42 0) 42) ;; Test! Test! Test! ; additional tests (check-expect (add42 1) 43) ;; example with booleans and if ;; is42: is it Douglas Adams's answer? ;; inputs: an integer, N ;; outputs: true if N==42, false otherwise (shows off cond) (define (is42 N) (if (equal? 42 N) ;; equal? compares values (deep); eq? compares addresses #t ;; true branch #f ;; false branch )) ;; A 'simpler' definition would directly ;; return the result of the equality check: ;; (define (is42 N) ;; (equal? 42 N)) (check-expect (is42 41) #f) (check-expect (is42 42) #t) ;; example with booleans and cond ;; sign: returns -1, 0, or 1 ;; inputs: an integer, N ;; outputs: -1 if N<0; 1 if N>0; 0 otherwise (define (sign N) (cond ( (> N 0) 1 ) ( (< N 0) -1 ) ( else 0 ) ;; else is a keyword! What ELSE could be here? )) ; provided tests (check-expect (sign 42) 1) ;; examples from class: ;; is-odd: tests for oddity (oddness?) ;; inputs: an integer, N ;; outputs: #t if N is odd, #f otherwise (define (is-odd N) (if (equal? (modulo N 2) 1) #t #f)) ;; fac: the factorial function ;; inputs: a positive integer, N ;; outputs: N! (define (fac N) (if (< N 1) 1 (* N (fac (- N 1))) )) ;; halve-count: the computer scientist's "log-base-2" function! ;; inputs: an integer, N ;; outputs: (log base 2 of N), i.e., ;; # of times you can divide N by 2 ;; until you reach 1 (define (halve-count N) (if (equal? N 1) 0 (+ (halve-count (quotient N 2)) 1))) (check-expect (is-odd 60) #f) (check-expect (fac 5) 120) (check-expect (halve-count 5) 2) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; YOUR FUNCTIONS AND TESTS GO HERE! ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Finally, we run the tests and output the results ;; ;; The output can get confusing if you have ;; more than 1 call to generate-report in your code ;; thus, the line below is best left until the very end (generate-report)