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

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

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

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

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

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

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

続きを読む

SICPゼミ第46回

練習問題4.18

(define (solve f y0 dt)
  (define y (integral (delay dy) y0 dt))
  (define dy (stream-map f y))
  y)

(define (solve_ f y0 dt)
  (let ((y '*hoge*) (dy '*hoge*))
    (let ((a (integral (delay dy) y0 dt))
          (b (stream-map f y)))
      (set! y a)
      (set! dy b))
    y))

(define (solve__ f y0 dt)
  (let ((y '*hoge*) (dy '*hoge*))
    (set! y (integral (delay dy) y0 dt))
    (set! dy (stream-map f y))
    y))

実行結果

> (stream-ref (solve (lambda (y) y) 1 0.001) 1000)
2.716923932235896
> (stream-ref (solve_ (lambda (y) y) 1 0.001) 1000)
. . car: contract violation
  expected: pair?
  given: '*hoge*
> (stream-ref (solve__ (lambda (y) y) 1 0.001) 1000)
2.716923932235896

書き換えるとエラーが出る。 solve_のほうでは、(b (stream-map f y)) が評価されるときに y には '*hoge* が入っているのでエラーが出る。 solve__ のほうでは、(set! y (integral (delay dy) y0 dt)) が評価されるときに dy には '*hogeが入っているが、遅延評価なので問題なく動く。

by tube

練習問題4.19

Ben式は割といろんな言語である気がする.Eva式は逐次定義絶対殺すマンなので割と異常だと思う.ところがGoshはこの方針で実装されているのでたいへん気持ち悪い.とりあえず結論としては「ここで出てくるようなプログラムを書くのを止めろ」に尽きる.by現実

SICPゼミ第45回

練習問題4.16

(define (lookup-variable-value var env)
  (define (env-loop env)
    (define (scan vars vals)
      (cond (( null? vars)
             (env-loop (enclosing-environment env )))
            ((eq? var (car vars ))
             (if (eq? (car vals) '*unassigned*)
                 (error "Unassigned Variable")
                 (car vals)))
            (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))

b.

(define (scan-out-defines procedure-body)
  (if (null? procedure-body)
      null
      (let ((exp (car procedure-body)))
        (if (definition? exp)
            (let ((var (definition-variable exp)))
              ((make-lambda var
                            (begin (set! var (definition-value exp))
                                   (scan-out-defines (cdr procedure-body))))
               '*unassigned*))
            (cons exp (scan-out-defines (cdr procedure-body)))))))

c. どっちでも問題はないが,make-procedureのほうが効率がいい.make-procedureに入れると,evalの一回だけscan-outするが,procedure-bodyに入れるとapplyするたびにscan-outすることになる.

練習問題4.17

f:id:sicp-zemi:20170426162851j:plain f:id:sicp-zemi:20170426162905j:plain

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