- 8時ちょっと前に月島駅の改札に着いたら誰もいなくてマジで心が折れた
- 結局、改札で1人、コンビニで1人、現地で1人追加で計4人(弾さん入れて5人)
- 雑談は他の人にまかせて、ひとりだけSICPの問題やってた
- 雑談はよく聞いてなかったけど盛り上がってたぽい
- 月島の最大のミッションは弾さんを退屈させないこと(ともんじゃ)だと思っているので、まあよかったのではないかと
- 問題5.21
- 先週書いたaのコードが処理系でちゃんと動くことを確認(assignが一箇所だけassginになってたけど)
- 結果はvalの値
(define count-leaves-machine
(make-machine
'(tree continue temp val)
(list (list 'null? null?)
(list 'pair? pair?)
(list 'car car)
(list 'cdr cdr)
(list 'not not)
(list '+ +))
'(
(assign continue (label count-done))
count-loop
(test (op null?) (reg tree))
(branch (label immediate-answer-null))
(assign temp (op pair?) (reg tree))
(test (op not) (reg temp))
(branch (label immediate-answer-not-pair))
(save continue)
(save tree)
(assign continue (label aftercount-car))
(assign tree (op car) (reg tree))
(goto (label count-loop))
aftercount-car
(restore tree)
(assign tree (op cdr) (reg tree))
(assign continue (label aftercount-cdr))
(save val)
(goto (label count-loop))
aftercount-cdr
(assign temp (reg val))
(restore val)
(restore continue)
(assign val (op +) (reg val) (reg temp))
(goto (reg continue))
immediate-answer-null
(assign val (const 0))
(goto (reg continue))
immediate-answer-not-pair
(assign val (const 1))
(goto (reg continue))
count-done)))
- もんじゃは1人帰って、もんじゃからが1人でやっぱり4人
- 人生相談会だった
(define count-leaves-machine
(make-machine
'(tree continue temp n)
(list (list 'null? null?)
(list 'pair? pair?)
(list 'car car)
(list 'cdr cdr)
(list 'not not)
(list '+ +))
'(
(assign n (const 0))
(assign continue (label count-done))
count-loop
(test (op null?) (reg tree))
(branch (label immediate-answer-null))
(assign temp (op pair?) (reg tree))
(test (op not) (reg temp))
(branch (label immediate-answer-not-pair))
(save continue)
(save tree)
(assign continue (label aftercount-car))
(assign tree (op car) (reg tree))
(goto (label count-loop))
aftercount-car
(restore tree)
(restore continue)
(assign tree (op cdr) (reg tree))
(goto (label count-loop))
immediate-answer-null
(goto (reg continue))
immediate-answer-not-pair
(assign n (op +) (reg n) (const 1))
(goto (reg continue))
count-done)))
- 関数の返り値を全く使わないコードになった(結果はnの値)
- 末尾再帰で関数から関数に値を渡すのに返り値を使うのは、変数の値を渡すための便法だということを感覚的に理解した
- ちょっと目からウロコ気分