(wat-aro)

生きてます

SICP 問題 4.55

データベースへの単純質問を示せ ;; a Ben Bitiddleに監督されているすべての人々 (supervisor ?x (Bitdiddle Ben)) ;; b 経理部門のすべての人々の名前と担当 (job ?x (accounting . ?type)) ;; c Slumervilleに住む人すべての名前と住所 (address ?x (Slum…

SICP 問題 4.54

requireを特殊形式で実装する. (define (require? exp) (tagged-list? exp 'require)) (define (require-predicate exp) (cadr exp)) (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted…

SICP 問題 4.53

permanent-set!でpairsに成功する組み合わせを束縛するが, (amb)で必ず失敗するので全ての成功する組み合わせをpairsに束縛する. 失敗継続が呼ばれ,if-failの第二引数のpairsが評価される. この時permanent-set!で束縛されているのでバックトラックで戻…

SICP 問題 4.52

利用者が失敗を捉えることができるif-failを実装する. (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted exp)) ((variable? exp) (analyze-variable exp)) ((assignment? exp) (analyz…

SICP 問題 4.51

バックトラックで戻らないpermanent-set!の実装 ;; permanent-set! (define (analyze-permanent-assignment exp) (let ((var (assignment-variable exp)) (vproc (analyze (assignment-value exp)))) (lambda (env succeed fail) (vproc env (lambda (val fa…

SICP 問題 4.50

ランダムな順に探すrambを実装する. (use srfi-27) (define (random-car lst) (list-ref lst (random-integer (length lst)))) (define (rember item lst) (cond ((null? lst) '()) ((eq? (car lst) item) (cdr lst)) (else (cons (car lst) (rember item (…

SICP 4.3 amb評価器

(define true #t) (define false #f) ;; eval (define (ambeval exp env succeed fail) ((analyze exp) env succeed fail)) (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted exp)) ((v…

OS X Yosemiteにswank-gaucheのインストール

きっかけはTwitter. @wat_aro どの処理系使ってるかわからないですがgaucheのswank-gaucheみたいに対応されてたりしませんか?— lispドラッグ常習者 (@rayfill) 2016, 1月 12 早速インストール. READMEに 設定方法 `dot.emacs'の内容を.emacsにコピーしてs…

SICP 問題 4.49

自然言語の構文解析用プログラムを少し改造するだけで文章の生成ができる. (define (an-element-of items) (require (not (null? items))) (amb (car items) (amb (cdr items)))) (define (parse-word word-list) (list (car word-list) (an-element-of (cd…

SICP 問題 4.48

adjectiveのみ追加する. ;; 冠詞の解析をparse-article-phraseにしてそこでmaybe-extendに進めばadjectiveも解析されるようにする. (define (parse-simple-noun-phrase) (list 'simple-noun-phrase (parse-article-phrase) (parse-word nouns))) (define (…

SICP 問題 4.47

(define (parse-verb-phrase) (amb (parse-word verbs) (list 'verb-phrase (parse-verb-phrase) (parse-prepositional-phrase)))) (parse '(the student with the cat sleeps in the class))これを例に考える. verbが問題なのでsleeps in the classだけに…

SICP 問題 4.46

構文解析器が右から左に非演算子を評価する時 perse-noun-phrase中の (amb noun-phrase (maybe-extend (list 'noun-phrase noun-phrase (parse-prepositional-phrase)))) で,maybe-extendが先に評価されてしまう. すると,実際に名詞が先にきた場合はmaybe…

SICP 問題 4.45

The professor lectures to the student in the class with the cat. これの5通りの構文解析結果を示す. まずはprofessor に in the class とwith the cat が掛かっている場合. 次はprofessorにwith the cat , student にin the classがかかっている場合…

SICP 問題 4.44

eight-queenをamb評価器を用いて解く. ただし,まだamb評価器は実装していないのでコードだけ. (define (eight-queen) (define (cross? a b) (= (/ (/ (car a) (car b)) (abs (- (cdr a) (cdr b)))) 1)) (let ((chess (iota 8 1))) (let ((one (cons 1 (am…

SICP 問題 4.43

まずは素直に問題文に出てくる通りに書いてみる. (define (kansas-state-enginner) (let ((moore (cons (amb 'mary 'gabrielle 'lorna 'rosalind 'melissa) (amb 'mary 'gabrielle 'lorna 'rosalind 'melissa))) (downing (cons (amb 'mary 'gabrielle 'lor…

SICP 問題 4.42

どちらかが正しいってどうやればいいのか思いつけず, ここを見たらヒントがあったのでその通り排他的論理和を作って解きました. SICP 第4章 Exercise 難易度リスト ( 4.1 ~ 4.79 ) | きのこる庭 (define (xor x y) (or (and x (not y)) (and (not x) y)))…

SICP 問題 4.41

多住居手続きをschemeで実装. (use util.combinations) (use srfi-1) (define (multiple-dwelling) (filter (lambda (x) (let ((baker (first x)) (cooper (second x)) (fletcher (third x)) (miller (fourth x)) (smith (fifth x))) (and (not (= baker 5)…

SICP 問題 4.40

人の階への割り当ての組みは,相異なるという要求の前では55通りある. 要求の後では5!通りになる. ambで生成してすぐにテストすることで効率的な手続き (define (multiple-dwelling) (let ((baker (amb 1 2 3 4 5))) (require (not (= baker 5))) (let ((c…

SICP 問題 4.39

制限の順番は解には影響しないが,その時間には影響する. 失敗が多い制限ほど先にテストするほうが実行速度は速くなる. ;; 本来のmultiple-dwelling (define (multiple-dwelling) (let ((baker (amb 1 2 3 4 5)) (cooper (amb 1 2 3 4 5)) (fletcher (amb …

SICP 問題 4.38

これを手で解けとな... B≠5 C≠1 F≠1 F≠5 C C≠5 |F-C| ≠ 1 答えは5通り. 計算機プログラムの構造と解釈 第2版作者: ハロルドエイブルソン,ジュリーサスマン,ジェラルド・ジェイサスマン,Harold Abelson,Julie Sussman,Gerald Jay Sussman,和田英一出版社/…

SICP 問題 4.37

(define (a-pythagorean-triple-between low high) (let ((i (an-integer-between low high)) (hsq (* high high))) (let ((j (an-integer-between i high))) (let ((ksq (+ (* i i) (* j j)))) (require (>= hsq ksq)) (let ((k (sqrt ksq))) (require (int…

SICP 問題 4.36

(define (a-pythagorean-triple-between low high) (let ((i (an-integer-between low high))) (let ((j (an-integer-between i high))) (let ((k (an-integer-between j high))) (require (= (+ (* i i) (* j j)) (* k k))) (list i j k))))) ;; an-integer…

SICP 問題 4.35

二つの与えられた限界の間の整数を返す手続き (define (an-integer-between low high) (require (<= low high)) (amb low (an-integer-between (+ low 1) high))) 計算機プログラムの構造と解釈 第2版作者: ハロルドエイブルソン,ジュリーサスマン,ジェラル…

SICP 問題 4.34

遅延対とリストを正当に印字できるようにする. consへのタグづけがどうしてもうまくいかなくてここを参考にしました. SICP Exercise 4.34 | Weiqun Zhang's Blog 前回からの変更箇所のみ (define (eval exp env) (cond ((self-evaluating? exp) exp) ((var…

SICP 問題 4.33

遅延リストの実装に合わせて,quoteを遅延リストに対応させる. (car '(a b c))で正しくaが表示できるようにする. make-lambdaの(make-quote (car obj))のところ,始め(car obj)だけにしていたら, 数字ではうまくいくのに'(a b c)だとunbound variable: a…

SICP 問題 4.32

遅延度の高い遅延リストではcar部も遅延されているので未定義の変数を使って構成するできる. ;;; M-Eval input: (define my-stream (cons x y)) ;;; M-Eval value: ok ;;; M-Eval input: my-stream ;;; M-Eval value: (compound-procedure (m) ((m x y)) <procedure-env>) </procedure-env>…

特殊形式は高階手続きと一緒に使うことができない

手続きは引数を全て評価してoperatorに渡す. 特殊形式は引数を全て評価するとは限らない. ここではdefineについて見てみる. defineは第1引数は評価せず,第2引数を評価した値を第1引数に束縛する. (define x (+ 1 2)) (+ 1 2) 3 (define x 3) x 3 次…

SICP 問題 4.31

(define (f a (b lazy) c (d lazy-memo)) ...) といった形で部分的に遅延評価やメモ化する遅延評価を実装する. 元となるのは4.30までで作っていた遅延評価器. まず変更した部分を書く. ;; メモ化する評価器 (define (force-it obj) (cond ((thunk? obj) (…

SICP 問題 4.30

並びの中の式は最後まで評価されないのではないかというCy D. Fectの心配に答える. a ;; 元のeval-sequence (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else (eval (first-exp exps) env) (eval-sequence (r…

遅延評価と末尾再帰フィボナッチ

前回のSICP問題4.29で遅延評価する評価器でメモ化しない場合に,する場合と比べてはるかに遅くなるプログラムの例としてフィボナッチを書きました. ただ,あまりに差が大きくてなぜそうなるのかがわからなかったので考えてみました. 評価器は最後に載せて…