[racket] iteration for user-defined data structures
Marijn,
The define-sequence-syntax form is for writing sequences such as
in-list and in-vector. Adding a syntax binding in addition to the
value binding allows constructs like for/list to inline the code
produced by the macro, rather than calling out to an external
procedure.
The for/fold/derived form is just a copy of for/fold; the only
difference is that it takes the original syntax of a derived macro as
an extra argument and reports all syntax errors in terms of that. To
write for/dlist, I suggest first just writing a macro like you would
write any other macro, and use the normal for/fold form. You can
later improve your macro's error messages by adapting it to
for/fold/derived.
The documentation could probably use some examples of those.
Carl Eastlund
On Wed, Jun 1, 2011 at 8:40 AM, Marijn <hkBst at gentoo.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> I'm working on a doubly-linked list (dlist) implementation and have been
> able to succesfully implement the prop:sequence #:property, allowing the
> use of my data structure as a sequence, allowing for example to convert
> to an ordinary list with `for/list'. I'm trying to now do the converse,
> that is implement my own `for/dlist' for constructing dlists from
> sequences, but I am having some trouble with the docs.
>
> Specifically, in the reference there is "2.18.2 Deriving New Iteration
> Forms"[1], which mentions `for/fold/derived' and
> `define-sequence-syntax', but doesn't really explain what they are for
> or how they should be used. The last time I thought I understood how to
> proceed I tried to do:
>
> (define (for/dlist
> (for/fold/derived for/dlist
>
> after fruitlessly trying to complete:
>
> (define-sequence-syntax for/dlist
> ...
> (for/fold/derived for/dlist
>
> but clearly I have no clue how this is supposed to work and I am
> currently not even sure that these constructs are there for supporting
> what I want to do. Some help would be highly appreciated.
>
> Some of the things that 2.18.2 does say, are worded rather poorly in my
> opinion, so for my part I will present some rewordings that I hope are
> more direct and clearer:
>
> Old paragraphs:
> Defines id as syntax. An (id . rest) form is treated specially when used
> to generate a sequence in a clause of for (or one of its variants). In
> that case, the procedure result of clause-transform-expr is called to
> transform the clause.
> When id is used in any other expression position, the result of
> expr-transform-expr is used. If it is a procedure of zero arguments,
> then the result must be an identifier other-id, and any use of id is
> converted to a use of other-id. Otherwise,expr-transform-expr must
> produce a procedure (of one argument) that is used as a macro transformer.
>
> Proposed replacement paragraphs:
> Defines id as syntax that is treated specially when used in/as
> [whichever is correct] a clause of an iteration (for, for/list, ...). In
> this special case, clause-transform-expr should evaluate to a syntax
> transformer (a procedure that takes a single syntax object argument and
> returns another) which is then called in the place of id except when it
> would return #f.
> When id is not used in a clause of an iteration, expr-transform-expr
> should either evaluate to a syntax transformer which is called in the
> place of id,
> or a thunk which returns an identifier other-id, which is used to
> convert each use of id to other-id.
>
> My understanding of the rest of that subsection is yet too low to
> attempt a rewrite.
>
> Marijn
>
> [1]:http://docs.racket-lang.org/reference/for.html?q=for/hash#(part._.Deriving_.New_.Iteration_.Forms)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.17 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk3mM08ACgkQp/VmCx0OL2xeEgCgxxhcuxs5YwvKEGnekESb7y9k
> 5t8An1743I1PHw1lxfpPc36kV4p+xl3k
> =xsSP
> -----END PGP SIGNATURE-----
> _________________________________________________
> For list-related administrative tasks:
> http://lists.racket-lang.org/listinfo/users
>
>