[racket] macro vs function differing results

From: Kevin Forchione (lysseus at gmail.com)
Date: Fri Feb 7 10:37:04 EST 2014

I’m trying to resolve why I get differing results in a let form when using a macro versus using the expanded function version. Here’s an example using racket 5.93:

#lang racket

(require (for-syntax racket/syntax))

(define *cont* identity)

(define-syntax (=defun stx)
  (syntax-case stx ()
    [(_ (name parm ...) body ...)
     (with-syntax ([f (format-id stx "=~a" #'name)]
                   [*cont* (format-id stx "~a" '*cont*)])
       #'(begin
           (define-syntax-rule (name parm ...)
             (f *cont* parm ...))
           (define (f *cont* parm ...) body ...)))]))

(=defun (foo x) *cont*)

(let ([*cont* cdr])
    (foo 10))
=> #<procedure:identity>

(let ([*cont* cdr])
    (=foo *cont* 10))
=> #<procedure:cdr>

Why does the macro return identity, while the function returns cdd?

Thanks!

Posted on the users mailing list.