免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 16545 | 回复: 73
打印 上一主题 下一主题

SICP 习题参考答案 2.2  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-22 20:26 |只看该作者 |倒序浏览
此帖为 SICP 章节 2.2 的参考答案。为保持可读性,请勿直接在本帖讨论。讨论请另开新帖。


论坛徽章:
0
2 [报告]
发表于 2008-09-22 20:27 |只看该作者

Exercise 2.17

Exercise 2.17.  Define a procedure last-pair that returns the list that contains only the last element of a given (nonempty) list:

(last-pair (list 23 72 149 34))
(34)

论坛徽章:
0
3 [报告]
发表于 2008-09-22 20:28 |只看该作者

Exercise 2.17. 答案


  1. (define (last-pair L)
  2.   (if (null? (cdr L)) (car L)
  3.       (last-pair (cdr L))))
复制代码

论坛徽章:
0
4 [报告]
发表于 2008-09-22 20:29 |只看该作者

Exercise 2.18.

Exercise 2.18.  Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order:

(reverse (list 1 4 9 16 25))
(25 16 9 4 1)

论坛徽章:
0
5 [报告]
发表于 2008-09-22 20:30 |只看该作者

Exercise 2.18. 答案


  1. (define (Reverse L)
  2.   (define (f L R)
  3.     (if (null? L) R
  4.         (f (cdr L) (cons (car L) R))))
  5.   (f L '()))
复制代码

论坛徽章:
0
6 [报告]
发表于 2008-09-22 20:31 |只看该作者

Exercise 2.19.

Exercise 2.19.  Consider the change-counting program of section 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change (which knows that there are five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change.

We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency:

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

We could then call cc as follows:

(cc 100 us-coins)
292

To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:

(define (cc amount coin-values)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (no-more? coin-values)) 0)
        (else
         (+ (cc amount
                (except-first-denomination coin-values))
            (cc (- amount
                   (first-denomination coin-values))
                coin-values)))))

Define the procedures first-denomination, except-first-denomination, and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not?

论坛徽章:
0
7 [报告]
发表于 2008-09-22 20:32 |只看该作者

Exerecise 2.19. 答案


  1. (define (first-denomination x)
  2.   (car x))

  3. (define (except-first-denomination x)
  4.   (cdr x))

  5. (define (no-more? x)
  6.   (null? x))
复制代码


不影响

论坛徽章:
0
8 [报告]
发表于 2008-09-22 20:33 |只看该作者

Exercise 2.20.

Exercise 2.20.  The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation. In a procedure definition, a parameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have as values the initial arguments, as usual, but the final parameter's value will be a list of any remaining arguments. For instance, given the definition

(define (f x y . z) <body>)

the procedure f can be called with two or more arguments. If we evaluate

(f 1 2 3 4 5 6)

then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition

(define (g . w) <body>)

the procedure g can be called with zero or more arguments. If we evaluate

(g 1 2 3 4 5 6)

then in the body of g, w will be the list (1 2 3 4 5 6).11

Use this notation to write a procedure same-parity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example,

(same-parity 1 2 3 4 5 6 7)
(1 3 5 7)

(same-parity 2 3 4 5 6 7)
(2 4 6)

论坛徽章:
0
9 [报告]
发表于 2008-09-22 20:33 |只看该作者

Exerecise 2.20. 答案


  1. (define (same-parity x . y)
  2.   (let ((p (if (even? x) even? odd?)))
  3.     (define (f y)
  4.       (if (null? y) y
  5.           (if (p (car y)) (cons (car y) (f (cdr y)))
  6.                 (f (cdr y)))))
  7.     (f y)))
复制代码

论坛徽章:
0
10 [报告]
发表于 2008-09-24 12:34 |只看该作者

Exercise 2.21.

Exercise 2.21.  The procedure square-list takes a list of numbers as argument and returns a list of the squares of those numbers.

(square-list (list 1 2 3 4))
(1 4 9 16)

Here are two different definitions of square-list. Complete both of them by filling in the missing expressions:

(define (square-list items)
  (if (null? items)
      nil
      (cons <??> <??>)))
(define (square-list items)
  (map <??> <??>))

[ 本帖最后由 win_hate 于 2008-9-24 12:36 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP