Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed. Added test

  • Loading branch information...
commit 8c04e7c66f49a794e0b08be51b123b3146a7d009 1 parent 6f71043
Igor Dobryn authored
2  ex_1_015.scm
@@ -3,8 +3,6 @@
3 3 (define (p x) (- (* 3 x) (* 3 (cube x))))
4 4
5 5 (define (sine angle)
6   - (display "1")
7   - (newline)
8 6 (if (not (> (abs angle) 0.1))
9 7 angle
10 8 (p (sine (/ angle 3.0)))))
22 ex_1_016.scm
... ... @@ -0,0 +1,22 @@
  1 +(require rackunit)
  2 +
  3 +(define (square x)
  4 + (* x x))
  5 +
  6 +(define (exponent b n)
  7 + (define (exp-iter b n a)
  8 + (cond ((= n 0) a)
  9 + ((even? n) (exp-iter (square b)
  10 + (/ n 2)
  11 + a))
  12 + (else (exp-iter b
  13 + (- n 1)
  14 + (* a b)))))
  15 + (if (< n 0)
  16 + (/ 1
  17 + (exp-iter b (- n) 1))
  18 + (exp-iter b n 1)))
  19 +
  20 +(check-equal? (exponent 3 4) 81)
  21 +(check-equal? (exponent 3 3) 27)
  22 +(check-equal? (exponent 3 -1) 1/3)
24 ex_1_017.scm
... ... @@ -0,0 +1,24 @@
  1 +(require rackunit)
  2 +
  3 +(define (double a)
  4 + (arithmetic-shift a 1))
  5 +
  6 +(define (halve a)
  7 + (arithmetic-shift a (- 1)))
  8 +
  9 +(define (square a)
  10 + (* a a))
  11 +
  12 +(define (* a b)
  13 + (if (= b 0)
  14 + 0
  15 + (+ a (* a (- b 1)))))
  16 +
  17 +(define (fast-expt b n)
  18 + (cond ((= n 0) 1)
  19 + ((even? n) (square (fast-expt b (/ n 2))))
  20 + (else (* b (fast-expt b (- n 1))))))
  21 +
  22 +
  23 +(check-equal? (fast-expt 5 4) 625)
  24 +(check-equal? (fast-expt 3 3) 27)
17 ex_1_018.scm
... ... @@ -0,0 +1,17 @@
  1 +(require rackunit)
  2 +
  3 +(define (double a)
  4 + (arithmetic-shift a 1))
  5 +
  6 +(define (halve a)
  7 + (arithmetic-shift a (- 1)))
  8 +
  9 +(define (* a b)
  10 + (cond ((= b 0) 0)
  11 + ((even? b) (double (* a (halve b))))
  12 + (else (+ a (* a (- b 1))))))
  13 +
  14 +(check-equal? (* 5 4) 20)
  15 +(check-equal? (* 3 3) 9)
  16 +(check-equal? (* 10 27) 270)
  17 +

0 comments on commit 8c04e7c

Please sign in to comment.
Something went wrong with that request. Please try again.