《SICP》中的一些练习题

1.16

(define (square n) (* n n))
(define (fast-expt b n)
  (cond ((= n 0) 1)
        ((even? n) (square (fast-expt b (/ n 2))))
        (else (* b (fast-expt b (- n 1))))))
(define (my-expt b n)
  (define (expt-iter b n a)
    (cond ((= n 0) a)
          ((even? n) (expt-iter (square b) (/ n 2) a))
          (else (expt-iter b (- n 1) (* a b)))))
  (expt-iter b n 1))

1.30

(define (sum term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ (term a) result))))
  (iter a 0))

1.32

(define (accumulate combiner null-value term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (combiner (term a) result))))
  (iter a null-value))
(define (ac-sum term a next b)
  (accumulate + 0 term a next b))
(define (ac-product term a next b)
  (accumulate * 1 term a next b))

1.33

(define (identity x) x)
(define (gcd a b)
  (if (= b 0)
      a
      (gcd b (remainder a b))))
(define (filtered-accumulate 
         filter combiner null-value term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (let ((x (term a)))
          (if (filter x) (iter (next a) (combiner x result)) (iter (next a) result)))))
  (iter a null-value))
(define (question-a a b)
  (filtered-accumulate prime? + 0 identity a inc b))
(define (question-b n)
  (define (filter x)
    (if (= (gcd x n) 1)
        true
        false))
  (filtered-accumulate filter * 1 identity 1 inc (- n 1)))

1.41 1.42 1.43

(define (double f)
  (lambda (x)
    (f (f x))))
(define (compose f g)
  (lambda (x)
    (f (g x))))
(define (repeated f n)
  (define (iter f n result)
    (cond ((<= n 0) result)
      ((even? n) (iter (compose f f) (/ n 2) result))
      (else (iter f (- n 1) (compose result f)))))
  (iter f n (lambda (x) x)))

2.27 2.28 2.30 2.31 2.32

(define (count-leaves x)
  (cond ((null? x) 0)
        ((not (pair? x)) 1)
        (else (+ (count-leaves (car x))
                 (count-leaves (cdr x))))))
(define (deep-reverse x)
  (cond ((null? x) nil)
        ((not (pair? x)) x)
        (else (append (deep-reverse (cdr x)) (list (deep-reverse (car x)))))))
(define (fringe x)
  (cond ((null? x) nil)
        ((pair? x) (append (fringe (car x)) (fringe (cdr x))))
        (else (list x))))
(define (square-tree-1 tree)
  (cond ((null? tree) nil)
        ((not (pair? tree)) (* tree tree))
        (else (cons (square-tree-1 (car tree))
                    (square-tree-2 (cdr tree))))))
(define (square-tree-2 tree)
  (map (lambda (sub-tree)
         (if (pair? sub-tree)
             (square-tree-2 sub-tree)
             (* sub-tree sub-tree)))
  tree))
(define (tree-map func tree)
  (cond ((null? tree) nil)
        ((not (pair? tree)) (func tree))
        (else (cons (tree-map func (car tree))
                    (tree-map func (cdr tree))))))
(define (subsets s)
  (if (null? s)
      (list nil)
      (let ((rest (subsets (cdr s))))
          (append rest (map (lambda (x) (cons (car s) x)) rest)))))

2.33

(define (accumulate op init seq)
  (if (null? seq)
      init
      (op (car seq)
          (accumulate op init (cdr seq)))))
;-----2.33-----
(define (rmap p seq)
  (accumulate (lambda (x y)
                (cons (p x) y))
              nil 
              seq))
(define (rappend seq1 seq2)
  (accumulate cons seq2 seq1))
(define (rlength seq)
  (accumulate (lambda (x y)
                (+ 1 y))
              0
              seq))

3.1 3.2 3.3

;-----3.1-----
(define (make-accumulator init)
  (let ((sum init))
    (lambda (x)
      (set! sum (+ sum x))
      sum)))
;-----3.2-----
(define (make-monitored f)
  (let ((cnt 0))
    (lambda (x)
      (cond ((eq? x 'how-many-calls?) cnt)
            ((eq? x 'reset-count) (begin (set! cnt 0) cnt))
            (else (begin (set! cnt (inc cnt)) (f x)))))))
;-----3.3-----
(define (make-account balance password)
  (let ((cnt 0))
    (define (withdraw amount)
      (if (>= balance amount)
          (begin (set! balance (- balance amount)) balance)
          "Insufficient funds"))
    (define (deposit amount)
      (set! balance (+ balance amount))
      balance)
    (define (call-the-cops)
      "call the cops")
    (define (dispatch p m)
      (if (not (eq? p password)) 
          (begin (set! cnt (+ cnt 1)) (if (>= cnt 3) (call-the-cops) "Incorrect password"))
          (begin (set! cnt 0) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE-ACCOUNT" m))))))
  dispatch))

你可能感兴趣的:(编程语言)