(define (good-enough? guess x)
  (< (abs (- (square guess) x)) .001))
(define (square x)
  (* x x))


(define (sqrt x)
  (define (good-enough? guess x)
    (< (abs (- (square guess) x)) .001))
  (define (improve guess x)
    (average guess (/ x guess)))
  (define (sqrt-iter guess x)
    (if (good-enough? guess x)
         guess
         (sqrt-iter (improve guess x) x)))
  (sqrt-iter 1 x))


(define (sqrt x)
  (define (good-enough? guess)
    (< (abs (- (square guess) x)) .001))
  (define (improve guess)
    (average guess (/ x guess)))
  (define (sqrt-iter guess)
    (if (good-enough? guess x)
        guess
        (sqrt-iter (improve guess x) x)))
  (sqrt-iter 1 x))


(define (prime1? n)
  (= n (smallest-divisor n)))
(define (smallest-divisor n)
  (define (find-divisor n test-divisor)
    (cond ((> (square test-divisor) n) n)
          ((divides? test-divisor n) test-divisor)
          (else (find-divisor n (+ test-divisor 1)))))
  (find-divisor n 2))


(define (prime2? n)
  (define (format-test)
    (define a (+ 2 (random (- n 2))))
    (= (expmod a n n) a))
  (cond ((= times 0) #T)
        ((format-test) (prime2? n (- times 1)))
        (else '())))
(define (expmod b e m)
  (cond ((= e 0) 1)
        ((even? e) (remainder (square (expmod b (/ e 2) m)) m))
        (else (remainder (* b (expmod b (- e 1) m)) m))))


(define (sum-integer a b)
  (if (> a b)
      ()
      (+ a (sum (+ a 1) b))))
(define (sum-cube a b)
  (if (> a b)
      ()
      (+ (cube a) (sum-cube (+ a 1) b))))
(define (pi8-sum a b)
  (if (> a b)
      ()
      (+ (/ 1 (* a (+ a 2))) (pi8-sum (+ a 4) b))))
(define pi (* 8 (pi8-sum 1 1000)))


(define (sum term a next b)
  (if (> a b)
      ()
      (+ (term a) (sum term (next a) next b))))
(define (sum-cube2 a b)
  (sum cube a 1+ b))
(define (pi8-sum2 a b)
  (define (pi-term x)
    (/ 1 (* x (+ x 2))))
  (define (pi-next x)
    (+ x 4))
  (sum pi-term a pi-next b))
(define (sum-integer2 a b)
  (define (id x) x)
  (sum id a 1+ b))
(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* dx (sum f a add-dx b)))


(define (sum-integer a b)
  (sum (lambda(x) x) a 1+ b)
(define (integral f a b dx)
  (* dx (sum f a (lambda (x) (+ x dx)) b)))


(define (f x y)
  (define a (+ 1 (* x y)))
  (define b (- 1 y))
  (+ (* x (square a)) (* y b) (* a b)))


(define (f x y)
  (define f-helper a b)
    (+ (* x (square a)) (* y b) (* a b)))
  (f-helper (+ 1 (* x y)) (- 1 y)))


(define (f x y)
  ((lambda (a b)
     (+ (* x (square a)) (* y b) (* a b)))
   (+ 1 (* x y)) (- 1 y)))


(define (f x y)
  (let ((a (+ 1 (* x y)))
        (b (- 1 y)))
    (+ (* x (square a)) (* y b) (* a b))))


(define zero
  (lambda (f) 0))
(define (next n)
  (lambda (f)
    (f (n f))))


(define zero
  (lambda (f)
    (lambda (nula)
      nula)))
(define (next n)
  (lambda (f)
    (lambda (nula)
      (f ((n f) nula)))))


(define (plus m n)
  (lambda (f)
    (lambda (nula)
      ((m f) ((n f) nula)))))


(define (krat m n)
  (lambda (f)
    (lambda (nula)
      ((m (n f)) nula))))


(define (na m n)
  (lambda (f)
    (lambda (nula)
      (((n m) f) nula))))