exercise-15.scm (1094B)
1 (define-library (sicp solutions chapter-1 exercise-15) 2 (import (scheme base) 3 (scheme write)) 4 (export sine 5 sine-times) 6 7 (begin 8 (define (cube x) (* x x x)) 9 10 (define (p x) (- (* 3 x) (* 4 (cube x)))) 11 12 (define (sine angle) 13 (if (not (> (abs angle) 0.1)) 14 angle 15 (p (sine (/ angle 3.0))))) 16 17 (define (sine-times angle) 18 (let loop ((angle angle) 19 (times 1)) 20 (display (string-append "angle: " 21 (number->string angle) 22 "; times: " 23 (number->string times))) 24 (newline) 25 (if (not (> (abs angle) 0.0001)) 26 (cons angle 27 times) 28 (let* ((result (loop (/ angle 3.0) 29 (+ times 1))) 30 (approx (p (car result)))) 31 (display (string-append "approximation: " 32 (number->string approx))) 33 (newline) 34 (cons approx 35 (cdr result))))))))