首页 > 其他 > 详细

SICP_2.21-2.23

时间:2017-02-13 23:31:14      阅读:300      评论:0      收藏:0      [点我收藏+]
 1 #lang racket
 2 
 3 ;;;;;;;;;;;;;;;;;;;2.21
 4 (define (square-list items)
 5   (if (null? items)
 6       null
 7       (cons (square (car items))
 8             (square-list (cdr items)))))
 9 
10 (define (square x)
11   (* x x))
12 ;;
13 (define (square-list2 items)
14   (map square items))
15 
16 ;;;;test
17 (square-list (list 1 2 3 4))
18 (square-list2 (list 1 2 3 4))
19 
20 ;;;;;;;;;;;;;;;;;;;;2.22
21 (define (square-list3 items)
22   (define (iter things answer)
23     (if (null? things)
24         answer
25         (iter (cdr things)
26               (append answer
27                       (list (square (car things)))))))
28   (iter items ()))
29 
30 ;;test
31 (square-list3 (list 1 2 3 4))
32 
33 ;;;;;;;;;;;;;;;;;;;;2.23
34 (define (for-each proc items)
35   (cond ((not (null? items))
36          (proc (car items))
37          (for-each proc (cdr items)))))
38 
39 ;;test
40 (for-each (lambda (x) (newline) (display x)) (list 57 321 88))

 

SICP_2.21-2.23

原文:http://www.cnblogs.com/tclan126/p/6395556.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!