Chapter 2, Building Abstractions with Data

Section - 2.3 Symbolic Data

Exercise 2.54


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#lang sicp

(define (equal? x y)
   (cond (
          (null? x) (null? y))
          ((null? y) false)
          ((pair? x)
                 (if (pair? y)                         
                        (and (equal? (car x) (car y)) (equal? (cdr x) (cdr y)))
                        false
                 )
          )
          ((pair? y) false)
          (else (eq? x y))
   )
)  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> (equal? '1 '2)
#f
> (equal? '1 '1)
#t
> (equal? '(1 2 3) '(1 2 5))
#f
>  (equal? '(1 2 3) '(1 2 3))
#t
>  (equal? '(1 (12 13)) '(1 12 13))
#f
>  (equal? '(1 (12 13)) '(1 (12 13)))
#t
>  (equal? '(1 (12 13)) '(1 (13 12)))
#f
> (equal? '() nil)
#t
> (equal? '() '1)
#f
>