-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcount-columns.scm
38 lines (30 loc) · 933 Bytes
/
count-columns.scm
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
(load "./transpose.scm")
(define (every? p l)
(or (null? l)
(and (p (car l))
(every? p (cdr l)))))
(define (any? p l)
(and (not (null? l))
(or (p (car l))
(any? p (cdr l)))))
(define (count-columns matrix)
(define (subset? column row)
(every? (lambda (x)
(member x row))
column))
(define (subset-of-row? column)
(any? (lambda (row)
(subset? column row))
matrix))
(length (filter subset-of-row?
(transpose matrix))))
(load "../testing/check.scm")
(check (count-columns '((1))) => 1)
(check (count-columns '((1 2))) => 2)
(check (count-columns '((1) (2))) => 0)
(check (count-columns '((1 3 5 7) (2 5 3 4))) => 2)
(check (count-columns '((1 3 5 7) (2 5 3 7))) => 3)
(check (count-columns '((1 4 3) (4 5 6) (7 4 9))) => 1)
(check (count-columns '((1 4 3) (4 5 6) (7 3 9))) => 0)
(check-report)
(check-reset!)