-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex2-53-symbolic-data.scm
67 lines (51 loc) · 1.85 KB
/
ex2-53-symbolic-data.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
;; Ch 2.3 Symbolic Data
(define (memq item x)
(cond ((null? x ) false)
((eq? item (car x)) x)
(else (memq item (cdr x)))))
;; Ex. 2.53 Predicted evaluation results:
;; (list 'a 'b 'c) --> (a b c)
;; (list (list 'george)) --> ((george))
;; (cdr '((x1 x2) (y1 y2))) --> ((y1 y2))
;; (cadr '((x1 x2) (y1 y2))) --> (y1 y2)
;; (pair? (car '(a short list))) --> false
;; (memq 'red '((red shoes) (blue socks))) --> false
;; (memq 'red '(red shoes blue socks)) --> (red shoes blue socks)
;; Ex. 2.54 implement equal?
;;
;; 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).
(define (myequal? xs ys)
(define (myeq? xs ys r)
(cond
((or (null? xs) (null? ys))
r)
((and (pair? (car xs)) (pair? (car ys)))
(myeq? (cdr xs) (cdr ys) (myeq? (car xs) (car ys) r)))
(else
(myeq? (cdr xs) (cdr ys) (eq? (car xs) (car ys))))))
(cond
((not (and (pair? xs) (pair? ys)))
(eq? xs ys))
((not (length=? xs ys))
false)
(else
(myeq? xs ys true))))
;; Test:
;;
;; (let* ((l1 (list 1 2 (list 3 4)))
;; (l2 (list "a" "b" 'c 'd))
;; (l3 '((1 2) ('a 'b) ("c" 'd) 42))
;; (all-lists (list l1 l2 l3)))
;; (list 'myequal? (map myequal? all-lists all-lists)
;; 'equal? (map equal? all-lists all-lists)))
;; Ex. 2.55 Eva Lu Ator types to the interpreter the expression
;; (car ''abracadabra)
;; To her surprise, the interpreter prints back quote. Explain.
;; 'object is equivalent to (quote object), and
;; ''object is equivalent to (quote (quote object))
;; The interpreter evaluates ''object to a quoted list of symbols
;; where quote itself is the first symbol