[racket] datum<->syntax with same context, yet "unbound identifier"

From: Greg Hendershott (greghendershott at gmail.com)
Date: Wed Nov 21 00:39:28 EST 2012

I've been struggling to get something working. Jon and Asumu leant me
a hand on #racket. I learned some ways to use patterns instead of
resorting to datum->syntax, which I can use in the future. But for
what I'm doing now (I need to sort arguments for a lambda) I don't see
any way around running things through syntax->datum and datum->syntax.
However I seem to be fudging lexical context.

The original is using syntax-parse to chunk up arg specs and so on.
But, here is a greatly-distilled version:

#lang racket

(require (for-syntax racket/list))

(define-syntax (lam stx)
  (syntax-case stx ()
    [(_ args body ...)
     #`(lambda #,(datum->syntax #'args (append (syntax->datum #'args)))
         body ...)]))

;; Use `lam'
(define f1
  (lam (x y)
    (list x y)))
(f1 1 0)
;; => '(1 0)
;; Good.

;; Now a variation:
(define-syntax (expand-to-lam stx)
  (syntax-case stx ()
    [(_ (name arg ...) body ...)
     #'(lam (arg ...) body ...)]))

;; Use it
(expand-to-lam (f2 x y)
  (list x y))
;; =>  x: unbound identifier in module
;;       in: x
;; Huh?


Is there something else I should do instead, or is this possibly a bug?

Posted on the users mailing list.