読者です 読者をやめる 読者になる 読者になる

SICPゼミ第41回

練習問題4.4
(define (eval-and exp env)
  (cond
    ((null? exp) #t)
    ((null? (cdr exp)) (eval (cdr exp) env))
    ((not (eval (car exp) env)) #f)
    (else (eval-and (cdr exp) env))))

(define (and? exp) (tagged-list? exp 'and))

by dolicas

派生式の方もできた

(define (vars exp))
(define (and->if exp)
  (make-if (null? (vars exp))
                 #t
                 (make-if (car (vars exp))
                                (and-if ('and (cdr (vars exp)))
                                false))

by ぷに

練習問題4.5
(define (expand-clauses clauses)
    (if (null? clauses)
'false ;
(let ((first (car clauses))
    (rest (cdr clauses)))
(if (cond-else-clause? first)
    (if (null? rest)
        (sequence->exp (cond-actions first))
        (error "ELSE clause isn't last: COND->IF"
            clauses))
    (make-if (cond-predicate first)
        (if (cond-test? (cond-actions first))
            ((eval (cdr cond-actions) env) (eval cond-predicate env))
            (sequence->exp (cond-actions first)))
        (expand-clauses rest))))))

(define (cond-test? clause) 
    (= (cond-predicate clase) '=>))

と変更する

by dolicas

練習問題4.6
(define (let->combination exp)
    (let ((defs (let-defs exp)))
    ((make-lambda (let-variable-from-defs defs) (let-body exp))
     (let-defbody-from-defs defs))))
(define (let-defs exp)
    (cadr exp))
(define (let-body exp)
    (caddr exp))
(define (let-variable-from-defs defs)
        (if (null? defs)
            '()
            (cons (caar defs) (let-variable-from-defs (cdr defs)))))
(define (let-defbody-from-defs defs)
        (if (null? defs)
            '()
            (cons (cdar defs) (let-variable-from-defs (cdr defs)))))