[racket] Splicing a variable to serve as let declarations

From: Alexander D. Knauth (alexander at knauth.org)
Date: Thu Dec 11 15:10:44 EST 2014

Or you don’t even need a syntax-parameter:
#lang racket
(require (for-syntax syntax/parse))
(define-syntax letexpand
 (syntax-parser
   [(_ alst body ...)
    (with-syntax ([alst (syntax-local-introduce #`#,(syntax-local-value #'alst))])
      #'(let alst body ...))]))
(define (foo)
  (define-syntax dave '((is 5) (fat 6)))
  (letexpand dave is fat))
(foo)

On Dec 11, 2014, at 6:52 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:

> #lang racket
> (require (for-syntax syntax/parse) racket/stxparam)
> 
> (define-syntax letexpand
>   (syntax-parser
>   [(_ alst body ...)
>     (with-syntax ([alst (syntax-local-introduce #`#,(syntax-parameter-value #'alst))])
>         #'(let alst body ...))]))
> 
> (define (foo)
>     (define-syntax-parameter dave '((is 4) (fat 6)))
>     (letexpand dave is fat))
> 
> (foo)
> 
> 
> 
> 
> Tue, 9 Dec 2014 06:33:14 +0200 от J Arcane <jarcane at gmail.com>:
> Hmm. That does appear to work at a global level, but then when you attempt to use it inside of a function it returns "identifier used out of context". Trying to define-for-syntax in a local context also causes problems; "begin-for-syntax: not in a definition context in: (begin-for-syntax (define-values (z) lst))"
> 
> On Mon, Dec 8, 2014 at 10:17 PM, Alexander D. Knauth <alexander at knauth.org> wrote:
> Would something like this work for what you want?
> 
> #lang racket
> (require (for-syntax syntax/parse))
> (define-for-syntax dave '((is 5) (fat 6)))
> (define-syntax letexpand
>   (syntax-parser
>     [(_ alst body ...)
>      (with-syntax ([alst (syntax-local-introduce #`#,(eval-syntax #'alst))])
>        #'(let alst body ...))]))
> (letexpand dave is fat)
> 
> On Dec 8, 2014, at 2:46 AM, J Arcane <jarcane at gmail.com> wrote:
> 
> > I've been experimenting with a new feature for Heresy, and I find myself in need of a macro that can insert the contents of an a-list into a let syntax to serve as its definitions. I am utterly failing to accomplish this. It seems like it should be simple enough to splice a variable's contents into the syntax, and in a sense it is, but not if I want it to actually then evaluate.
> >
> > For example, I can do this:
> >
> > (define dave '((is 5) (fat 6)))
> >
> > (define-syntax letexpand
> >   (syntax-rules ()
> >     [(_ alst body ...)
> >      #`(let #,alst body ...)]))
> >
> > (letexpand dave is fat)
> >
> > Which returns a syntax object which looks like what I want:
> >
> > #<syntax:...:8:7 (let ((is 5) (fat 6)) is fat)>
> >
> > But doesn't actually the evaluate that object. But doing that with merely (let alst body ...) creates an error because rather than splicing alst's contents itself, it merely inserts the variable name given, and then fails because let doesn't recognize it. It will work with a handwritten unquoted alist in that space, but this is largely useless because of course that's what let already basically does anyway.
> >
> > Am I starting in the wrong place here, and if not, how do I turn that syntax object into something that actually evaluates?
> > ____________________
> >  Racket Users list:
> >  http://lists.racket-lang.org/users
> 
> 
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
> 
> 
> 
> -- 
> Roman Klochkov

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141211/0e9c5c60/attachment.html>

Posted on the users mailing list.