sicp每日一题[2.22-2.23]

37 篇文章 0 订阅
23 篇文章 0 订阅

Exercise2.22

Louis Reasoner tries to rewrite the first s q u a r e − l i s t square-list squarelist procedure of Exercise 2.21 so that it evolves an iterative process:

(define (square-list items)
  (define (iter things answer)
    (if (null? things)
        answer
        (iter (cdr things)
              (cons (square (car things))
                    answer))))
  (iter items nil))

Unfortunately, defining s q u a r e − l i s t square-list squarelist this way produces the answer list in the reverse order of the one desired. Why?
Louis then tries to fix his bug by interchanging the arguments to c o n s cons cons:

(define (square-list items)
  (define (iter things answer)
    (if (null? things)
        answer
        (iter (cdr things)
              (cons answer
                    (square (car things))))))
  (iter items nil))

This doesn’t work either. Explain.


第一部分的代码得到的答案是倒序的,这个是因为程序里每次都把新加入的元素放到结果列表的开头;第二部分得到的答案是这样的:'(((((() . 1) . 9) . 25) . 49) . 81),虽然顺序对了,这并不是我们想要的列表,其实只要在调用第一部分代码后再次调用之前练习里的 reverse 函数就可以得到顺序正确的列表了。

Exercise2.23

The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. How ever, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all— f o r − e a c h for-each foreach is used with procedures that perform an action, such as printing. For example,

(for-each(lambda(x)
           (newline)
           (display x))
         (list5732188))

57
321
88

The value returned by the call to f o r − e a c h for-each foreach (not illustrated above) can be something arbitrary, such as true. Give an implementation of f o r − e a c h for-each foreach.


这道题开始我不知道怎么连续执行 2 条程序,所以不知道怎么处理,后来上网查了一下,发现 c o n d cond cond 可以直接把多条语句放到一起执行,然后这道题就没有什么难度了。

(define (for-each f items)
  (cond ((not (null? items))
         (f (car items))
         (for-each f (cdr items)))))


(define test (list 57 321 88))
(for-each (lambda(x) (newline) (display x))
          test)

(newline)

(for-each (lambda(x) (newline) (display (square x)))
          test)

; 执行结果
57
321
88

3249
103041
7744
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值