-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex2.54-implement-equal.scm
102 lines (89 loc) · 2.56 KB
/
ex2.54-implement-equal.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(define (equal? l1 l2)
(print l1)
(print l2)
(print "")
(cond ((and (null? l1) (null? l2)) #t)
((or (null? l1) (null? l2)) #f)
(else (let ((l1-car (car l1))
(l1-cdr (cdr l1))
(l2-car (car l2))
(l2-cdr (cdr l2)))
(cond ((and (pair? l1-car) (pair? l2-car))
(if (equal? l1-car l2-car) (equal? l1-cdr l2-cdr) #f))
((and (not (pair? l1-car)) (not (pair? l2-car)))
(if (eq? l1-car l2-car) (equal? l1-cdr l2-cdr) #f))
(else #f))))))
; ; (equal? '(this is a list) '(this is a list))
; (equal? '(this is (a) list) '(this is a list))
; ; (equal? '(this is a list) '(this (is a) list))
; (equal? '(1 2 3 (4 5) 6) '(1 2 3 (4 5 7) 6))
; simplify towards nonofficial solution:
; ->
(define (equal? l1 l2)
(print l1)
(print l2)
(print "")
(cond ((and (null? l1) (null? l2)) #t)
((or (null? l1) (null? l2)) #f)
((and (not (pair? list1)) (not (pair? list2)))
(eq? list1 list2))
(else (let ((l1-car (car l1))
(l1-cdr (cdr l1))
(l2-car (car l2))
(l2-cdr (cdr l2)))
(cond ((and (pair? l1-car) (pair? l2-car))
(if (equal? l1-car l2-car) (equal? l1-cdr l2-cdr) #f))
((and (not (pair? l1-car)) (not (pair? l2-car)))
(if (equal? l1-car l2-car) (equal? l1-cdr l2-cdr) #f))
(else #f))))))
; ->
(define (equal? l1 l2)
(print l1)
(print l2)
(print "")
(cond ((and (null? l1) (null? l2)) #t)
((or (null? l1) (null? l2)) #f)
((and (not (pair? list1)) (not (pair? list2)))
(eq? list1 list2))
((and (pair? list1) (pair? list2))
(if (equal? (car l1) (car l2)) (equal? (cdr l1) (cdr l2)) #f))
(else #f)
))
; ->
(define (equal? l1 l2)
(print l1)
(print l2)
(print "")
(cond ((and (null? l1) (null? l2)) #t)
((or (null? l1) (null? l2)) #f)
((and (not (pair? list1)) (not (pair? list2)))
(eq? list1 list2))
((and (pair? list1) (pair? list2))
(and (equal? (car l1) (car l2)) (equal? (cdr l1) (cdr l2))))
(else #f)
))
; since (pair? '())
; => #f
; (eq? '() '())
; => true
(define (equal? l1 l2)
(print l1)
(print l2)
(print "")
(cond ((and (not (pair? list1)) (not (pair? list2)))
(eq? list1 list2))
((and (pair? list1) (pair? list2))
(and (equal? (car l1) (car l2)) (equal? (cdr l1) (cdr l2))))
(else #f)
))
(define (equal? list1 list2)
(print "")
(print list1)
(print list2)
(cond ((and (not (pair? list1)) (not (pair? list2)))
(eq? list1 list2))
((and (pair? list1) (pair? list2))
(and (equal? (car list1) (car list2)) (equal? (cdr list1) (cdr list2))))
(else #f)))
; (equal? '(1 2 3 (4 5) 6) '(1 2 3 (4 5) 6))
(equal? '(1) '(1 2))