Skip to content

Instantly share code, notes, and snippets.

@exilyte
Created August 1, 2014 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exilyte/780315f35fa99d5318c5 to your computer and use it in GitHub Desktop.
Save exilyte/780315f35fa99d5318c5 to your computer and use it in GitHub Desktop.
How to Design Programs Exercise 3.3.1 Using Beginner's Student Language
;; Contract: print : expr expected_expr ->
;;expr "should be" expected_expr
;; Purpose: to test expressions to find out whether they're
;; correct or not
;; Examples: (print (inches->cm 22/7) 7.98287514)
;; => "3.142857142857143 should be 7.98287514"
;; Definition: [refines the header]
(define (print actual expected)
(string-append
(number->string (exact->inexact actual))
" should be "
(number->string (exact->inexact expected))))
;; Contract: inches->cm : number -> number
;; Purpose: to convert inches to centimeters
;; Examples: (inches->cm 22/7) should be 7.9828514
;; Definition: [refines the header]
(define (inches->cm in)
(* in 2.54))
;; Tests:
(print (inches->cm 22/7) (* 2.54 (/ 22 7)))
(print (inches->cm 3.5) (* 2.54 3.50))
(print (inches->cm 100) (* 2.54 100))
;; Contract: feet->inches : number -> number
;; Purpose: to convert feet to inches
;; Examples: (feet->inches 32) should be 384
;; Definition:
(define (feet->inches ft)
(* ft 12))
;; Tests:
(print (feet->inches 32) (* 32 12))
(print (feet->inches 12.5) (* 12.5 12))
(print (feet->inches -99) (* -99 12))
;; Contract: yards->feet : number -> number
;; Purpose: to convert yards to feet
;; Examples: (yards->feet 3) should be 9
;; Definition:
(define (yards->feet yd)
(* yd 3))
;; Tests:
(print (yards->feet 3) (* 3 3))
(print (yards->feet 1.5) (* 1.5 3))
(print (yards->feet -3.5) (* -3.5 3))
;; Contract: rods->yards : number -> number
;; Purpose: to convert rods to yards
;; Examples: (rods->yards 1) should be 5.5
;; Definition:
(define (rods->yards rd)
(* rd 5.5))
;; Tests:
(print (rods->yards 1) (* 1 5.5))
(print (rods->yards 2) (* 2 5.5))
(print (rods->yards 3) (* 3 5.5))
;; Contract: furlongs->rods
;; Purpose: to convert furlongs to rods
;; Examples: (furlongs->rods 5) should be 200
;; Definition:
(define (furlongs->rods fl)
(* fl 40))
;; Tests:
(print (furlongs->rods 5) (* 5 40))
(print (furlongs->rods 3) (* 3 40))
(print (furlongs->rods 1) (* 1 40))
;; Contract: miles->furlongs
;; Purpose: to convert miles to furlongs
;; Examples: (miles->furlongs 5) should be 40
;; Definition:
(define (miles->furlongs mi)
(* mi 8))
;; Tests:
(print (miles->furlongs 5) (* 5 8))
(print (miles->furlongs 6.6) (* 6.6 8))
(print (miles->furlongs (/ 22 7)) (* (/ 22 7) 8))
;; Contract: feet->cm : number -> number
;; Purpose: to convert feet to centimeters
;; Examples: (feet->cm 1) should be 0.032808
;; Definition:
(define (feet->cm ft)
(/ ft 30.48))
;; Tests:
(print (feet->cm 1) (* 1 30.48))
(print (feet->cm 3) (* 3 30.48))
(print (feet->cm 5) (* 5 30.48))
;; Contract: yards->cm : number -> number
;; Purpose: to convert yards to cm
;; Examples: (yards->cm 1) should be 91.44
;; Definition:
(define (yards->cm yd)
(* yd 91.44))
;; Tests:
(print (yards->cm 1) (* 1 91.44))
(print (yards->cm 5.5) (* 5.5 91.44))
(print (yards->cm 9.324) (* 9.324 91.44))
;; Contract: rods->inches : number -> number
;; Purpose: to convert rods to inches
;; Examples: (rods->inches 1) should be 198
;; Definition:
(define (rods->inches rd)
(* rd 198))
;; Tests:
(print (rods->inches 1) (* 1 198))
(print (rods->inches 3.3) (* 3.3 198))
(print (rods->inches 9.9) (* 9.9 198))
;; Contract: miles->feet : number -> number
;; Purpose: to convert miles to feet
;; Examples: (miles->feet 1) should be 5280
;; Definition:
(define (miles->feet mi)
(* mi 5280))
;; Tests:
(print (miles->feet 1) (* 1 5280))
(print (miles->feet #i3.14159) (* #i3.14159 5280))
(print (miles->feet 3) (* 3 5280))
#| Start Of Results
"7.982857142857143 should be 7.982857142857143"
"8.89 should be 8.89"
"254.0 should be 254.0"
"384.0 should be 384.0"
"150.0 should be 150.0"
"-1188.0 should be -1188.0"
"9.0 should be 9.0"
"4.5 should be 4.5"
"-10.5 should be -10.5"
"5.5 should be 5.5"
"11.0 should be 11.0"
"16.5 should be 16.5"
"200.0 should be 200.0"
"120.0 should be 120.0"
"40.0 should be 40.0"
"40.0 should be 40.0"
"52.8 should be 52.8"
"25.142857142857142 should be 25.142857142857142"
"0.03280839895013123 should be 30.48"
"0.0984251968503937 should be 91.44"
"0.16404199475065617 should be 152.4"
"91.44 should be 91.44"
"502.92 should be 502.92"
"852.58656 should be 852.58656"
"198.0 should be 198.0"
"653.4 should be 653.4"
"1960.2 should be 1960.2"
"5280.0 should be 5280.0"
"16587.5952 should be 16587.5952"
"15840.0 should be 15840.0"
End Of Results|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment