练习2.53-练习2.56

15 篇文章 0 订阅

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)
  (cadr x))

(define (exponent x)
  (caddr 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 (addend s)
  (cadr s))

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

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

(define (multiplier p) (cadr p))

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

6.练习2.58

A.

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

(define (addend s)
  (car s))

(define (augend s)
  (caddr 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)))))

(define (addend s)
  (define (addend-iter result s1)
    (if (eq? (car s1) '+) 
        result
        (addend-iter (append result (list (car s1))) (cdr s1))))
  (let ((res (addend-iter '() s)))
    (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)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值