Groups
Groups
Sign in
Groups
Groups
sicp-sig
Conversations
About
Send feedback
Help
연습문제 3.17
2 views
Skip to first unread message
xeraph
unread,
Jan 30, 2008, 12:36:15 AM
1/30/08
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sicp-sig
; pict. 3.16
(define x (list 'a
'b))
(define z1 (cons x x))
; pict. 3.17
(define z2 (cons (list 'a 'b) (list 'a 'b)))
(define (set-to-wow! x)
(set-car! (car x) 'wow)
x)
; 3.16
(define (count-pairs x)
(if (not (pair? x))
0
(+ (count-pairs (car x))
(count-pairs (cdr x))
1)))
; 3.17
(define (correct-count-pairs x)
(define trace '())
(define (count-pairs x)
(cond ((not (pair? x)) 0)
((memq x trace) 0)
(else
(set! trace (cons x trace))
(+ (count-pairs (car x))
(count-pairs (cdr x))
1))))
(count-pairs x))
(define q1 '(c))
(define q2 (cons q1 q1))
(count-pairs (cons q2 q2))
(correct-count-pairs (cons q2 q2))
cycle 없는 경우에만 제대로 동작합니다.
cycle 존재 여부를 찾는 것은 다음 문제입니다.
Reply all
Reply to author
Forward
0 new messages