This preview shows page 1. Sign up to view the full content.
Unformatted text preview: E A statement that describes the function, its parameters (by name) and what the function should produce. EXAMPLES Provide representations of all the possible cases and outcomes that could arise with the function. BODY The actual code. TESTING (check
expect (function_name sample_values) expected_value) If your function output and the expected value are equal, then the test will pass. Scheme will tell you f one or more of your tests have passed or not. Example 2: Produce a function called sum_square that produces the sum of the squares of any two real numbers, but rounds the number down to the nearest integer. ;;sum_square: num num > int
;;Purpose: the function consumes two numbers, a and b, produces the sum of the squares
of these two numbers, rounded down to the nearest integer.
;;Examples:
;;(sum_square 2 4) > 20
;;(sum_square 3.4 6.3) > 51
;;(sum_square 5 6) > 61 (define (sum_square a b) (floor (+ (* a a) (* b b))))
;;Tests:
(checkexpect (sum_square 2 4) 20)
(checkexpect (sum_square 3.4 6.3) 51)
(checkexpect (sum_square 5 6) 61) STRINGS A value made up of words, letters, numbers, spaces, and punctuation marks that are enclosed in quotation marks (ex. “computer” “computers123” “computer science” “sdalkfjhdf 34 jdhf875”) “Computer” 0 1 2 3 4 5 6 7 Built
In String Functions: (string
append “joe” “clark”)
> “joe clark” (string
length “joe clark”)
> 9 (subtring “computer” 0 4)
> comp (substring “computer” 0 1)
> “c” (subtring “computer” 1)
> “omputer” (string>? “nowhere” “here”)
> true HELPER FUNCTION A helper function is defined before the main function, and is used to generate similar expressions, express easy/repetitive computations in clear form, and to avoid really long complex definitions. • You still need the full design recipe for helper functions • Choose meaningful names (not just helper1 etc.) Example 3 (swap parts): Take a string and swap the front of the string with the back of it. If the string length is odd, make the front part shorter. ;;front_part: string > string
;;Purpose: To take a string, str, and produce the front part of it
;;Examples: (front_part “computer”) > “comp”
;;(front_part “compute”) > “com”
(define (front_part str) (substring str (floor (/ (stringlength str) 2)))))
;;Tests:
(checkexpect (front_part “computer”) “comp”)
(checkexpect (front_part “compute”) “com”)
;;back_part: string > string
;;Purpose: To take a string, str, and produce the back part of it ;;Examples: (back_part “computer”) > “uter”
;;(back_part “compute”) > “pute”
(define (front_part str) (substring str (floor (/ (stringlength str) 2))))
;; Tests:
(checkexpect (front_part “computer...
View
Full
Document
 Fall '13
 TroyVasiga
 Computer Science

Click to edit the document details