さかもとのブログ

つらつらと

SICP演習問題4.5

一応結果も載せる.

;;exercise4.5
(define (cond-recipient? clause) (eq? (car (cond-actions clause)) '=>))

(define (test->recipient clause)
  (let ((predicate (cond-predicate clause))
        (action (cadr (cond-actions clause))))    ;1引数の手続き
    (list action predicate)))

(define (expand-clauses clauses)
  (if (null? clauses)
      #f
      (let ((first (car clauses))
            (rest (cdr clauses)))
        (if (cond-else-clause? first)
            (if (null? rest)
                (sequence->exp (cond-actions first))
                (error "ELSE clauses isn't last -- COND->IF"
                       clauses))
            (make-if (cond-predicate first)
                     (if (cond-recipient? first)
                         (test->recipient first)
                         (sequence->exp (cond-actions first)))
                       (expand-clauses rest))))))

(driver-loop)
(cond ((> 10 1) 10)
      (else 11))

;;; M-Eval value:
;10

(cond ((assoc 'b '((a 1) (b 2))) => cadr)
      (else 'hoge))

;;; M-Eval value:
;2

(cond ((assoc 'b '((a 1) (c 2))) => cadr)
      (else 'hoge))

;;; M-Eval value:
;hoge