[plt-scheme] Scheme behavior

From: Jay Riddle (jcriddle4 at yahoo.com)
Date: Mon Apr 6 23:22:43 EDT 2009

In order to learn scheme and macros a little better I was attempting to write a macro that would allow me make a slighty simpler stuct-copy.  My version of the struct-copy would require one few parameters.  This is sort of like what ocaml has using the 'with' statement on a structure(?I am not sure I used the correct terms there?).

So instead of writting

      (struct-copy my-struct some-mystruct (fieldN valueN))

I would write

     (with some-mystruct (fieldN valueN)) 

Scheme wasn't too happy with some of my primitive attempts with (define-syntax....) so I attempted with define-macro.

I came up with this

(define-macro with
    (λ (s . body)
      (list 'cond
        (list (list 'myvar? s) (append (list 'struct-copy 'myvar s) body))
        (list (list 'myfun? s) (append (list 'struct-copy 'myfun s) body))
        (list (list 'myexp? s) (append (list 'struct-copy 'myexp s) body))
        '(else (raise "091-33 'with' not defined for struct type")))))

The error I am getting is "struct-copy: accessor name not associated with the given structure type in:..."

In order to narrow down what I was doing wrong I did a simple test case that should be simular to what my macro was doing and I noticed that this code would fail to compile in drscheme.

1   (define (test e)
2     (cond 
3       ((myvar? e) (struct-copy myvar e (name "tom")(isfunc #t))) 
4       ((myfun? e) (struct-copy myfun e (name "tom")(isfunc #t))) 
5       ((myexp? e) (struct-copy myexp e (name "tom")(isfunc #t))) 
6       (else (raise 091-33 'with' not defined for struct type)))
7     )

Basically the only structure that has the field 'isfunc' is the myexp structure.  Is scheme doing some type checking or something?  Why does scheme care about line 3 and 4 at compile time?

Posted on the users mailing list.