kb84tkhrのブログ

何を書こうか考え中です あ、あと組織とは関係ないってやつです 個人的なやつ

Reasoned Schemer (59) 10章 once

こんどはonce
これはconduのもとになってるはず

(define (once g)
  (lambda (s)
    (let loop ((s-inf (g s)))
      (cond
        ((null? s-inf) '())
        ((pair? s-inf)
         (cons (car s-inf) '()))
        (else (lambda () (loop (s-inf))))))))

ifteとそっくり
pairだったとき、carだけとってあとは捨ててしまう
onceだから

例を実行してみる
once単体で使うんじゃなくてifteと組み合わせて使うんだ

> ((ifte (once (disj2 (== #t x) (== #f x)))
     (== #f y)
     (== #t y))
   empty-s)
(((#(y) . #f) (#(x) . #t)))

disj2(== #f x)が結果に寄与してないってことだな

これでconduを書くとどうなる
onceoを書いてみよう
teacupoもセットだな

こういうのだった

(defrel (teacupo t)
  (conde ((== 'tea t))
         ((== 'cup t))))

(defrel (onceo g)
  (condu (g #s) 
         (#s #u)))

こうなる?

(define (teacupo t)
  (disj2 (== 'tea t) (== 'cup t)))

(define (onceo g)
  (ifte (once g) succeed (ifte (once succeed) fail fail)))

どれどれ

> ((teacupo x) empty-s)
(((#(x) . tea)) ((#(x) . cup)))
> ((onceo (teacupo x)) empty-s)
(((#(x) . tea)))
> (map (reify x) (run-goal #f (teacupo x)))
(tea cup)
> (map (reify x) (run-goal #f (onceo (teacupo x))))
(tea)

動いてるみたい
細かく確かめる元気はない

The end, sort of.

sort ofね