###### 2.2 层次闭包性质 练习 2.17 2.20
#lang racket

(define a (list 1 2 3 4))

(define (list-ref items n)
(if (= n 0)
(car items)
(list-ref (cdr items) (- n 1))))

#|
(define (length items)
(if (null? items)
0
(+ 1 (length (cdr items)))))
|#

(define (length items)
(define (iter items len)
(if (null? items)
len
(iter (cdr items ) (+ 1 len))))
(iter items 0))

;: (length a

;: 练习 2.17
;: 如果 items 本来为null？ 应该报错
(define (last-pair items)
(if (null? (cdr items))
(car items)
(last-pair (cdr items))))

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

;: 练习 2.18
(define (reverse items)
(if (null? items)
'()
(append (reverse (cdr items))
(list (car items)))))

;: 测试
(reverse (list 1 2 3 4))

;: 练习 2.19
;:

#| 原来的方式
(define (count-change amount)
(cc amount 5))

(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0)
(= kinds-of-coins 0))
0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))

(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))

(count-change 100) => 292
|#

(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) (no-more? coin-values)) 0)
(else
(+ (cc amount
(except-first-denomination coin-values))
(cc (- amount
(first-denomination coin-values))
coin-values)))))

(define (no-more? coin-values)
(null? coin-values))
(define (except-first-denomination coin-vlaues)
(cdr coin-vlaues))
(define (first-denomination coin-values)
(car coin-values))

(cc 100 us-coins)
(display "*****************************")
(newline)

;: 练习 2.20
(define (filter check? items)
(cond ((null? items) '())
((check? (car items))
(cons (car items)
(filter check? (cdr items))))
(else
(filter check? (cdr items)))))

;:::(filter odd? (list 1 2 3 4 5 6 7 8 9))

(define (same-parity first . items)
(let ((check? (if (odd? first) odd? even?)))
(cons first (filter check? items))))
;: 测试
(same-parity 1 2 3 4 5 6 7)
(same-parity   2 3 4 5 6 7)



#### 严蔚敏版数据结构课本代码——算法2.2

2017-10-22 15:50:33

#### js 匿名函数和闭包函数（js练习）

2016-03-27 19:22:59

#### Linux（ubuntu13.04版本）下编译源…

2017-07-28 23:10:50

#### 2.2层次性数据和闭包性质

2017-11-10 16:39:40

#### Go指南练习之《斐波纳契闭包》

2017-08-20 11:11:52

#### valgrind安装失败解决--configure: error: Valgrind requires glibc version 2.2 - 2.14

2017-03-18 17:27:46

#### valgrind 安装

2012-12-14 11:26:50

#### 红烛教鞭 (e-Pointer)V2.2 绿色特别版

2010年01月05日 36KB 下载

#### Go指南练习之《斐波纳契闭包》(Fibonacci closure)

2017-01-16 09:47:00

#### C++ Primer 第5版--练习10.21

2017-03-22 22:10:35

## 不良信息举报

2.2 层次闭包性质 练习 2.17 2.20