[plt-scheme] building mutually recursive structs

From: David A. Herman (dherman at ccs.neu.edu)
Date: Tue Jul 20 13:14:58 EDT 2004

What's the most Schemely idiom for building structs with graph structure?
The most Java-like solution would be to make the accessors functions. The
Haskellish solution would be to use delay and force. The C solution would
probably be to run two passes, first creating the objects and then filling
in the back-pointers.

I actually prefer the latter, because it requires the least extra work of
the client: once the data structure is created, you just use the normal
accessors. But I'm not sure how to abstract over the pattern. I.e., I'd
like a form that does things like:

  (define-struct thing (parent value children))
  (letrec-data ([root (make-thing #f 0 (list (make-thing root 1 null)
                                             (make-thing root 2 null)
                                             (make-thing root 3 null)))])
  root)
  =>
  (let ([root (make-thing #f 0 (list (make-thing 'hukairs 1 null)
                                     (make-thing 'hukairs 2 null)
                                     (make-thing 'hukairs 3 null)))])
    (set-thing-parent! (car (thing-children root)))
    (set-thing-parent! (cadr (thing-children root)))
    (set-thing-parent! (caddr (thing-children root))))

I suppose I could write a function that traverses all structs and lists
within structs, but this is slow and ungeneral.

Dave


Posted on the users mailing list.