练习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)))

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

_海阔天空

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值