2.22
(define (for-each2 proc items)
(proc (car items))
(if (null? (cdr items))
(newline)
(for-each2 proc (cdr items))
)
)
2.21
(define (square-list3 items)
(define (iter things answer)
(if (null? things)
answer
(iter (cdr things)
( append answer (list(square (car things)))
)
))
)
(iter items ())
)
2.20
(define (square-list1 items)
(if (null? items)
()
(cons (* (car items) (car items) ) (square-list1 (cdr items)))
)
)
(define (square-list2 items)
(map (lambda (x) (* x x)) items )
)
2.19
(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))
(define (cc amount coin-values)
(cond ((= amount 0) 1)
((or (< amount 0) (null? coin-values)) 0)
(else
(+ (cc amount
(except-first-denomination coin-values))
(cc (- amount
(first-denomination coin-values))
coin-values
)
))
)
)
(define (first-denomination items)
(car items)
)
(define (except-first-denomination items)
(cdr items)
)
(cc 100 us-coins)
SICP Exercise 2.19-
Scheme编程示例
最新推荐文章于 2024-03-15 09:41:07 发布
本文介绍了使用Scheme语言实现的几个编程示例,包括列表操作函数如square-list的不同实现方式,以及递归函数for-each2的应用。此外,还展示了如何通过递归解决计数问题,并给出了具体的实现代码。
2921

被折叠的 条评论
为什么被折叠?



