Scheme で TCO

末尾呼び出しの最適化が保証されている Scheme でやる意味は特にないわけだが原理の確認のためにやってみる
ネタ元は Javascript in Ten Minutes ( https://github.com/spencertipping/js-in-ten-minutes/ )の §7.5

; tco.scm
(define (tail fun . args)
  (cons fun args) )

(define (call_with_tco fun . args)
  (let ((esc (car args)))
       (do ((c (cons fun args) (apply (car c) (cdr c))))
           ((eq? (car c) esc) (apply (car c) (cdr c))) )))

(define (fact k n acc)
  (if (> n 0)
      (tail fact k (- n 1) (* acc n))
      (tail k acc) ))
$ rlwrap gosh
gosh> (load "./tco")
#t
gosh> (call_with_tco fact identity 5 1)
120

変更点としては脱出用の k を先頭に置くようにしたぐらいであとはほぼそのまま
eq? で手続きが比較できるかどうかは実装依存なので、そのへん Gauche に依存