SICPゼミ第44回

練習問題4.14

なんかタグとかがうまく処理されないと思う。

by tube

練習問題4.15

ネゲートして自分を突っ込む。終わり。

by tube

SICPゼミ第43回

練習問題4.11

(define (make-frame variables values)
  (if (= (length variables values))
      (if (null? variables)
          nil
         (cons (cons (car variables) (car values))) (make-frame (cdr variables) (cdr values)))
      (if (< (length variables) (length values))
          (error "Too many arguments supplied" variables values)
          (error "Too few arguments supplied" variables values ))))

(define (frame-variables frame)
  (if (null? frame)
      nil
      (cons (caar frame) (frame-variables (cdr frame)))))
(define (frame-values frame)
  (if (null? frame)
      nil
      (cons (cdar frame) (frame-variables (cdr frame)))))

(define (add-binding-to-frame! var val frame)
  (set! frame (cons (cons var val) frame)))

by pine

練習問題4.12
(define (enviroment-searching-alg var find-operation error-operation env)
  (define (env-loop env)
    (define (scan vars vals)
      (cond ((null? vars)
        (env-loop (enclosing-environment env)))
        ((eq? var (car vars)) (find-operation (car vals) (car vars)))
        (else (scan (cdr vars) (cdr vals)))))
    (if (eq? env the-empty-environment)
      (error-operation)
      (let ((frame (first-frame env)))
        (scan (frame-variables frame)
        (frame-values frame)))))
  (env-loop env))

(define (lookup-variable-value var env)
  (enviroment-searching-alg var (lambda (x y) x) (error 'hoge) env))

(define (set-variable-value! var val env)
  (enviroment-searching-alg var (lambda (x y) (set! x val)) (error 'hoge) env))

(define (define-variable! var val env)
  (enviroment-searching-alg var (lambda (x y) (set! x val)) 
    (add-binding-to-frame! var val (car env)) (list (car env))))

by dolicas

練習問題4.13
(define (make-unbound! var env) 
    (define (env-loop env) 
        (define (scan vars vals) 
            (cond ((null? vars) (env-loop (enclosing-environment env))) 
                    ((eq? var (car vars))
                     (begin (set! vals (cdr vals))
                              (set! vars (cdr vars)))) 
                    (else (scan (cdr vars) (cdr vals))))) 
        (if (eq? env the-empty-environment) 
             (error "Unbound variable" var) 
             (let ((frame (first-frame env))) 
                    (scan (frame-variables frame) 
                            (frame-values frame))))) 
    (env-loop env))

By tube

SICPゼミ第42回

練習問題4.7
(define (let*->nested-lets exp)
  (let ((defs (car exp)) (body (cdr exp)))
    (if (null? (cdr defs))
      (list 'let (car defs) body)
      (list 'let (car defs)
        (let*->nested-lets (pair (cdr defs) body))))))

by dolicas

問題文で言ってる派生式でうまくいくはず.

練習問題4.8
(define (let->combination exp)
    (if (pair? (cadr exp))
        (let->combination-prototype exp)
        (let* ((defs (let-defs exp)) 
            (body (cadddr exp)) 
            (func (make-lambda defs body)))
          (let->combination-prototype
            (list 'let (cons ((cadr exp) func) defs) body))))

(define (let->combination-prototype 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)))))

by dolicas

練習問題4.9
(define (for? exp) (tagged-list? exp 'for))
(define (for-initial exp) cadr)
(define (for-terminate exp) caddr)
(define (for-body exp) cadddr)

(define (for->rec exp)
        (let->combination
          ('let for_iter
                ((i (for-initial exp)))
                (make-if
                  (> i (for-terminate exp))
                  'done
                  (begin (for-body exp)
                         (for_iter (+ i 1)))))))

by tube

練習問題4.10
(define (eval-if exp env)
(if (true? (eval (if-predicate exp) env))
(eval (if-alternative exp) env)
(eval (if-consequent exp) env)))

と変えるとifが逆になるという新しい文法のschemeになる.

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)))))

SICPゼミ第40回

2章はやばいので4章に帰ってきました。

練習問題4.2

applicaionかどうかの判定 (application?) はタグを気にせずペアかどうかしか見ていない.これが最後でないと (define …) みたいなのも適用として扱ってしまうのでおかしくなる. call を使うことにするのであれば,ほかのやつと同じように,callというタグが付いてると思って扱えばよい.

by dolicas

練習問題4.3

(define (eval exp env)
  (if (self-evaluating? exp)
    exp
    (if (get 'eval (get-tag exp))
      ((get 'eval (get-tag exp)) (get-content exp) env)
      (apply (eval (operator exp) env) (list-of-values (operands exp) env)) 
    )
  ))
(define (get-tag exp)
  (car exp))
(define (get-content exp) (cdr exp))

by dolicas

SICPゼミ第39回

練習問題2.92

係数がy の多項式であるx の多項式と係数がx の多項式であるy の多項式の両方が与えられる問題だと考えるとなかなかえぐいので次回に続く。

SICPゼミ第38回

練習問題2.91

(define (install-polynomial-package)
  ;; 内部手続き
  ;; poly の表現
  (define (make-poly variable term-list) (cons variable term-list ))
  (define (variable p) (car p))
  (define (term-list p) (cdr p))
  (define same-variable? eq?)
  (define variable? symbol?)
  ;; 項と項リストの表現
  (define (add-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (add-terms (term-list p1) (term-list p2)))
        (error "Polys not in same var: ADD-POLY" (list p1 p2))))
  (define (add-terms L1 L2)
    (cond (( empty-termlist? L1) L2)
          (( empty-termlist? L2) L1)
          (else
           (let ((t1 (first-term L1))
                 (t2 (first-term L2)))
             (cond ((> (order t1) (order t2))
                    (adjoin-term
                     t1 (add-terms (rest-terms L1) L2)))
                   ((< (order t1) (order t2))
                    (adjoin-term
                     t2 (add-terms L1 (rest-terms L2))))
                   (else
                      (adjoin-term
                     (make-term (order t1)
                                (add (coeff t1) (coeff t2)))
                     (add-terms (rest-terms L1)
                                (rest-terms L2 )))))))))
  (define (inversion termList)
    (if (empty-termlist? termList)
        termList
        (let ((term (first-term termList)))
          (if (eq? (type-tag term) 'polynomial)
              (adjoin-term
               (make-term (order term)
                          (inversion (coeff term)))
               (inversion (rest-terms termList)))
              (adjoin-term
               (make-term (order term)
                          (mul (make-scheme-number -1) (coeff term)))
               (inversion (rest-terms termList)))))))
  (define (mul-poly p1 p2)
    (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (mul-terms (term-list p1) (term-list p2)))
        (error "Polys not in same var: MUL-POLY" (list p1 p2))))
  (define (mul-terms L1 L2)
    (if (empty-termlist? L1)
        (the-empty-termlist)
        (add-terms (mul-term-by-all-terms (first-term L1) L2)
                   (mul-terms (rest-terms L1) L2))))
  (define (mul-term-by-all-terms t1 L)
    (if (empty-termlist? L)
        (the-empty-termlist)
        (let ((t2 (first-term L)))
          (adjoin-term
           (make-term (+ (order t1) (order t2))
                      (mul (coeff t1) (coeff t2)))
           (mul-term-by-all-terms t1 (rest-terms L))))))
  (define (adjoin-term term term-list)
    (if (=zero? (coeff term ))
        term-list
        (cons term term-list )))
  (define (div-terms L1 L2)
    (if (empty-termlist? L1)
        (list (the-empty-termlist) (the-empty-termlist ))
        (let ((t1 (first-term L1))
              (t2 (first-term L2)))
          (if (> (order t2) (order t1))
              (list (the-empty-termlist) L1)
              (let ((new-c (div (coeff t1) (coeff t2)))
                    (new-o (- (order t1) (order t2))))
                (let ((rest-of-result
                       (let ((rest (add-terms L1
                                              (inversion
                                               (mul-terms L2 (list (make-term new-o new-c)))))))
                         (div-terms rest L2))))
                  (cons (adjoin-term (make-term new-o new-c)
                                     (car rest-of-result))
                        (cdr rest-of-result))
                  ))))))
  (define (div-poly p1 p2)
     (if (same-variable? (variable p1) (variable p2))
        (make-poly (variable p1)
                   (div-terms (term-list p1) (term-list p2)))
        (error "Polys not in same var: ADD-POLY" (list p1 p2))))
(define (the-empty-termlist) '())
(define (first-term term-list) (car term-list ))
(define (rest-terms term-list) (cdr term-list ))
(define (empty-termlist? term-list) (null? term-list ))
(define (make-term order coeff) (list order coeff ))
(define (order term) (car term ))
(define (coeff term) (cadr term ))
  ;; システムのほかの部分とのインターフェイス
  (define (tag p) (attach-tag 'polynomial p))
  (put 'add '(polynomial polynomial)
       (lambda (p1 p2) (tag (add-poly p1 p2))))
  (put 'mul '(polynomial polynomial)
       (lambda (p1 p2) (tag (mul-poly p1 p2))))
  (put 'make 'polynomial
       (lambda (var terms) (tag (make-poly var terms ))))
  (put 'div '(polynomial polynomial)
       (lambda (p1 p2) (tag (div-poly p1 p2))))
  'done)

by tube