第166回 素人くさいSICP読書会(at 三田某所)

  • 会場提供ありがとうございました
  • 参加者5人。これくらいの人数がいないと読書会って感じがしないなー
  • 315ページの「その他の命令」のところから読む
  • applicationを「作用」と訳しているところは、「適用」にしてほしかった
  • make-operation-expのコードの中の(p) (p)がメガネのように見える
  • 実際には236ページのanalyze-applicationとほぼ同じ形
  • pと略さなくてもprocと書けばいいんじゃ?
  • 問題5.9
  • make-operation-expを書き換えればOK

最初、書いた解答

(define (make-operation-exp exp machine labels operations)
  (define (correct-operands? operands)
    (if (null? operands)
        #t
        (if (or (register-exp? (car operands))
                (constant-exp? (car operands)))
            (correct-operand? (cdr operands))
            #f)))
  (let ((operands (operation-exp-operands exp)))
    (if (correct-operands? operands)
        (let ((op (lookup-prim (operation-exp-op exp) operations))
              (aprocs
               (map (lambda (e)
                      (make-primitive-exp e machine labels))
                    operands)))
          (lambda ()
            (apply op (map (lambda (p) (p)) aprocs))))
        (error "Operand must be constant or register" operands))))
  • hisaさんがWikiに上げていた解答を見せてもらって、無駄な処理をしていたことに気づく

実際にはこんな感じでOK

(define (make-operation-exp exp machine labels operations)
  (let ((op (lookup-prim (operation-exp-op exp) operations))
        (aprocs
         (map (lambda (e)
                (if (or (constant-exp? e) (register-exp? e))
                    (make-primitive-exp e machine labels)
                    (error "Operand must be constant or register" e)))
              (operation-exp-operands exp))))
    (lambda () 
      (apply op (map (lambda (p) (p)) aprocs)))))
  • 問題5.10
  • 「新しい構文」を思いつけないのが最大の問題
  • 今までの命令でCPUの機能はだいたい実現できてるし
  • hisaさん案はcallとret
    • 現状の機能でも複数の命令を組み合わせればcall/retの機能は記述できる
    • でもこれが一番まともっぽかった
  • 半分冗談でremark(注釈)という構文を考えたが、パース時にはじけば済む話。ダメダメ
  • 帰り道にswap構文というのを思いついた。あまり役立ちそうにないけど