SICP-2.1.1节练习
SICP-2.1.3节练习

SICP-2.1.2节练习

lispor posted @ Feb 24, 2011 03:20:42 PM in Scheme with tags SICP , 1509 阅读

练习 2.2 - 2.3

 
练习 2.2:
Consider the problem of representing line segments in a plane. Each segment is represented as a pair
of points: a starting point and an ending point. Define a constructor make-segment and selectors
start-segment and end-segment that define the representation of segments in terms of
points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y
coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that
define this representation. Finally, using your selectors and constructors, define a procedure
midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose
coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll
need a way to print points:
(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))
我的解答:
程序:
(define (make-point x y)
  (cons x y))

(define (x-point point)
  (car point))

(define (y-point point)
  (cdr point))

(define (print-point point)
  (display "(")
  (display (x-point point))
  (display ",")
  (display (y-point point))
  (display ")")
  (newline))

(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 ((start (start-segment segment))
        (end (end-segment segment)))
    (make-point (/ (+ (x-point start)
                      (x-point end))
                   2.0)
                (/ (+ (y-point start)
                      (y-point end))
                   2.0))))

运行结果:
scheme@(guile-user)> (define p1 (make-point 3 6))
scheme@(guile-user)> (define p2 (make-point 4 3))
scheme@(guile-user)> (define s (make-segment p1 p2))
scheme@(guile-user)> (print-point (midpoint-segment s))
(3.5,4.5)
 
练习 2.3:
Implement a representation for rectangles in a plane. (Hint: You may want to make use of Exercise
2-2.) In terms of your constructors and selectors, create procedures that compute the perimeter and
the area of a given rectangle. Now implement a different representation for rectangles. Can you
design your system with suitable abstraction barriers, so that the same perimeter and area
procedures will work using either representation?
我的解答:
(define (square x)
  (* x x))

(define (make-point x y)
  (cons x y))

(define (x-point point)
  (car point))

(define (y-point point)
  (cdr point))

(define (print-point point)
  (display "(")
  (display (x-point point))
  (display ",")
  (display (y-point point))
  (display ")")
  (newline))

(define (distance-points point1 point2)
  (let ((x1 (x-point point1))
        (y1 (y-point point1))
        (x2 (x-point point2))
        (y2 (y-point point2)))
    (sqrt (+ (square (- x2 x1))
             (square (- y2 y1))))))

(define (make-segment start end)
  (cons start end))

(define (start-segment segment)
  (car segment))

(define (end-segment segment)
  (cdr segment))

(define (length-segment segment)
  (let ((start (start-segment segment))
        (end (end-segment segment)))
    (distance-points start end)))

(define (make-rect-1 base-segment height)
  (cons base-segment height))

(define (width-rect-1 rect)
  (let ((base-segment (car rect)))
    (length-segment base-segment)))

(define (height-rect-1 rect)
  (abs (cdr rect)))

(define (perimeter-rect-1 rect)
  (let ((height (height-rect-1 rect))
        (width (width-rect-1 rect)))
    (* 2 (+ width height))))

(define (area-rect-1 rect)
  (let ((height (height-rect-1 rect))
        (width (width-rect-1 rect)))
    (* width height)))

(define (make-rect-2 base-start base-end height)
  (cons base-start
        (cons base-end height)))

(define (width-rect-2 rect)
  (let ((start (car rect))
        (end (car (cdr rect))))
  (distance-points  start end)))

(define (height-rect-2 rect)
  (cdr (cdr rect)))

(define (perimeter-rect-2 rect)
  (let ((width (width-rect-2 rect))
        (height (height-rect-2 rect)))
    (* 2 (+ width height))))

(define (area-rect-2 rect)
  (let ((width (width-rect-2 rect))
        (height (height-rect-2 rect)))
    (* width height)))

(define (area-rect rect)
  (if (pair? (car (car rect)))
      (area-rect-1 rect)
      (area-rect-2 rect)))

(define (perimeter-rect rect)
  (if (pair? (car (car rect)))
      (perimeter-rect-1 rect)
      (perimeter-rect-2 rect)))


运行结果:
scheme@(guile-user)> (define p1 (make-point 3 6))
scheme@(guile-user)> (define p2 (make-point 3 9))
scheme@(guile-user)> (define s (make-segment p1 p2))
scheme@(guile-user)> (define rect1 (make-rect-1 s 6))
scheme@(guile-user)> (define rect2 (make-rect-2 p1 p2 6))
scheme@(guile-user)> (perimeter-rect rect1)
18.0
scheme@(guile-user)> (perimeter-rect rect2)
18.0
scheme@(guile-user)> (area-rect rect1)
18.0
scheme@(guile-user)> (area-rect rect2)
18.0

 

seo service UK 说:
Dec 06, 2023 03:37:36 AM

I really like your take on the issue. I now have a clear idea on what this matter is all about

레고 이벤트 혜택 说:
Dec 18, 2023 07:06:58 PM

I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious...

놈놈놈벳 도메인 说:
Dec 18, 2023 08:08:44 PM

Cool you write, the information is very good and interesting, I'll give you a link to my site. I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so sp

키톤 가입코드 说:
Dec 18, 2023 08:27:27 PM

I am impressed by the information that you have on this blog. It shows how well you understand this subject. wow... what a great blog, this writter who wrote this article it's realy a great blogger, this article so inspiring me to be a better person . Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. Cool stuff you have and you keep overhaul every one of us 

메이저공원 说:
Dec 18, 2023 08:46:55 PM

Outstanding article!  I want people to know just how good this information is in your article. Your views are much like my own concerning this subject. I will visit daily your blog because I know. It may be very beneficial for me.

메이저사이트 说:
Dec 18, 2023 09:06:36 PM

Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing.

스포츠토토 说:
Dec 18, 2023 09:17:39 PM

Nice post. I discover some thing much harder on various blogs everyday. Most commonly it is stimulating to study content from other writers and exercise a specific thing from their website. I’d opt to apply certain while using the content in this little blog whether or not you do not mind. Natually I’ll provide a link on your own internet weblog. Appreciate your sharing. I am curious to find out what blog system you are working with? I’m experiencing some small security issues with my latest website and I would like to find something more secure. 

안전놀이터추천 说:
Dec 18, 2023 09:43:35 PM

Cool you write, the information is very good and interesting, I'll give you a link to my site. I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so sp

먹튀검증사이트 说:
Dec 18, 2023 09:46:11 PM

Nice post. I learn some thing tougher on different blogs everyday. Most commonly it is stimulating to read content off their writers and practice something at their store. I’d opt to use some with all the content in my weblog whether or not you do not mind. Natually I’ll provide a link with your internet weblog. Thank you for sharing 

토토홍보게시판 说:
Dec 18, 2023 10:01:54 PM

I am impressed by the information that you have on this blog. It shows how well you understand this subject. wow... what a great blog, this writter who wrote this article it's realy a great blogger, this article so inspiring me to be a better person . Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. Cool stuff you have and you keep overhaul every one of us 

꽁머니 说:
Dec 18, 2023 10:39:54 PM

I am impressed by the information that you have on this blog. It shows how well you understand this subject. wow... what a great blog, this writter who wrote this article it's realy a great blogger, this article so inspiring me to be a better person . Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. Cool stuff you have and you keep overhaul every one of us

파워볼놀이터 说:
Dec 18, 2023 10:51:26 PM

Nice post. I learn some thing tougher on different blogs everyday. Most commonly it is stimulating to read content off their writers and practice something at their store. I’d opt to use some with all the content in my weblog whether or not you do not mind. Natually I’ll provide a link with your internet weblog. Thank you for sharing 

먹튀검증업체순위 说:
Dec 18, 2023 11:02:41 PM

It is a good site post without fail. Not too many people would actually, the way you just did. I am impressed that there is so much information about this subject that has been uncovered and you’ve defeated yourself this time, with so much quality. Good Works!

먹튀사이트 说:
Dec 18, 2023 11:04:06 PM

An impressive share, I merely with all this onto a colleague who had previously been conducting a little analysis within this. And that he the truth is bought me breakfast simply because I uncovered it for him.. here------------- I gotta bookmark this website it seems very useful very helpful. I enjoy reading it. I fundamental to learn more on this subject.. Thanks for the sake theme this marvellous post.. Anyway, I am gonna subscribe to your silage and I wish you post again soon. I am curious to find out what blog system you are utilizing? I’m having some minor security problems with my latest website and I would like to find something more secure.

토토사이트 说:
Dec 18, 2023 11:13:38 PM

severely of which saintly all the same, I adore people go in on top of that satisfying shots might be feature personss damaging get pleasure from increasingly being defrent mind overall poeple, 

온라인카지노 说:
Dec 18, 2023 11:15:04 PM

An impressive share, I merely with all this onto a colleague who had previously been conducting a little analysis within this. And that he the truth is bought me breakfast simply because I uncovered it for him.. here------------- I gotta bookmark this website it seems very useful very helpful. I enjoy reading it. I fundamental to learn more on this subject.. Thanks for the sake theme this marvellous post.. Anyway, I am gonna subscribe to your silage and I wish you post again soon. I am curious to find out what blog system you are utilizing? I’m having some minor security problems with my latest website and I would like to find something more secure.

카지노롤링총판 说:
Dec 18, 2023 11:36:23 PM

severely of which saintly all the same, I adore people go in on top of that satisfying shots might be feature personss damaging get pleasure from increasingly being defrent mind overall poeple, 

메이저파워볼사이트 说:
Dec 18, 2023 11:44:32 PM

I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious...

제왕카지노가입 说:
Dec 18, 2023 11:53:03 PM

I think I have never seen such blogs ever before that has complete things with all details which I want. So kindly update this ever for us

먹튀사이트 说:
Dec 18, 2023 11:58:11 PM

I am impressed by the information that you have on this blog. It shows how well you understand this subject. wow... what a great blog, this writter who wrote this article it's realy a great blogger, this article so inspiring me to be a better person . Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. Cool stuff you have and you keep overhaul every one of us 

메이저사이트 说:
Dec 19, 2023 12:05:31 AM

I am impressed by the information that you have on this blog. It shows how well you understand this subject. wow... what a great blog, this writter who wrote this article it's realy a great blogger, this article so inspiring me to be a better person . Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. Cool stuff you have and you keep overhaul every one of us 

홀덤사이트순위 说:
Dec 19, 2023 12:14:10 AM

These is apparently just like definitely great. Most of these modest items are designed through the use of variety of base consciousness. I love these significantly 

가입머니 说:
Dec 19, 2023 12:21:28 AM

Outstanding article!  I want people to know just how good this information is in your article. Your views are much like my own concerning this subject. I will visit daily your blog because I know. It may be very beneficial for me.

먹튀검증사이트안전놀이터 说:
Dec 19, 2023 12:29:23 AM

Whether you truly need a hot companion in excessive lodgings or homes, our horny female escorts in Goa will reach you inside a short period of time.

가족방 说:
Dec 19, 2023 12:33:05 AM

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well. Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers.

벳매니아가입 说:
Dec 19, 2023 12:44:04 AM

Nice post. I discover some thing much harder on various blogs everyday. Most commonly it is stimulating to study content from other writers and exercise a specific thing from their website. I’d opt to apply certain while using the content in this little blog whether or not you do not mind. Natually I’ll provide a link on your own internet weblog. Appreciate your sharing. I am curious to find out what blog system you are working with? I’m experiencing some small security issues with my latest website and I would like to find something more secure. 

먹튀신고 说:
Dec 19, 2023 12:46:45 AM

Thank you for sharing a bunch of this quality contents, I have bookmarked your blog. Please also explore advice from my site. I will be back for more quality contents.

에볼루션파워볼배팅 说:
Dec 19, 2023 01:00:14 AM

Nice post. I learn some thing tougher on different blogs everyday. Most commonly it is stimulating to read content off their writers and practice something at their store. I’d opt to use some with all the content in my weblog whether or not you do not mind. Natually I’ll provide a link with your internet weblog. Thank you for sharing 

엠카지노 说:
Dec 19, 2023 01:10:26 AM

I am impressed by the information that you have on this blog. It shows how well you understand this subject. wow... what a great blog, this writter who wrote this article it's realy a great blogger, this article so inspiring me to be a better person . Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing. Cool stuff you have and you keep overhaul every one of us 

메이저사이트 说:
Dec 19, 2023 01:23:23 AM

Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing.

바카라 说:
Dec 19, 2023 01:38:38 AM

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well. Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers.

사설토토 说:
Dec 19, 2023 01:56:07 AM

Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing.

먹튀폴리스역사 说:
Dec 19, 2023 02:05:22 AM

Its opportunity are so fantastic and working style so speedy. I truly like you're composing style, incredible data, thankyou for posting. I see the greatest contents on your blog and I extremely love reading them.

먹튀검증업체 说:
Dec 19, 2023 02:22:54 AM

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. This article gives the light in which we can observe the reality. I am happy to find this post very useful for me, as it contains a lot of information. Thanks for all your help and wishing you all the success in your categories. Thanks for sharing the content for any sort of online business consultation is the best place to find in the town. 

먹튀스팟주소 说:
Dec 19, 2023 02:33:51 AM

hey there i stumbled upon your website searching around the web. I wanted to say I like the look of things around here. Keep it up will bookmark for sure. Cool you write, the information is very good and interesting, I'll give you a link to my site. you have got a great blog here! do you want to make some invite posts in my blog. Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info.

라이브스코어7M 说:
Dec 19, 2023 02:48:11 AM

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too.I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site. 

토토사이트추천 说:
Jan 20, 2024 07:49:51 PM

I have read your excellent post. This is a great job. I have enjoyed reading your post first time. I want to say thanks for this post. Thank you...

바카라사이트 说:
Jan 20, 2024 10:08:16 PM

Hello. splendid job. I did not imagine this. This is a impressive articles. Thanks!

카지노 커뮤니티 说:
Jan 20, 2024 10:30:47 PM

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post

먹튀검증 说:
Jan 20, 2024 10:59:55 PM

Easy option to get useful information as well as share good stuff with good ideas and concepts

바카라 사이트 说:
Jan 20, 2024 11:49:15 PM

This is a great feature for sharing this informative message. I am impressed by the knowledge you have on this blog. It helps me in many ways. Thanks for posting this again

카지노 커뮤니티 说:
Jan 21, 2024 12:06:24 AM

awesome article. Thank you for sharing the post with your tribe

꽁머니 说:
Jan 21, 2024 12:41:16 AM

I very glad to find this site on bing, just what I was looking for : D as well saved to favorites .

먹튀검증 说:
Jan 21, 2024 01:32:00 AM

Impressive work on the essayist's part.

카지노사이트추천 说:
Jan 21, 2024 01:53:16 AM

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!

industrial outdoor s 说:
Jan 21, 2024 06:29:58 PM

I really appreciate your effort in helping people get the information they need

카지노커뮤니티 说:
Jan 21, 2024 07:15:43 PM

I learn some new stuff from it too, thanks for sharing your information.

소액결제현금화 说:
Jan 21, 2024 07:44:50 PM

I really appreciate your effort in helping people get the information they need

고화질스포츠중계 说:
Jan 21, 2024 08:19:32 PM

Thank you very much for this useful article. I like it

카지노사이트 说:
Jan 21, 2024 08:58:28 PM

Appreciating the time and energy you put into your website and detailed information you present. It’s great to come across a blog every once in a while that isn’t the same outdated rehashed information. Excellent read!

카지노사이트 说:
Jan 23, 2024 09:47:05 PM

Hello I want to share good information. Get good information. I will get good information. Everyone will have a hard time due to the corona, but please do your best. I hope that the corona will disappear soon. It would be hard for everyone, but I hope that the more I will endure and get good results. Thank you

카지노 说:
Jan 23, 2024 11:22:38 PM

As i followed on your web log even while aligning really just a little submits. Attractive technique for then, We are book-marking at one time seize ones own whole goes up.

토토검증사이트 说:
Jan 24, 2024 12:58:06 AM

The goal of the society is to create professionally well skilled students To achieve the said goal, arrangements have been made with the pioneers and front runners both in India and abroad.

바카라 说:
Jan 25, 2024 07:57:52 PM

바카라 바카라사이트 우리카지노 카지노는 바카라, 블랙잭, 룰렛 및 슬롯 등 다양한 게임을 즐기실 수 있는 공간입니다. 게임에서 승리하면 큰 환호와 함께 많은 당첨금을 받을 수 있고, 패배하면 아쉬움과 실망을 느끼게 됩니다.

하노이 가라오케 说:
Jan 25, 2024 08:00:01 PM

하노이 꼭 가봐야 할 베스트 업소 추천 안내 및 예약, 하노이 밤문화 에 대해서 정리해 드립니다. 하노이 가라오케, 하노이 마사지, 하노이 풍선바, 하노이 밤문화를 제대로 즐기시기 바랍니다. 하노이 밤문화 베스트 업소 요약 베스트 업소 추천 및 정리.

안전놀이터 说:
Jan 28, 2024 07:11:43 PM

No.1 먹튀검증 사이트, 먹튀사이트, 검증사이트, 토토사이트, 안전사이트, 메이저사이트, 안전놀이터 정보를 제공하고 있습니다. 먹튀해방으로 여러분들의 자산을 지켜 드리겠습니다. 먹튀검증 전문 커뮤니티 먹튀클린만 믿으세요!!

베트남 밤문화 说:
Jan 28, 2024 07:17:33 PM

베트남 남성전용 커뮤니티❣️ 베트남 하이에나 에서 베트남 밤문화를 추천하여 드립니다. 베트남 가라오케, 베트남 VIP마사지, 베트남 이발관, 베트남 황제투어 남자라면 꼭 한번은 경험 해 봐야할 화끈한 밤문화로 모시겠습니다.

IT technology 说:
Jun 02, 2024 09:34:45 PM

The tech-original mission is to help you better understand technology, and make better decisions in the fields of IT, Tech, and Crypto.
https://www.tech-original.com/

IT technology 说:
Jun 07, 2024 04:52:41 PM

The tech-original mission is to help you better understand technology, and make better decisions in the fields of IT, Tech, and Crypto.
https://www.tech-original.com/


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter