<div>Hello,</div>
<div> </div>
<div>I created a program which will display (in a list) a <a href="http://en.wikipedia.org/wiki/Farey_sequence">farey sequence</a> of level n. My code is as follows:</div>
<div> </div>
<div>(define-struct farey (num denom))</div>
<div> </div>
<div>(define (farey-level n)<br> (farey-acc (- n 1) (list (make-farey 0 1)<br> (make-farey 1 1))))</div>
<div> </div>
<div>(define (farey-acc n flist)<br> (cond<br> [(= n 0) flist]<br> [else (farey-acc (- n 1) (insert-new-farey flist))]))</div>
<div> </div>
<div>(define (insert-new-farey flist)<br> (append (get-first-two flist)<br> (get-middle flist)<br> (get-last-two flist)))</div>
<div> </div>
<div>(define (get-first-two flist)<br> (list (first flist)<br> (get-between flist)<br> (second flist)))</div>
<div> </div>
<div>(define (get-middle flist)<br> (local<br> ((define (get-mid flist acc)<br> (cond<br> [(empty? flist) empty]<br> [(= (length flist) 2) (reverse acc)]<br> [else (get-mid (rest flist) (cons (first flist) acc))])))<br>
(get-mid (rest (rest flist))<br> empty)))</div>
<div> </div>
<div>(define (get-last-two flist)<br> (cond<br> [(empty? (rest (rest flist))) (list (first flist)<br> (get-between flist)<br> (second flist))]<br>
[else (get-last-two (rest flist))]))</div>
<div> </div>
<div>(define (get-between flist)<br> (make-farey (+ (farey-num (first flist))<br> (farey-num (second flist)))<br> (+ (farey-denom (first flist))<br> (farey-denom (second flist)))))</div>
<div> </div>
<div>I test the code in Advanced Student. I call the function (farey-level 4) and get this result:</div>
<div> </div>
<div>> (shared ((-1- (make-farey 0 1)) (-9- (make-farey 1 1)))<br> (list -1- (make-farey 1 4) (make-farey 1 3) (make-farey 1 2) -9- -1- (make-farey 1 2) (make-farey 2 3) (make-farey 3 4) -9-))</div>
<div> </div>
<div>This is not the result I was expecting so I run the code again, this time in Intermediate Student, with the same function call, (farey-level 4). This is the result I get:</div>
<div> </div>
<div>>(list<br> (make-farey 0 1)<br> (make-farey 1 4)<br> (make-farey 1 3)<br> (make-farey 1 2)<br> (make-farey 1 1)<br> (make-farey 0 1)<br> (make-farey 1 2)<br> (make-farey 2 3)<br> (make-farey 3 4)<br> (make-farey 1 1))</div>
<div> </div>
<div>Now that is the result I was expecting. My question is, why did it get (shared) in Advanced Student? I didn't even use (set!). The only instance I ever encountered (shared) was when I defined a doubly-linked list and, of course, I was expecting it then. Did I make a mistake in my code?<br clear="all">
<br>-- <br>Chad Estioco<br>BS Computer Science<br>University of the Philippines-Diliman<br>==============================<br><a href="http://www.geocities.com/lokisky_walker">http://www.geocities.com/lokisky_walker</a><br>
</div>