SICP 読み (221) 4.3.2 非決定性プログラムの例

ちょい前に断念した 8queen と amb なソレですが、自然言語構文解析なナニで例示されている parse-noun-phrase などの手続きの定義を見ていて、これ方式で何とかなりそうな気がしています。
実は問題 4.49 をバスの中で見てて、変更簡単じゃね??と言いつつ見てたんですが、found-word に (cdr word-list) を amb したソレを、という事に気がつきまして、これは降参かな、と思いつつ例示された手続きを見てて

(define (list-amb l)
  (if (null? l)
      (amb)
      (amb (car l)
	   (list-amb (cdr l)))))

みたいなナニでできるんじゃね??という事に思い当たった次第でして。上記のソレを使用して評価器上で

(list (list-amb '(1 2 3)) (list-amb '(1 2 3)))

みたいな事をしたら一応きちんとバックトラックしている模様。むむ。

問題 4.49

机上ベースで問題の解を。

(define (list-amb l)
  (if (null? l)
      (amb)
      (amb (car l)
	   (list-amb (cdr l)))))

(define (parse-word word-list)
  (require (not (null? *unparsed*)))
  (let ((found-word (list-amb (cdr word-list))))
    (set! *unparsed* (cdr *unparsed*))
    (list (car word-list) found-word)))

で良いのかなぁ。とりあえず実機確認する前に机上ベースで出てくる予想を。とりあえず、parse に渡すのは '(the professor lectures to the student with the cat) で。

(the student studies for the student for the student)
(the student studies for the student for the professor)
(the student studies for the student for the cat)
(the student studies for the student for the class)
(the student studies for the student for a student)
(the student studies for the student for a professor)
(the student studies for the student for a cat)
(the student studies for the student for a class)
(the student studies for the student to the student)
(the student studies for the student to the professor)
(the student studies for the student to the cat)
(the student studies for the student to the class)

なんかキリが無いなぁ。先頭からキてくれたらもう少し面白そげなんですが。実機で確認したら当たり前ですが上記の通りな模様。出力を整形するのは手間なんで略。(を
もしリキがあったら 8queen にトライしてみたひ。