15 hours ago

my attempt to do the exercises in sicp.

## Sunday, July 20, 2008

### sicp exercise 2.54

;; 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.

(define (equal? x y)

(cond ((and (not (pair? x))

(not (pair? y))) (eq? x y))

((and (pair? x) (pair? y))

(and (eq? (car x) (car y))

(equal? (cdr x) (cdr y))))

(else #f)))

(display (equal? '(a b c) '(a b c)))

(newline)

(display (equal? 'a 'a))

(newline)

(display (equal? '(a b c) '(a b f)))

(newline)

(display (equal? 'a '(a b f)))

(newline)

Subscribe to:
Post Comments (Atom)

## No comments:

Post a Comment