blob: 2cf35056c09075e9c4d771826cde5ff2cf208ff0 (
plain) (
blame)
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
53
54
55
56
57
58
59
60
|
; first version -- no data abstraction
(define (total-hand hand)
(if (empty? hand)
0
(+ (butlast (last hand))
(total-hand (butlast hand)) )))
(total-hand '(3h 10c 4d))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; second version -- add selectors
(define (total-hand hand)
(if (empty? hand)
0
(+ (card-rank (one-card hand))
(total-hand (remaining-cards hand)) )))
(define card-rank butlast)
(define card-suit last)
(define one-card last)
(define remaining-cards butlast)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; third version -- add constructors
(define (make-card rank suit)
(word rank (first suit)) )
(define make-hand se)
(total-hand (make-hand (make-card 3 'heart)
(make-card 10 'club)
(make-card 4 'diamond) ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; fourth version -- change implementation
(define (make-card rank suit)
(cond ((equal? suit 'heart) rank)
((equal? suit 'spade) (+ rank 13))
((equal? suit 'diamond) (+ rank 26))
((equal? suit 'club) (+ rank 39))
(else (error "say what?")) ))
(define (card-rank card)
(remainder card 13))
(define (card-suit card)
(nth (quotient card 13) '(heart spade diamond club)))
|