Can someone explain why the below doesn't work? I'm going through SICP. This exercise wants you to make a function that counts the pairs of a structure.
(define (count-pairs x)
(define (count-pair-wrap x traversed-pairs)
(if (or (not (pair? x)) (memq x traversed-pairs))
0
(begin (set! traversed-pairs (cons x traversed-pairs))
(+ (count-pair-wrap (car x) traversed-pairs)
(count-pair-wrap (cdr x) traversed-pairs)
1))))
(count-pair-wrap x (list)))
The correct solution is shown below. What could be going wrong?
(define (count-pairs x)
(let ((encountered '()))
(define (helper x)
(if (or (not (pair? x)) (memq x encountered))
0
(begin
(set! encountered (cons x encountered))
(+ (helper (car x))
(helper (cdr x))
1))))
(helper x)))
>>59243921
Why not load it in a debugger mate and step through it?
Why are those hot guys hitting their backs with a wooden chair?
>>59243921
>kissing men in the lips
Degenerates