;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; ;;; file init.scm ;;; ;;; ICS 22 Scheme lab assignment file ;;; ;;; ;;; ;;; Revised and tested for Windows NT by ;;; ;;; Li-Wei (Gary) Chen ;;; ;;; Summer 1998 ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Redefine "car" and "cdr" for clarity (define first car) (define rest cdr) ;; Define atom? in terms of pair? (define atom? (lambda (x) (not (pair? x)))) ;; Define some math functions for fun (define (fact n) (cond ((<= n 0) 1) ((= n 1) 1) (else (* n (fact (- n 1)))))) (define pi 3.14159265358979323846264338327950288419716939937510582097494459230781620628620862) ; Here is some code to output rational numbers as inexact, fixed-point ; decimals, instead. ;; Construct the numeral for the correctly rounded ;; approximation to num, then (if necessary) pad it on the ;; left with spaces until it is field-width columns wide. (define (output-fixed-point num field-width fraction-length) (pad-on-left (rounded-numeral num fraction-length) #\space field-width)) ;; To construct the numeral for a real number, rounded to a ;; specified number of places after the decimal point: ;; ;; (1) If fraction-length is zero, so that no decimals ;; should appear, just round off the number and push it ;; through number->string. ;; ;; (2) Otherwise, determine the sign, separate the integer ;; part of the number's absolute value from its fractional ;; part, multiply the fractional part by the power of ten ;; that will push the required number of decimal places to ;; the left-hand side of the decimal point, and round it ;; off. Check whether this generates a carry; if so, the ;; integer part should be one larger and the digits after ;; the decimal point should be 0. Call the empoint ;; procedure to put the pieces of the numeral together and ;; attach the sign. (define (rounded-numeral num fraction-length) (if (zero? fraction-length) (number->string (inexact->exact (round num))) (let* ((sign (if (negative? num) "-" "")) (abs-num (abs num)) (integer-part (inexact->exact (truncate abs-num))) (fractional-part (- abs-num integer-part)) (frac-multiplier (expt 10 fraction-length)) (postpoint (inexact->exact (round (* fractional-part frac-multiplier))))) (string-append sign (if (= postpoint frac-multiplier) (empoint (+ integer-part 1) 0 fraction-length) (empoint integer-part postpoint fraction-length)))))) ;; Given an integer to print to the left of a decimal point ;; and an integer representing a decimal fraction to print ;; to the right of a decimal point, convert both to strings ;; and pad the latter on the left with enough zeroes to ;; bring it up to the specified fraction-length, then ;; concatenate the strings, inserting a decimal point ;; between them. (define (empoint int frac fraction-length) (string-append (number->string int) "." (pad-on-left (number->string frac) #\0 fraction-length))) ;; To pad a string on the left with copies of a specified ;; character in order to bring it up to a specified minimum ;; length, check whether any padding is needed, and if it ;; is, prepend a string consisting of copies of the pad ;; character and equal in length to the difference between ;; the specified minimum length and the current length. (define (pad-on-left str pad-char desired-length) (let ((len (string-length str))) (if (<= desired-length len) str (string-append (make-string (- desired-length len) pad-char) str))))