<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
You might have already read it, but I also found the guide "Fear of
Macros" by Greg Hendershott incredibly helpful in understanding
them, especially considering things like with-syntax and format-id:<br>
<a class="moz-txt-link-freetext" href="http://www.greghendershott.com/fear-of-macros/index.html">http://www.greghendershott.com/fear-of-macros/index.html</a><br>
<br>
<div class="moz-cite-prefix">On 03.05.13 04:57, Sean McBeth wrote:<br>
</div>
<blockquote
cite="mid:CAJgfiauT5cFMD2ZGd-2Pr163C6CPRtNiQPvWbzs_56GrqmjUAg@mail.gmail.com"
type="cite">
<p dir="ltr">I think I get it just from reading it (in bed, on
the phone, annoying the wife). I had tried to do almost this
very thing with datum->syntax at one point, but I had put the
quotesyntax on datum->syntax, not on id directly. I don't
understand why that would make a difference, it seems like it is
similar to doing (list 'a 'b) instead of '(a b)... oh, nope, now
I get it. It specifically *is* similar, except my second example
should have been '(list a b).</p>
<div class="gmail_quote">On May 2, 2013 10:38 PM, "Sean McBeth"
<<a moz-do-not-send="true"
href="mailto:sean.mcbeth@gmail.com">sean.mcbeth@gmail.com</a>>
wrote:<br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<p dir="ltr">Aaah, man. Thanks. Shutdown the pc for the night
so u will try tomorrow.</p>
<div class="gmail_quote">On May 2, 2013 10:35 PM, "Jay
McCarthy" <<a moz-do-not-send="true"
href="mailto:jay.mccarthy@gmail.com" target="_blank">jay.mccarthy@gmail.com</a>>
wrote:<br type="attribution">
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
You were close to what you want. Here's a version with a
nice utility<br>
and then the underlying machinery that makes it:<br>
<br>
#lang racket<br>
(require (for-syntax racket/syntax))<br>
<br>
(define-syntax (double-define stx)<br>
(syntax-case stx (double-define)<br>
[(_ id val1 val2)<br>
(with-syntax ([id-1 (format-id #'id "~a-1" #'id)]<br>
[id-2 (datum->syntax<br>
#'id<br>
(string->symbol<br>
(format "~a-2"<br>
(syntax->datum<br>
#'id))))])<br>
#'(begin (define id-1 val1)<br>
(define id-2 val2)))]))<br>
<br>
(double-define id 3 7)<br>
(displayln id-1)<br>
(displayln id-2)<br>
<br>
On Thu, May 2, 2013 at 8:29 PM, Sean McBeth <<a
moz-do-not-send="true"
href="mailto:sean.mcbeth@gmail.com" target="_blank">sean.mcbeth@gmail.com</a>>
wrote:<br>
> Hi there!<br>
><br>
> I'm pretty new to Racket, though not the basic
concepts of functional<br>
> programming [1] Maybe I don't need macros here at
all, but it seemed like<br>
> the right sort of lever when I first started, but now
I'm pretty stuck[2]<br>
> and I don't understand enough about the macro system
yet to be able to<br>
> figure this out.<br>
><br>
> Basically, I'm trying to make a database migration
tool + relational mapper.<br>
> I'd like to be able to define my tables in an
abbreviated Racket syntax and<br>
> use the definition to generate everything from the
create-table SQL scripts,<br>
> a few, basic CRUD-scripts-for-all-columns to structs
that will mirror a full<br>
> table row when processing the query results.<br>
><br>
> Right now, the table definition looks like this:<br>
><br>
> (define-table tickets get-all-tickets<br>
> ([ticket_id serial primary-key]<br>
> [priority int nullable] ;; I believe in making
not-null the default case<br>
> [description (varchar max)]<br>
> [finished_on datetime (default "9999-12-31
23:59:59.999")])<br>
><br>
> And this is pretty easy to parse into some "table"
structs that describe<br>
> everything fairly sufficiently[3]:<br>
> <a moz-do-not-send="true"
href="https://gist.github.com/capnmidnight/5506674"
target="_blank">https://gist.github.com/capnmidnight/5506674</a><br>
><br>
> Now, my sticking point is that I don't want to have
explicitly define that<br>
> "get-all-tickets" identifier. I notice that, in my
creating the "column"<br>
> struct, I've received a number of procedures for the
constructor and field<br>
> accessors, all given a prefix of "column" for their
identifier. So at first<br>
> glance, it seems like there are forms like struct
that are capable of<br>
> dynamically defining identifiers.<br>
><br>
> So, I stepped into the definition for struct and
tried to make sense of it,<br>
> but the best I could figure out was that struct used
syntax-case instead of<br>
> syntax-rules. It was a bit of a hair-ball for me, I
couldn't suss out the<br>
> cross references, and at least at this late of an
hour I'm having trouble<br>
> understanding the documentation on syntax-case.<br>
><br>
> Specifically, I tried to do something like:<br>
><br>
> (define-syntax (double-define stx)<br>
> (syntax-case stx (double-define)<br>
> [(_ id val1 val2)<br>
> #`(begin (define id-1 val1)<br>
> (define id-2 val2))]))<br>
><br>
> (double-define id 3 7)<br>
> (displayln id-1) ;; error "id-1 unbound identifier"<br>
> (displayln id-2)<br>
><br>
> I then tried something like:<br>
><br>
> (define-syntax (double-define stx)<br>
> (syntax-case stx (double-define)<br>
> [(_ id val1 val2)<br>
> (with-syntax ([id-1 #'(string->symbol (format
"~a-1" id))] ;; error<br>
> "define: not an identifier, identifier with default,
or keyword for<br>
> procedure argument"<br>
> [id-2 #'(string->symbol (format
"~a-2" id))])<br>
> #'(begin (define id-1 val1)<br>
> (define id-2 val2)))]))<br>
><br>
> (double-define id 3 7)<br>
> (displayln id-1)<br>
> (displayln id-2)<br>
><br>
> Clearly, not correct.<br>
><br>
> I could make the table struct into a table class and
then just define a<br>
> get-all method that does what I want, but that kind
of feels like giving up<br>
> and I'm more interested in using this to learn more
about using macros, as<br>
> it has already done for me.<br>
><br>
><br>
><br>
> [1] Functional C# is something of a job safety
program of mine :P<br>
><br>
> [2] i.e. been banging my head against the desk for
the last 6 hours. I have<br>
> gotten pretty comfortable with syntax-rules though,
so it wasn't a complete<br>
> waste.<br>
><br>
> [3] This isn't the final form, but I'm just pushing
some code around to try<br>
> to get the basic concepts working. For example, the
get-all-tickets<br>
> procedure wouldn't just return the query, it'd
eventually execute it and<br>
> return the results.<br>
><br>
> ____________________<br>
> Racket Users list:<br>
> <a moz-do-not-send="true"
href="http://lists.racket-lang.org/users"
target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
<br>
<br>
<br>
--<br>
Jay McCarthy <<a moz-do-not-send="true"
href="mailto:jay@cs.byu.edu" target="_blank">jay@cs.byu.edu</a>><br>
Assistant Professor / Brigham Young University<br>
<a moz-do-not-send="true"
href="http://faculty.cs.byu.edu/%7Ejay" target="_blank">http://faculty.cs.byu.edu/~jay</a><br>
<br>
"The glory of God is Intelligence" - D&C 93<br>
</blockquote>
</div>
</blockquote>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">____________________
Racket Users list:
<a class="moz-txt-link-freetext" href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a>
</pre>
</blockquote>
<br>
</body>
</html>