[plt-scheme] Structure constructor

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Mon Jul 17 14:03:43 EDT 2006

Hi all,

What is the simplest way to go from a structure to its constructor?
The following seems to work, but I have a hunch there is a simpler
solution.

; struct-constructor : struct -> constructor
(define (struct-constructor s)
  (define (struct->struct-id s)
    (let*-values ([(info _)                    (struct-info s)]
                  [(name _1 _2 _3 _4 _5 _6 _7) (struct-type-info info)])
      name))
  (let* ([name (struct->struct-id s)]
         [make-name (string->symbol
                     (string-append
                      "make-" (symbol->string name)))])
    (namespace-variable-value make-name)))

; > (define-struct foo (bar baz) (make-inspector))
; > (define s (make-foo 'a 'b))
; > (struct-constructor s)
; #<struct-procedure:make-foo>

; > (print-struct #t)
; > ((struct-constructor s) 'c 'd)
; #(struct:foo c d)

-- 
Jens Axel Søgaard







Posted on the users mailing list.