-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathex-234.rkt
86 lines (74 loc) · 1.87 KB
/
ex-234.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#lang htdp/bsl+
(require test-engine/racket-tests)
; ### Data Definitions
; Song is a String
; LOS is one of:
; - '()
; - (cons Song LOS)
; ### Constants
(define one-list
'("Asia: Heat of the Moment"
"U2: One"
"The White Stripes: Seven Nation Army"
))
; ### Functions
; List-of-songs -> List-of-rank-songs
; Adds ranks to a list of songs. First song gets rank #1
(define (ranking los)
(reverse (add-ranks (reverse los)))
)
; List-of-songs -> List-of-rank-songs
; Adds ranks to a list of songs. Last songs gets rank #1
(define (add-ranks los)
(cond
[(empty? los) '()]
[else
(cons
(list
; NOTE: original exercise does not convert to string
(number->string (length los))
(first los))
(add-ranks (rest los))
)]))
(define (make-ranking lors)
`(table ,@(make-rows lors))
)
; List-of-ranking-songs -> List-of-trs
(check-expect
(make-rows
'(("1" "Nested Coil: I like when you talk to that potato")
("2" "Abba: Kill 'em with fire")
))
'((tr (td "1") (td "Nested Coil: I like when you talk to that potato"))
(tr (td "2") (td "Abba: Kill 'em with fire"))
))
(check-expect
(make-rows (ranking one-list))
'((tr (td "1") (td "Asia: Heat of the Moment"))
(tr (td "2") (td "U2: One"))
(tr (td "3") (td "The White Stripes: Seven Nation Army"))
))
(define (make-rows lors)
(cond
[(empty? lors) '()]
[else
(cons
`(tr ,@(make-cells (first lors)))
; or just:
; (cons 'tr (make-cells (first lors)))
(make-rows (rest lors))
)]))
; List-of-strings -> List-of-tds
(check-expect
(make-cells '("2" "Johnny: the lazy bird"))
'((td "2") (td "Johnny: the lazy bird"))
)
(define (make-cells los)
(cond
[(empty? los) '()]
[else
(cons
(list 'td (first los))
(make-cells (rest los))
)]))
(test)