欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SICP 2.17~2.23 习题答案

程序员文章站 2022-07-14 10:55:37
...
这一节比较容易

(define (square n) (* n n))

(define (list-ref items n)  
   (if (= n 0)  
       (car items)  
       (list-ref (cdr items) (- n 1))))  
   
(define (length items)  
  (if (null? items)  
      0  
      (+ 1 (length (cdr items)))))

(define (append list1 list2)
  (if (null? list1)
      list2
      (cons (car list1) (append (cdr list1) list2))))
  
;; 2.17  
(define (last-pair items)  
  (if (null? (cdr items)) (car items)  
       (last-pair (cdr items))))  
;(last-pair (list 23 72 149 34))  
   
;; 2.18  
(define (reverse items)  
  (define (reverse-iter items new-item counter)  
    (cond ((null? (cdr items)) (cons (car items) new-item))  
           ((= 0 counter) (reverse-iter (cdr items) (cons (car items) nil) (+ counter 1)))  
           (else (reverse-iter (cdr items) (cons (car items) new-item) (+ counter 1)))))
  (reverse-iter items nil 0))  
  
;(reverse (list 1 4 9 16 25))

;; 2.19
(define (no-more? coin-values) (null? coin-values))

(define (except-first-denomination coin-values) (cdr coin-values))

(define (first-denomination coin-values) (car coin-values))

(define (cc amount coin-values)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (no-more? coin-values)) 0)
        (else
         (+ (cc amount
                (except-first-denomination coin-values))
            (cc (- amount
                   (first-denomination coin-values))
                coin-values)))))

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

;; (cc 100 us-coins)

;; 2.20
(define (same-parity . items)
  (let ((parity (even? (car items))))
    (define (same-parity-iter items)
      (cond ((null? items) nil)
            ((equal? parity (even? (car items))) (cons (car items) (same-parity-iter (cdr items))))
            (else (same-parity-iter (cdr items)))))
    (same-parity-iter items)))

;(same-parity 1 2 3 4 5 6 7 8)

(define (map proc items)
  (if (null? items)
      nil
      (cons (proc (car items)) (map proc (cdr items)))))

(define (scale-list items factor)
  (map (lambda (x) (* x factor))
       items))

;;2.21
;(define (square-list items)
;  (if (null? items)
;      nil
;      (cons (square (car items)) (square-list (cdr items)))))
;(define (square-list items)
;  (map (lambda (x) (* x x)) items))

;2.22
(define (square-list items)
  (define (iter things answer)
    (if (null? things)
        answer
        ;(iter (cdr things)
        ;      (cons (square (car things))
        ;            answer))
        (cons (square (car things)) (iter (cdr things) nil))
        ))
  (iter items nil))

;(square-list (list 1 2 3 4))

;(define (for-each proc items)
;  (cond ((not (null? items))
;         (proc (car items))
;         (for-each proc (cdr items)))))

(define (for-each proc items)
  (if (not (null? items))
      (let ()
       (proc (car items))
       (for-each proc (cdr items)))))

(for-each (lambda (x) (newline) (display x))
          (list 57 321 88))
相关标签: Scheme