<div dir="ltr">Hello. I have a problem around how to use syntax-case.<div><br></div><div>Here's a situation.</div><div>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.</div>
<div><br></div><div>I noticed I could make a copy if I use "array" function provided there. Here is its format:</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium">(array </span><var style="color:rgb(0,0,0);font-family:monospace;font-size:medium">shape</var><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium"> </span><var style="color:rgb(0,0,0);font-family:monospace;font-size:medium">obj ...</var><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium">)</span><br>
</div><div><span style="color:rgb(0,0,0);font-family:monospace;font-size:medium"><br></span></div><div><font color="#000000" face="monospace" size="3">I mean, in order to make a copy of an array, the idea must be</font></div>
<div><font color="#000000" face="monospace" size="3"><br></font></div><div><font color="#000000" face="monospace" size="3">(array (array-shape the-original) obj ...)</font></div><div><font color="#000000" face="monospace" size="3"><br>
</font></div><div><font color="#000000" face="monospace" size="3">;however, I wondered how I should express "obj ..." part. The</font><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px"> </span><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px">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.</span></div>
<div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px"><br></span></div><div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px">(defmacro copy-array (arr)</span></div>
<div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px">    `(array (array-shape arr) ,@(array->list arr)))</span></div><div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px"><br>
</span></div><div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px">At first, I checked Racket reference and I tried (require compatibility/defmacro).</span></div><div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px"><br>
</span></div><div><span style="color:rgb(68,68,68);font-family:arial,sans-serif;line-height:16.1200008392334px"><div>> (require srfi/25 racket/include compatibility/defmacro)</div><div>> (include "arlib.scm")</div>
<div><div>> (define-macro (copy-array arr)</div><div>    `(array (array-shape arr) ,@(array->list arr)))</div><div>> (define a (array (shape 0 3 0 3) 1 2 3 4 5 6 7 8 9))</div><div>> (define b (copy-array a))</div>
<div>. . array->list: undefined;</div><div> cannot reference an identifier before its definition</div><div>  phase: 1</div><div>  explanation: cannot access the run-time definition</div></div><div><br></div><div>Strangely, even though I'd included "arlib.scm", the interpreter said array->list was undefined. </div>
<div>So it might be the time(actually the first time to me) to use syntax-case.</div><div>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:</div>
<div><br></div><div><div>(define-syntax copy-array</div><div>    (lambda (stx)</div><div>      (syntax-case stx ()</div><div>        ((_ arr)</div><div>         #`(array (array-shape arr) #,@(array->list (syntax->datum #'arr)))))))</div>
</div><div><br></div><div>I tried:</div><div><br></div><div><div>> (define b (copy-array a))</div><div>. . array->list: undefined;</div><div> cannot reference an identifier before its definition</div><div>  phase: 1</div>
<div>  explanation: cannot access the run-time definition</div></div><div><br></div><div>array->list is "undefined" again.</div><div>What happens in the syntax-case here? </div><div><br></div><div>Thanx.</div>
</span></div></div>