`
t0uch
  • 浏览: 59568 次
  • 性别: Icon_minigender_1
  • 来自: 柳州
社区版块
存档分类
最新评论

SICP 2.33~2.39习题答案

    博客分类:
  • SICP
阅读更多
(define (filter predicate sequence)
  (cond ((null? sequence) nill)
        ((predicate (car sequence))
         (cons (car sequence)
               (filter predicate (cdr sequence))))
        (else (filter predicate (cdr sequence)))))

(define (accumulate op initial sequence)
  (if (null? sequence)
      initial
      (op (car sequence)
          (accumulate op initial (cdr sequence)))))

(define (enumerate-interval low high)
  (if (> low high)
      nil
      (cons low (enumerate-interval (+ low 1) high))))

;2.33
(define (map p sequence)
  (accumulate (lambda (x y) (cons (p x) y)) nil sequence))
;(map (lambda (x) (* x x)) (list 1 2 3 4))

(define (append seq1 seq2)
  (accumulate cons seq2 seq1))
;(append (list 1 2 3) (list 3 4 5))

(define (length sequence)
  (accumulate (lambda (x y) (+ 1 y)) 0 sequence))
;(length (list 1 2 3))

;2.34
(define (horner-eval x coefficient-sequence)
  (accumulate (lambda (this-coeff higher-terms)
                (+ this-coeff (* x higher-terms)))
              0
              coefficient-sequence))

;(horner-eval 2 (list 1 3 0 5 0 1))

;2.35
(define (count-leaves t)
  (accumulate +
              0
              (map (lambda (x)
                     (if (pair? x) (count-leaves x)
                         1))
                   t)))

;(define x (cons (list 1 2) (list 3 4)))
;(count-leaves (list x x))

;2.36
(define (accumulate-n op init seqs)
  (if (null? (car seqs))
      nil
      (cons (accumulate op init (map car seqs))
            (accumulate-n op init (map cdr seqs)))))

(define s (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12)))
;(accumulate-n + 0 s)

;; 2.38
(define (fold-left op initial sequence)
  (define (iter result rest)
    (if (null? rest)
        result
        (iter (op result (car rest))
              (cdr rest))))
  (iter initial sequence))

(define (fold-right op initial sequence)
  (accumulate op initial sequence))
;(fold-right / 1 (list 1 2 3))
;(fold-left / 1 (list 1 2 3))

;(fold-right list nil (list 1 2 3))
;(fold-left list nil (list 1 2 3))

;;2.39
(define (reverse sequence)
  (fold-right (lambda (x y)
                (append y
                        (cons (if (pair? x) (reverse x)
                                  x)
                              nil)))
              nil
              sequence))

(define (reverse sequence)
  (fold-left (lambda (x y)
                (append (cons (if (pair? y) (reverse y)
                                  y)
                              nil)
                        x))
             nil
             sequence))

;(reverse (list 1 2 3 4 5 6))

2.37中的线性代数都已经忘光了,略过。
2.38的答案是,op要符合 a op b = b op a 的原则,如+,*。
分享到:
评论

相关推荐

    SICP 习题答案

    《计算机程序的构造和解释》...通过解答SICP的习题,读者将深入理解这些概念,并能运用到实际的编程实践中。习题旨在促进对这些基本原理的深入思考,帮助程序员建立坚实的基础,进而在面对复杂的编程挑战时能游刃有余。

    sicp第二章练习题的解答

    以上就是基于文件名推测的SICP第二章练习题相关知识点。这些内容深入地涵盖了函数式编程的基础和应用,对于提升编程思维和技能大有裨益。实际的学习过程中,通过阅读和理解这些代码,结合原书的理论部分,将有助于...

    SICP习题解答,主要第一章的内容习题答案

    《SICP习题解答,主要第一章的内容习题答案》 SICP,全称《Structure and Interpretation of Computer Programs》(计算机程序的构造和解释),是计算机科学领域的一本经典教材,由MIT(麻省理工学院)的 Harold ...

    SICP-计算机课后习题资源

    What I have stored is the reading notes of SICP and the solutions of the exercises after class.:art:SICP()

    SICP中文第二版

    SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版

    SICP 解题集

    《SICP解题集》是一份专注于探讨和解答《结构与解释程序》(Structure and Interpretation of Computer Programs,简称SICP)一书中习题的资源。SICP是计算机科学领域的一本经典教材,由Harold Abelson、Gerald Jay ...

    sicp in python 中文 sicp 中文

    sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh

    sicp:我的 SICP 练习

    在" sicp-master "这个压缩包中,可能包含的是对SICP各章节练习题的解答,包括源代码、注释和分析。这些练习通常涵盖了函数式编程的基础,如高阶函数、递归、闭包,以及更高级的主题,如过程构造、数据结构、环境...

    SICP(python中文带书签)

    《计算机程序的构造与解释》(Structure and Interpretation of Computer Programs,简称SICP)是一本备受推崇的经典计算机科学教材,由Harold Abelson和Gerald Jay Sussman撰写,并由MIT出版社出版。这本书以其深入...

    sicp 2.2.4节图形语言

    《SICP 2.2.4 节:图形语言》是计算机科学经典教材《结构与解释程序》(Structure and Interpretation of Computer Programs)中的一个重要章节,它深入介绍了如何利用编程来创建图形,以及如何设计和理解复杂的计算...

    北京大学,计算机程序构造和解释(SICP)课件,裘宗燕老师主讲

    《计算机程序构造和解释》(SICP,Structure and Interpretation of Computer Programs)是一本具有深远影响力的计算机...压缩包中的“SICP 北大课件”文件可能包含课件、讲义、习题解答等资料,是学习SICP的宝贵资源。

    SICP-Python版本

    SICP-Python版本

    SICP 使用的scheme解释器

    SICP 使用的scheme解释器 以前叫DrScheme

Global site tag (gtag.js) - Google Analytics