Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 0.96 KB | None | 0 0
  1. ;; '(case s [x e] ...)
  2. ;; ==>
  3. ;; '(let ([tmp s]) (cond [(symbol=? tmp 'x) e] ...))
  4. (define (case-transformer s)
  5.   (match s
  6.     [`(case ,subj [,sym* ,bdy*] ...)
  7.      (let ([tmp (fresh! 'tmp)])
  8.        `(let ([,tmp ,subj])
  9.           (cond
  10.             ,@(map (λ (sym bdy)
  11.                      `[(symbol=? ,tmp ',sym) ,bdy])
  12.                    sym*
  13.                    bdy*))))]
  14.  
  15.     [_ (error 'case-transformer "malformed case: ~e" s)]))
  16.  
  17. #;; in regular racket:
  18. (define-syntax (case stx)
  19.   (syntax-case stx ()
  20.     [(case subj [sym bdy] ...)
  21.      ;; note: the syntax/parse library has a more convenient
  22.      ;; form for with-syntax
  23.      (with-syntax ([tmp (generate-temporary #'subj)])
  24.        ;; "pattern variables" (subj,sym,bdy,tmp) are automatically
  25.        ;; substituted and cant be used outside of syntax quote #'
  26.        #'(let ([tmp subj])
  27.            (cond
  28.              ;; these ellipsis take care of mapping
  29.              [(symbol=? tmp 'sym) bdy] ...)))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement