[plt-scheme] It gets shared!

From: Andrei Estioco (chadestioco at gmail.com)
Date: Mon Feb 16 19:30:11 EST 2009

Hello,

I created a program which will display (in a list) a farey
sequence<http://en.wikipedia.org/wiki/Farey_sequence>of level n. My
code is as follows:

(define-struct farey (num denom))

(define (farey-level n)
  (farey-acc (- n 1) (list (make-farey 0 1)
                           (make-farey 1 1))))

(define (farey-acc n flist)
  (cond
    [(= n 0) flist]
    [else (farey-acc (- n 1) (insert-new-farey flist))]))

(define (insert-new-farey flist)
  (append (get-first-two flist)
          (get-middle flist)
          (get-last-two flist)))

(define (get-first-two flist)
  (list (first flist)
        (get-between flist)
        (second flist)))

(define (get-middle flist)
  (local
    ((define (get-mid flist acc)
       (cond
         [(empty? flist) empty]
         [(= (length flist) 2) (reverse acc)]
         [else (get-mid (rest flist) (cons (first flist) acc))])))
    (get-mid (rest (rest flist))
             empty)))

(define (get-last-two flist)
  (cond
    [(empty? (rest (rest flist))) (list (first flist)
                                        (get-between flist)
                                        (second flist))]
    [else (get-last-two (rest flist))]))

(define (get-between flist)
  (make-farey (+ (farey-num (first flist))
                 (farey-num (second flist)))
              (+ (farey-denom (first flist))
                 (farey-denom (second flist)))))

I test the code in Advanced Student. I call the function (farey-level 4) and
get this result:

> (shared ((-1- (make-farey 0 1)) (-9- (make-farey 1 1)))
  (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-))

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:

>(list
 (make-farey 0 1)
 (make-farey 1 4)
 (make-farey 1 3)
 (make-farey 1 2)
 (make-farey 1 1)
 (make-farey 0 1)
 (make-farey 1 2)
 (make-farey 2 3)
 (make-farey 3 4)
 (make-farey 1 1))

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?

-- 
Chad Estioco
BS Computer Science
University of the Philippines-Diliman
==============================
http://www.geocities.com/lokisky_walker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090217/3116eb63/attachment.html>

Posted on the users mailing list.