CS 115 – SOS Review Package – Midterm

Scheme will tell you f one or more of your tests have

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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: (check-expect (sum_square 2 4) 20) (check-expect (sum_square 3.4 6.3) 51) (check-expect (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 (/ (string-length str) 2))))) ;;Tests: (check-expect (front_part “computer”) “comp”) (check-expect (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 (/ (string-length str) 2)))) ;; Tests: (check-expect (front_part “computer...
View Full Document

Ask a homework question - tutors are online