Blame


1 665c255d 2023-08-04 jrmu ;; Devise a correct version of the count-pairs procedure of exercise 3.16 that returns the number of distinct pairs in any structure. (Hint: Traverse the structure, maintaining an auxiliary data structure that is used to keep track of which pairs have already been counted.)
2 665c255d 2023-08-04 jrmu
3 665c255d 2023-08-04 jrmu (define (test-case actual expected)
4 665c255d 2023-08-04 jrmu (newline)
5 665c255d 2023-08-04 jrmu (display "Actual: ")
6 665c255d 2023-08-04 jrmu (display actual)
7 665c255d 2023-08-04 jrmu (newline)
8 665c255d 2023-08-04 jrmu (display "Expected: ")
9 665c255d 2023-08-04 jrmu (display expected)
10 665c255d 2023-08-04 jrmu (newline))
11 665c255d 2023-08-04 jrmu
12 665c255d 2023-08-04 jrmu (define (count-pairs x)
13 665c255d 2023-08-04 jrmu (if (not (pair? x))
14 665c255d 2023-08-04 jrmu 0
15 665c255d 2023-08-04 jrmu (+ (count-pairs (car x))
16 665c255d 2023-08-04 jrmu (count-pairs (cdr x))
17 665c255d 2023-08-04 jrmu 1)))
18 665c255d 2023-08-04 jrmu
19 665c255d 2023-08-04 jrmu (define three '(a b c))
20 665c255d 2023-08-04 jrmu (define four )
21 665c255d 2023-08-04 jrmu (define seven )
22 665c255d 2023-08-04 jrmu (define circular )
23 665c255d 2023-08-04 jrmu