;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname module-02) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; (abs x) return the absolute value of x.
;; abs: Int -> Nat
;;;; or...
;; abs: Num -> Num
;; Examples:
(check-expect (abs -1) 1)
(check-expect (abs 5) 5)
;; (gcd a b) return the gcd of a and b.
;; gcd: Nat Nat -> Nat
;; Examples:
(check-expect (gcd 4 6) 2)
(check-expect (gcd 7 1) 1)
(check-expect (gcd 42 0) 42)
;; (count-vertices g) return the number of vertices in g.
;; count-vertices: Graph -> Nat
;; (distance x y) return the distance between (0,0) and (x,y).
;; distance: Num Num -> Num
;; Examples:
(check-expect (distance 3 4) 5)
(check-expect (distance 0 7) 7)
(define (distance x y)
(sqrt (+ (* x x)
(* y y))))
;; Tests:
(check-within (distance 1 1) (sqrt 2) 0.0001)
;; A variable containing a Str has a name just like
;; a variable containing some other value.
(define hello "Hey hey!")
;; The following are a few helper functions for swap-parts
;; (mid s) indicate where the middle of s is.
;; mid: Str -> Nat
;; Examples:
(check-expect (mid "whatever") 4)
(check-expect (mid "cat") 1)
(define (mid s)
(quotient (string-length s) 2))
;; (front-part s) return the front half of s.
;; front-part: Str -> Str
;; Examples:
(check-expect (front-part "whatever") "what")
(check-expect (front-part "something") "some")
(define (front-part s)
(substring s 0 (mid s)))
;; (back-part s) return the back part of s.
;; back-part: Str -> Str
;; Examples:
(check-expect (back-part "whatever") "ever")
(check-expect (back-part "something") "thing")
(define (back-part s)
(substring s (mid s)))
;; (swap-parts x) returns new string with front and back halves of x reversed.
;; swap-parts: Str -> Str
;; Examples:
(check-expect (swap-parts "sock") "ckso")
(check-expect (swap-parts "cat") "atc")
(define (swap-parts x)
(string-append
(back-part x)
(front-part x)))