-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.16.rkt
51 lines (43 loc) · 820 Bytes
/
3.16.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
#lang scheme
; set-cdr! required
(require r5rs)
; Ben's method:
(define (count-pair x)
(if (not (pair? x))
0
(+ (count-pair (car x))
(count-pair (cdr x))
1)))
; test case
; output 3
; [1]->[2]->[3]
(count-pair (list 1 2 3)) ; 3
; output 4
; [2]->[ ]->[1]
; |____↑
(define a (cons 1 null)) ; 1
(define b (cons a a)) ; 1
(define c (cons 2 b)) ; 1
(count-pair c)
; output 5
; [ ]->[1]->[2]
; |____↑
(define d (list 1 2)) ; 2
(define e (cons d d)) ; 1
(count-pair e)
; output 7
; |――――↓
; [ ]->[ ]->[1]
; |____↑
(define f (cons b b)) ; 1 (and a is 1, b is 1)
(count-pair f)
; loop
; [1]->[2]
; ↑ |
; ―[3]<-
(define t1 (cons 3 null))
(define t2 (cons 2 t1))
(define t3 (cons 1 t2))
(set-cdr! t1 t3)
(define loop t3)
(count-pair loop) ; death loop