-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtail_recursion.rkt
52 lines (41 loc) · 1.39 KB
/
tail_recursion.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#lang racket
; Zilu Tian
; Lecture notes for SICP
;Recursive procedure can have iterative process or recursive process
;
;For languages where interpretation of any recursive procedures consumes
;an amount of memory that grows with the number of procedure calls,
;iterative processes have special loop constructs
;
;Tail-recursive: An implementation such that any iterative process consumes
;constant space, even if described by a recursive procedure
; Tree recursion
; Counting change
;
; How many different ways can we make change of 1,
; given 0.5, 0.25, 0.1, 0.05, and 0.01?
(define count-change
(λ (amount)
(define cc
(λ (amount coin-type)
(cond [(= amount 0) 1]
[(or (< amount 0) (= coin-type 0)) 0]
[else (+ (cc amount (- coin-type 1))
(cc (- amount (coin-value coin-type)) coin-type))])))
(define coin-value
(λ (coin-type)
(cond [(= coin-type 1) 50]
[(= coin-type 2) 25]
[(= coin-type 3) 10]
[(= coin-type 4) 5]
[(= coin-type 5) 1])))
(cc amount 5)))
;; floating point numbers suffer from precision
;(define coin-value
; (λ (coin-type)
; (cond [(= coin-type 1) 0.5]
; [(= coin-type 2) 0.25]
; [(= coin-type 3) 0.1]
; [(= coin-type 4) 0.05]
; [(= coin-type 5) 0.01])))
(count-change 100)