[racket] macro question

From: Sean Kanaley (skanaley at gmail.com)
Date: Thu Oct 2 21:51:40 EDT 2014

(let ([x y])
  x)
=> y

That "let" is just a wrapper that equals y. So your macro is really

(define-syntax (mylet stx)
  (syntax-case stx ()
    [(_ x) #'x]
    [(_ x xs ...) #'(cons x (mylet xs ...))])])

Which causes
(mylet 1 2 3) => '(1 2 . 3) ;don't forget the dot!

And that's just the macro version of a normal recursive function

(define (mylet . xs)
  (match xs
    [(list x) x]
    [(list x xs ...) (cons x (apply mylet xs))]))

So nothing macro-y has really taken place. If you are attempting to
redefine let, you'll have to place any binding forms inside of the output
syntax, right of the sharp quote. The runtime code that the macro expands
into has to bind something, in other words. Your original version binds a
value during the expansion of the output code, where the output code is
just the function I have above. I hope that makes sense.


On Thu, Oct 2, 2014 at 9:19 PM, Kevin Forchione <lysseus at gmail.com> wrote:

> Hi guys,
> Why does this appear to work? I assume it’s not a recommended approach.
>
> #lang racket
>
> (require (for-syntax syntax/parse))
>
> (define-syntax (mylet stx)
>   (let ([val (syntax-case stx ()
>                [(_ x) #'x]
>                [(_ x xs ...) #'(cons x (mylet xs ...))])])
>     val))
>
> (mylet 1 2 3) => ‘(1 2 3)
>
> -Kevin
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141002/9828e620/attachment-0001.html>

Posted on the users mailing list.