Blame


1 665c255d 2023-08-04 jrmu (define (test-case actual expected)
2 665c255d 2023-08-04 jrmu (newline)
3 665c255d 2023-08-04 jrmu (display "Actual: ")
4 665c255d 2023-08-04 jrmu (display actual)
5 665c255d 2023-08-04 jrmu (newline)
6 665c255d 2023-08-04 jrmu (display "Expected: ")
7 665c255d 2023-08-04 jrmu (display expected)
8 665c255d 2023-08-04 jrmu (newline))
9 665c255d 2023-08-04 jrmu
10 665c255d 2023-08-04 jrmu (define (memq item x)
11 665c255d 2023-08-04 jrmu (cond ((null? x) false)
12 665c255d 2023-08-04 jrmu ((eq? item (car x)) x)
13 665c255d 2023-08-04 jrmu (else (memq item (cdr x)))))
14 665c255d 2023-08-04 jrmu (test-case (list 'a 'b 'c) '(a b c))
15 665c255d 2023-08-04 jrmu (test-case (list (list 'george)) '((george)))
16 665c255d 2023-08-04 jrmu (test-case (cdr '((x1 x2) (y1 y2))) '((y1 y2)))
17 665c255d 2023-08-04 jrmu (test-case (cadr '((x1 x2) (y1 y2))) '(y1 y2))
18 665c255d 2023-08-04 jrmu (test-case (pair? (car '(a short list))) #f)
19 665c255d 2023-08-04 jrmu (test-case (memq 'red '((red shoes) (blue socks))) #f)
20 665c255d 2023-08-04 jrmu (test-case (memq 'red '(red shoes blue socks)) '(red shoes blue socks))
21 665c255d 2023-08-04 jrmu
22 665c255d 2023-08-04 jrmu Exercise 2.54. Two lists are said to be equal? if they contain equal elements arranged in the same order. For example,
23 665c255d 2023-08-04 jrmu
24 665c255d 2023-08-04 jrmu (equal? '(this is a list) '(this is a list))
25 665c255d 2023-08-04 jrmu
26 665c255d 2023-08-04 jrmu is true, but
27 665c255d 2023-08-04 jrmu
28 665c255d 2023-08-04 jrmu (equal? '(this is a list) '(this (is a) list))
29 665c255d 2023-08-04 jrmu
30 665c255d 2023-08-04 jrmu 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.36