练习2.41-练习2.47

16 篇文章 0 订阅

1.练习2.41

(define (seprate-except x except)
  (filter (lambda (couple) 
            (let ((first (car couple))
                  (second (cadr couple)))
            (and (not (= first except))
                 (not (= second except))
                 (not (= first second)))))
          (map (lambda (n) (list n (- x n)))
               (enumerate-interval 1 (- x 1)))
          )
  )

(define (flatmap proc seq)
  (accumulate append '() (map proc seq)))

(define (seprate-to-three n)
  (flatmap (lambda (x) 
             (map (lambda (y) (cons x y)) 
                  (seprate-except (- n x) x)))
           (enumerate-interval 1 (- n 1)))
  )

2.练习2.42

(define empty-board '())

(define (adjoin-position new-row k rest-of-queens)
  (cons (list k new-row) rest-of-queens))

(define (safe? k positions)
  (define (check-safe col row rest-positions)
    (if (null? rest-positions)
        true
        (let ((first-position-col (car (car rest-positions)))
              (first-position-row (cadr (car rest-positions)))
              (remain-positions (cdr rest-positions)))
          (and (not (= col first-position-col))
               (not (= row first-position-row))
               (not (= (abs (- col first-position-col))
                       (abs (- row first-position-row))))
               (check-safe col row remain-positions)))))
  (if (null? positions)
      true
      (let ((col (car (car positions)))
            (row (cadr (car positions)))
            (rest (cdr positions)))
        (if (null? rest)
            true
            (check-safe col row rest)))))

3.练习2.43

因为重复计算k-1的皇后问题。

4.练习2.44

(require (planet "sicp.ss" ("soegaard" "sicp.plt" 2 1)))

(define (up-split painter n)
  (if (= n 0)
      painter
      (let ((smaller (up-split painter (- n 1))))
        (below painter (beside smaller smaller)))))

5.练习2.45

(define (split action1 action2)
  (lambda (painter n)
    (if (= n 0)
        painter
        (let ((smaller ((split action1 action2) painter (- n 1))))
          (action1 painter (action2 smaller smaller))))))

6.练习2.46
(define (make-vect x y)
  (cons x y))

(define (xcor-vect v)
  (car v))

(define (ycor-vect v)
  (cdr v))

(define (add-vect v1 v2)
  (make-vect (+ (xcor-vect v1)
                (xcor-vect v2))
             (+ (ycor-vect v1)
                (ycor-vect v2))))

(define (sub-vect v1 v2)
  (make-vect (- (xcor-vect v1)
                (xcor-vect v2))
             (- (ycor-vect v1)
                (ycor-vect v2))))

(define (scale-vect v s)
  (make-vect (* s (xcor-vect v))
             (* s (ycor-vect v))))
7.练习2.47

(define (make-frame-list origin edge1 edge2)
  (list origin edge1 edge2))

(define (make-frame-cons origin edge1 edge2)
  (cons origin (cons edge1 edge2)))

(define (origin-frame-list frame)
  (car frame))

(define (edge1-frame-list frame)
  (cadr frame))

(define (edge2-frame-list frame)
  (caddr frame))


(define (origin-frame-cons frame)
  (car frame))

(define (edge1-frame-cons frame)
  (cadr frame))

(define (edge2-frame-cons frame)
  (cddr frame))


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

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

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

打赏作者

_海阔天空

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

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

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

打赏作者

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

抵扣说明:

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

余额充值