理系学生日記

おまえはいつまで学生気分なのか

問題3-17 (3.3.1 Mutable List Structure)

問題3-16で出てきた count-pairs を,同じ pair を重複して数えないようにするよう変更する問題.
(not (not-include ...))に深い意味はない.

(use srfi-1)

(define (count-pairs x)
  (define (not-include? elem l)
    (not (find (lambda (x) (eq? x elem)) l)))
  (define sight '())

  (define (count-pairs-only-sight x)
    (cond ((not (pair? x)) 0)
	  ((not (not-include? x sight)) 0)
	  (else
	   (set! sight (cons x sight))
	   (+ (count-pairs-only-sight (car x))
	      (count-pairs-only-sight (cdr x))
	      1))))
  (count-pairs-only-sight x))