2.1
(define (make-rat n d)
(let ((g (gcd n d)))
(cond ((and (< n 0) (> d 0)) (cons (/ n g) (/ d g)))
((and (> n 0) (> d 0)) (cons (/ n g) (/ d g)))
(else (cons (/ (- n) g) (/ (- d) g)))
)))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x)))
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(define (equal-rat? x y)
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
(define one-half (make-rat -1 -2))
(print-rat one-half)
;;(define one-third (make-rat 1 3))
;;(print-rat (add-rat one-half one-third))
2.2
(define (average x y)
(/ (+ x y) 2))
(define (make-point x y)
(cons x y))
(define (x-point x) (car x))
(define (y-point x) (cdr x))
(define (print-point p)
(newline)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))
(define test-point (make-point 1 2))
(print-point test-point)
(define (make-segment start end)
(cons start end))
(define (start-segment segment)
(car segment))
(define (end-segment segment)
(cdr segment))
(define (midpoint-segment segment)
(let ((x (average (car (car segment)) (car (cdr segment))))
(y (average (cdr (cdr segment)) (cdr (car segment)))))
(make-point x y)))
(define (print-segment s)
(newline)
(display "[")
(print-point (car s))
(display ",")
(print-point (cdr s))
(display "]"))
(define start-point (make-point 1 2))
(define end-point (make-point 3 4))
(define one-segment (make-segment start-point end-point))
(print-segment one-segment)
(print-point (midpoint-segment one-segment))
2.3
(define (average x y)
(/ (+ x y) 2))
(define (square x)
(* x x))
(define (make-point x y)
(cons x y))
(define (x-point x) (car x))
(define (y-point x) (cdr x))
(define (print-point p)
(newline)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))
(define (make-segment start end)
(cons start end))
(define (start-point segment)
(car segment))
(define (end-point segment)
(cdr segment))
(define (make-rectangle segment1 segment2)
(cons segment1 segment2))
(define (segment-len segment)
(sqrt (+ (square (- (x-point (start-point segment)) (x-point (end-point segment))))
(square (- (y-point (start-point segment)) (y-point (end-point segment)))))))
(define (print-segment s)
(newline)
(display "[")
(print-point (car s))
(display ",")
(print-point (cdr s))
(display "]"))
(define (make-rectangle segment1 segment2)
(cons segment1 segment2))
(define (rectangle-segment1 r) (car r))
(define (rectangle-segment2 r) (cdr r))
(define (rectangle-area rectangle)
(* (segment-len (rectangle-segment1 rectangle)) (segment-len (rectangle-segment2 rectangle))))
(define (rectangle-length rectangle)
(* (+ (segment-len (rectangle-segment1 rectangle)) (segment-len (rectangle-segment2 rectangle)))))
(define one-segment (make-segment (make-point 1 2) (make-point 3 2)))
(define two-segment (make-segment (make-point 1 4) (make-point 3 4)))
(display (segment-len one-segment))
(newline)
(display (segment-len two-segment))
(newline)
(define one-rectangle (make-rectangle one-segment two-segment))
(display (rectangle-area one-rectangle))
(newline)
(display (rectangle-length one-rectangle))
2.4
(define (my-cons x y)
(lambda (m) (m x y)))
(define (my-car z)
(z (lambda (p q) p)))
(define (my-cdr z)
(z (lambda (p q) q)))
(define point (my-cons 1 2))
(my-car point)
(my-cdr point)
2.5
(define (power n m)
(if (= m 1)
n
(* n (power n (- m 1)))))
(define (my-cons x y)
(* (power 2 x) (power 3 y)))
(define (my-car c)
(define (iter c result)
(if (= (remainder c 2) 0)
(iter (/ c 2) (+ result 1))
result))
(iter c 0))
(define (my-cdr c)
(define (iter c result)
(if (= (remainder c 3) 0)
(iter (/ c 3) (+ result 1))
result))
(iter c 0))
(define c (my-cons 3 2))
(my-car c)
(my-cdr c)
分享到:
相关推荐
《计算机程序的构造和解释》...通过解答SICP的习题,读者将深入理解这些概念,并能运用到实际的编程实践中。习题旨在促进对这些基本原理的深入思考,帮助程序员建立坚实的基础,进而在面对复杂的编程挑战时能游刃有余。
以上就是基于文件名推测的SICP第二章练习题相关知识点。这些内容深入地涵盖了函数式编程的基础和应用,对于提升编程思维和技能大有裨益。实际的学习过程中,通过阅读和理解这些代码,结合原书的理论部分,将有助于...
《SICP习题解答,主要第一章的内容习题答案》 SICP,全称《Structure and Interpretation of Computer Programs》(计算机程序的构造和解释),是计算机科学领域的一本经典教材,由MIT(麻省理工学院)的 Harold ...
在" sicp-master "这个压缩包中,可能包含的是对SICP各章节练习题的解答,包括源代码、注释和分析。这些练习通常涵盖了函数式编程的基础,如高阶函数、递归、闭包,以及更高级的主题,如过程构造、数据结构、环境...
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
《SICP解题集》是一份专注于探讨和解答《结构与解释程序》(Structure and Interpretation of Computer Programs,简称SICP)一书中习题的资源。SICP是计算机科学领域的一本经典教材,由Harold Abelson、Gerald Jay ...
《计算机程序的构造与解释》(Structure and Interpretation of Computer Programs,简称SICP)是一本备受推崇的经典计算机科学教材,由Harold Abelson和Gerald Jay Sussman撰写,并由MIT出版社出版。这本书以其深入...
"sicp-solutions"是一个针对该书练习题的解答集,主要使用了Scheme语言,一个Lisp方言,而具体的实现环境是mit-scheme 9.2。 Scheme语言是Lisp家族的一员,以其简洁的语法和强大的函数式编程特性闻名。在"sicp-...
《SICP 2.2.4 节:图形语言》是计算机科学经典教材《结构与解释程序》(Structure and Interpretation of Computer Programs)中的一个重要章节,它深入介绍了如何利用编程来创建图形,以及如何设计和理解复杂的计算...
书中大量的练习题和示例代码鼓励读者动手实践,加深对概念的理解。此外,SICP还引入了许多先进的编程思想和技术,如函数式编程、递归、抽象数据类型等,这些都对现代软件开发产生了深远的影响。 #### 四、总结 ...
scheme编译器最新版,Windows下直接安装即可运行,sicp刷题必备。祝大家早日学到sicp之精髓~
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
《计算机程序构造和解释》(SICP,Structure and Interpretation of Computer Programs)是一本具有深远影响力的计算机...压缩包中的“SICP 北大课件”文件可能包含课件、讲义、习题解答等资料,是学习SICP的宝贵资源。
Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用
《SICP》全称是《Structure and Interpretation of Computer Programs》,中文译为《计算机程序的构造和解释》。这是一本经典的计算机科学教材,由Harvard大学的 Harold Abelson 和 Gerald Jay Sussman 教授撰写,...
本书名为《a_book_sicp_py》,是一本以Python语言为基础介绍设计模式和计算机科学基础的书籍。根据描述和部分内容,可以提炼出以下知识点: 1. 编程语言的重要性:在计算机科学的宽泛领域中,编程语言扮演着至关...
标题中的"PyPI 官网下载 | sicp-0.0.1b102.dev4.tar.gz"指的是从Python的官方包索引(Python Package Index,简称PyPI)上下载的一个名为"sicp"的软件包的版本号为0.0.1b102.dev4的压缩文件,其格式是tar.gz。...