-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 2.54 equal?.rkt
69 lines (60 loc) · 1.69 KB
/
Exercise 2.54 equal?.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
68
69
#lang racket
; Exercise 2.54. Two lists are said to be equal? if they contain equal
; elements arranged in the same order. For example,
; (equal? '(this is a list) '(this is a list))
; is true, but
; (equal? '(this is a list) '(this (is a) list))
; is false. To be more precise, we can define equal? recursively in terms of
; the basic eq? equality of symbols by saying that a and b are equal? if they
; are both symbols and the symbols are eq?, or if they are both lists such that
; (car a) is equal? to (car b) and (cdr a) is equal? to (cdr b). Using this
; idea, implement equal? as a procedure.
; SOLUTION
(define (equal? list1 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
false
)
)
)
; Tests
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 128 MB.
> (equal? '(this is a list) '(this is a list))
#t
> (equal? '(this is a list) '(this (is a) list))
#f
> (equal? 'king 'queen)
#f
> (equal? 'king 'king)
#t
> (equal? 26 25)
#f
> (equal? 26 26)
#t
> (equal? '(this is a longer list) '(this is another short list))
#f
> (equal? '(this is a longer list than the next one) '(this is another short list))
#f
> (equal? '(this is a longer list than the next one) 'word)
#f
> (equal? '(this is a longer list than the next one) '(this is a longer list than the next one))
#t
> (equal? 'football '(football volleyball))
#f
> (equal? '() '())
#t
> (equal? '(first) '(next))
#f
> (equal? '(first) '(first))
#t
> (equal? '(first) '((first)))
#f
>