ex 2.53, 2.54, 2.55

;; sicp ex 2.53
(define (memq item x)
  (cond ((null? x) #f)
        ((eq? item (car x)) x)
        (else (memq item (cdr x)))))
;; gosh> (list 'a 'b 'c)
;; (a b c)
;;
;; gosh> (list (list 'george))
;; ((george))
;;
;; gosh> (cdr '((x1 x2) (y1 y2)))
;; ((y1 y2))
;;
;; gosh> (cadr '((x1 x2) (y1 y2)))
;; (y1 y2)
;;
;; gosh> (pair? (car '(a short line)))
;; #f
;; (car '(a short line)) -> a なので
;;
;; gosh> (memq 'red '((red shoes) (blue socks)))
;; #f
;; memqは入れ子になったものの要素一つ一つまでは走査されないので #f
;;
;; gosh> (memq 'red '(red shoes blue socks))
;; (red shoes blue socks)
;; sicp ex 2.54
(define (my-equal? a b)
  (cond ((and (null? a) (null? b)) #t)
        ((or (null? a) (null? b)) #f)
        ((and (pair? (car a)) (pair? (car b))) (and (my-equal? (car a) (car b))
                                                    (my-equal? (cdr a) (cdr b))))
        ((or (pair? (car a)) (pair? (car b))) #f)
        ((eq? (car a) (car b)) (my-equal? (cdr a) (cdr b)))
        (else #f)))

場合分けをしっかりやる必要がある。

;; sicp ex 2.55
;; gosh> (car ''abracatabra)
;; quote
;; 
;; (car (quote (quote abracatabra)))
;; なので2番目のquoteが返っている。

新しい概念が出てくるときは問題が簡単。


ex 2.44〜2.52の図形言語はしっかりとやってみたいので楽しみに取っておく。