Skip to content

Instantly share code, notes, and snippets.

@dyoo
Created January 11, 2013 21:25
Show Gist options
  • Save dyoo/4514083 to your computer and use it in GitHub Desktop.
Save dyoo/4514083 to your computer and use it in GitHub Desktop.
#lang racket/base
(require (for-syntax racket/base))
(provide swap)
;; taken from:
;;http://docs.racket-lang.org/guide/syntax-case.html
(define-syntax swap
(lambda (stx)
(syntax-case stx ()
[(swap x y)
(if (and (identifier? #'x)
(identifier? #'y))
#'(let ([tmp x])
(set! x y)
(set! y tmp))
(raise-syntax-error #f
"not an identifier"
stx
(if (identifier? #'x)
#'y
#'x)))])))
(module* test racket/base
(require (submod "..")
rackunit)
(define x 'x)
;; A little setup for running the compiler for the run-time tests
(define-namespace-anchor anchor)
(define ns (namespace-anchor->namespace anchor))
(define-syntax-rule (try-compile expr)
(parameterize ([current-namespace ns])
(compile 'expr)))
(check-exn exn:fail:syntax?
(lambda () (try-compile (swap x 2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment