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

SICPを読む上でよく見るメモ

ゼミのたびに「あの単語の意味ってなんだったっけ?」って事案が多発するので定義をまとめた記事を作ります。

適用順序評価と正規順序評価
  • 正規順序評価(normal-order evaluation)

  完全に展開してから簡約する。

  • 適用順序評価(applicative-order evaluation)

  引数を評価してから展開する。

続きを読む

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 の多項式の両方が与えられる問題だと考えるとなかなかえぐいので次回に続く。