-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathex-175.rkt
69 lines (55 loc) · 1.47 KB
/
ex-175.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
#lang htdp/bsl
(require 2htdp/batch-io)
; ### Constants
; ### Data Definitions
(define-struct stats [c w l])
; Statistic is a structure (make-statistic Number Number Number)
; ### Functions
; String -> Statistic
(define (wc n)
(extract-stats (read-words/line n))
)
; List-of-list-of-string -> Statistic
; Reads a text in form of list of lists and returns its statistics
(check-expect (extract-stats '()) (make-stats 0 0 0))
(check-expect
(extract-stats (list (list "hello" "there") (list "how" "you" "doing?")))
(make-stats 22 5 2)
)
(define (extract-stats lls)
(cond
[(empty? lls) (make-stats 0 0 0)]
[else
(sum-stats
(make-stats
(line-chars (first lls)) ; white spaces between words ignored...
(length (first lls))
1
)
(extract-stats (rest lls))
)]))
; List-of-strings -> Number
; Sums the lengths of all the strings
(check-expect (line-chars (list "eeeyyy" "macarena" "aaaaamm")) 21)
(define (line-chars los)
(cond
[(empty? los) 0]
[else
(+
(string-length (first los))
(line-chars (rest los))
)]))
; Statistic Statistic -> Statistic
; Sums a Statistic to another Statistic
(check-expect
(sum-stats (make-stats 1 2 3) (make-stats 3 0 5))
(make-stats 4 2 8)
)
(define (sum-stats a b)
(make-stats
(+ (stats-c a) (stats-c b))
(+ (stats-w a) (stats-w b))
(+ (stats-l a) (stats-l b))
))
(require test-engine/racket-tests)
(test)