SICP問題3.17
正しい解を返すcount-pairsの定義
数えた対を覚えておいて、最後にその対の数を返す
(define (count-pairs x) (define counted-pairs '()) (define (count-pairs-sub y) (if (and (pair? y) (not (memq y counted-pairs))) (begin (set! counted-pairs (cons y counted-pairs)) (count-pairs-sub (car y)) (count-pairs-sub (cdr y))))) (count-pairs-sub x) (length counted-pairs))
テスト3.16で使用したリスト構造を使用する
(count-pairs l3) 3 (count-pairs l4) 3 (count-pairs l7) 3 (count-pairs ll) 3
OK