SICP 2.27 2.28 2.29
SICP 2.27 2.28 2.29
代码强国 发表于2年前
SICP 2.27 2.28 2.29
  • 发表于 2年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

2.27

(define (deep-reverse lst)
  (define (x-reverse l r)
    (cond ((null? l) r)
          ((pair? (car l)) (x-reverse (cdr l) (cons (x-reverse (car l) '()) r)))
          (else (x-reverse (cdr l) (cons (car l) r)))))
  (x-reverse lst '()))

2.28

(define (my-fringe lst)
  (define (x-fringe l res)
    (cond ((pair? l) (x-fringe (car l) (x-fringe (cdr l) res)))
          ((null? l) res)
          (else (cons l res))))
  (x-fringe (car lst) (x-fringe (cdr lst) '())))

2.29

(define (left-branch x) (car x))
(define (right-branch x) (car (cdr x)))
(define (branch-length x) (car x))
(define (branch-structure x) (car (cdr x)))

(define (is-branch? x) (number? (branch-length x)))
(define (total-weight x)
  (if (is-branch? x)
      (branch-structure x)
      (+ (total-weight (left-branch x))
         (total-weight (right-branch x)))))

(define (get-moment x)
  (* (branch-length x) (branch-structure x)))
(define moment-add +)
(define (total-moment x)
  (if (is-branch? x)
      (get-moment x)
      (moment-add (total-moment (left-branch x))
                  (total-moment (right-branch x)))))
(define (mobile-equilibrium? x)
  (if (is-branch? x)
      #t
      (let ((left (left-branch x))
            (right (right-branch x)))
        (if (= (total-moment left) (total-moment right))
            (and (mobile-equilibrium? left)
                 (mobile-equilibrium? right))
            #f))))

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 1
博文 45
码字总数 8396
×
代码强国
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: