#lang scheme ; creates a module, allowing all functions in module to be executed in order ; to run, type mzscheme ; or run mzscheme, and type (load "") ; to compile and execute separately ; mzc --exe ; ./ ;; exercise 1 (define (sphereVol r) ;; calculate the volume of a sphere of radius r (* (/ 4 3) (* 3.14159 (* r r r)) ) ) (define (letDemo) (let ( (a 3) (b 4) ) (printf "from letDemo~n") (printf "value of a is ~a~n" a) (printf "value of b is ~a~n" b) ) ) ;; exercise 3 (define (quadeq a b c) ;; use the quadratic formula (let ( (discrim (- (* b b) (* 4 a c))) ) (cond ( (> discrim 0) (printf "double real roots ~a and ~a~n" (/ (+ (- 0 b) (sqrt discrim)) (* 2 a)) (/ (- (- 0 b) (sqrt discrim)) (* 2 a)) ) ) ( (< discrim 0) (printf "double complex roots ~a and ~a~n" (/ (+ (- 0 b) (sqrt discrim)) (* 2 a)) (/ (- (- 0 b) (sqrt discrim)) (* 2 a)) ) ) (#t (printf "single root ~a~n" (/ (+ (- b) (sqrt discrim)) (* 2 a)) ) ) ) ) ) ;; exercise 4 (define (power a b) ;; assumes b is a non-negative integer (cond ( (= b 0) 1 ) ( (= b 1) a ) ( (> b 1) (* a (power a (- b 1))) ) ) ) ;; exercise 5 (define (numZeros aList) ;; number of zeros in a simple list (if (null? aList) 0 (if (= 0 (car aList)) (+ 1 (numZeros (cdr aList))) (numZeros (cdr aList)) ) ) ) ;; exercise 6 ;(define (min a b) (if (< a b) a b) ) ;(define (max a b) (if (> a b) a b) ) (define (fVal comparef aList) ;; return the comparef-est value in aList, assumes aList is not empty (if (null? (cdr aList)) (car aList) (comparef (car aList) (fVal comparef (cdr aList) ) ) ) ) (define (minMax aList) ;; return min and max of a non-empty list as a list of length 2 (list (fVal min aList) (fVal max aList) )) (when #t (letDemo) (printf "test ex. 1: Volume of a sphere of radius 1 is ~a~n" (sphereVol 1)) (printf "test ex. 3: Quadratic equation a=2 b=4 c=3~n") (quadeq 2 4 3) (printf "test ex. 4: 2 raised to the power 3 is ~a~n" (power 2 3)) (printf "test ex. 5: number of zeros in '(0 2 0 4 0 6) is ~a~n" (numZeros '(0 2 0 4 0 6) ) ) (let ( (theAnswer (minMax '(10 -2 0 4 20 6) ) ) ) (printf "test ex. 6: min and max in '(10 -2 0 4 20 6) is ~a and ~a~n" (car theAnswer) (cadr theAnswer)) ) )