练习2.53-练习2.56

1.练习2.53

'(a b c)
'((george))
'((y1 y2))
'(y1 y2)
#f
#f
'(red shoes blue socks)

2.练习2.54

(define (equal? s1 s2)
(cond ((and (null? s1) (null? s2)) true)
((or (null? s1) (null? s2)) false)
(else (let ((per-s1 (car s1))
(per-s2 (car s2))
(rest-s1 (cdr s1))
(rest-s2 (cdr s2)))
(cond ((and (not (pair? per-s1))
(not (pair? per-s2)))
(and true
(equal? rest-s1 rest-s2)))
((and (pair? per-s1)
(pair? per-s2))
(and (equal? per-s1 per-s2)
(equal? rest-s1 rest-s2)))
(else false))))))

3.练习2.55

4.练习2.56

(define (exponentiation? x)
(and (pair? x) (eq? (car x) '**)))

(define (base x)

(define (exponent x)

(define (make-exponentiation base exp)
(cond ((=number? exp 0) 1)
((=number? exp 1) base)
((=number? base 1) base)
(else (list '** base exp))))

5.练习2.57

(define (sum? x)
(and (pair? x) (eq? (car x) '+)))

(define (augend s)
(if (null? (cdddr s))
(cons '+ (cddr s))))

(define (product? x)
(and (pair? x) (eq? (car x) '*)))

(define (multiplicand p)
(if (null? (cdddr p))
(cons '* (cddr p))))


6.练习2.58

A.

(define (sum? x)
(and (pair? x) (eq? (cadr x) '+)))

(car s))

(define (augend s)

(define (product? x)
(and (pair? x) (eq? (cadr x) '*)))

(define (multiplier p) (car p))

(define (multiplicand p) (caddr p))

B.

(define (sum? x)
(if (null? x)
false
(or (eq? (car x) '+) (sum? (cdr x)))))

(if (eq? (car s1) '+)
result
(addend-iter (append result (list (car s1))) (cdr s1))))
(if (= (length res) 1)
(car res)
res)))

(define (augend s)
(define (augend-iter s1)
(if (eq? (car s1) '+)
(cdr s1)
(augend-iter (cdr s1))))
(let ((res (augend-iter s)))
(if (= (length res) 1)
(car res)
res)))

(define (product? x)
(if (null? x)
false
(if (eq? (car x) '+)
false
(or (not (eq? (car x) '*))(product? (cdr x))))))

(define (multiplier p)
(define (multiplier-iter result p1)
(if (eq? (car p1) '*)
result
(multiplier-iter (append result (list (car p1))) (cdr p1))))
(let ((res (multiplier-iter '() p)))
(if (= (length res) 1)
(car res)
res)))

(define (multiplicand p)
(define (last-part p2)
(if (eq? (car p2) '*)
(cdr p2)
(last-part (cdr p2))))
(let ((res (last-part p)))
(if (= (length res) 1)
(car res)
res)))

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客