PasteRack.org
Paste # 31329
2020-12-03 05:13:29

Forked from paste # 82059.

Fork as a new paste.

Paste viewed 600 times.


Embed:

defines

  1. #lang racket/base
  2.  
  3. (require (for-syntax racket/base
  4.                      syntax/parse))
  5.  
  6. (define-syntax (demo stx)
  7.   (syntax-parse stx
  8.     [(_ the-one:expr the-other:expr)
  9.      #'(begin
  10.          (define (the-first) the-one)
  11.          (define (the-second) the-other))]))
  12.  
  13. (demo 1 2)
  14.  
  15.  
  16. (define-syntax (demo-unhygienic stx)
  17.   (syntax-parse stx
  18.     ((_ the-one:expr the-other:expr)
  19.      (with-syntax
  20.        ((the-first (datum->syntax stx 'the-first))
  21.         (the-second (datum->syntax stx 'the-second)))
  22.        #'(begin
  23.            (define (the-first) the-one)
  24.            (define (the-second) the-other))))))
  25.  
  26. (demo-unhygienic 3 4)
  27. (displayln (the-first))
  28. (displayln (the-second))
  29.  
  30.  
  31. (define-syntax (demo-hygienic stx)
  32.   (syntax-parse stx
  33.     [(_ the-first-name:id the-second-name:id the-one:expr the-other:expr)
  34.      #'(begin
  35.          (define (the-first-name) the-one)
  36.          (define (the-second-name) the-other))]))
  37.  
  38. (demo-hygienic the-first-one the-second-one 5 6)
  39.  
  40. (displayln (the-first-one))
  41. (displayln (the-second-one))

=>

3

4

5

6