[racket] how to use syntax-case

From: 亀田馬志 (masashi.kameda at gmail.com)
Date: Thu Feb 6 18:22:22 EST 2014

Hello. I have a problem around how to use syntax-case.

Here's a situation.
I'm using SRFI-25, the array library. It is useful; however, it doesn't
provide something like "copy-array". I would not like  to use something
"destructive", therefor I would like to make a copy of an array I defined.

I noticed I could make a copy if I use "array" function provided there.
Here is its format:

(array shape obj ...)

I mean, in order to make a copy of an array, the idea must be

(array (array-shape the-original) obj ...)

;however, I wondered how I should express "obj ..." part. The supplement of
SRFI25, or arlib.scm, provides array->list function, so I noticed I've got
to use a macro like this in the Common Lisp style.

(defmacro copy-array (arr)
    `(array (array-shape arr) ,@(array->list arr)))

At first, I checked Racket reference and I tried (require
compatibility/defmacro).

> (require srfi/25 racket/include compatibility/defmacro)
> (include "arlib.scm")
> (define-macro (copy-array arr)
    `(array (array-shape arr) ,@(array->list arr)))
> (define a (array (shape 0 3 0 3) 1 2 3 4 5 6 7 8 9))
> (define b (copy-array a))
. . array->list: undefined;
 cannot reference an identifier before its definition
  phase: 1
  explanation: cannot access the run-time definition

Strangely, even though I'd included "arlib.scm", the interpreter said
array->list was undefined.
So it might be the time(actually the first time to me) to use syntax-case.
Through some basic tutorials around syntax-case, the syntax-case macro
equivalent to the macro written in Common Lisp style stated above must be
something like this, I believe:

(define-syntax copy-array
    (lambda (stx)
      (syntax-case stx ()
        ((_ arr)
         #`(array (array-shape arr) #,@(array->list (syntax->datum
#'arr)))))))

I tried:

> (define b (copy-array a))
. . array->list: undefined;
 cannot reference an identifier before its definition
  phase: 1
  explanation: cannot access the run-time definition

array->list is "undefined" again.
What happens in the syntax-case here?

Thanx.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140207/9a69106a/attachment.html>

Posted on the users mailing list.