Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; '(case s [x e] ...)
- ;; ==>
- ;; '(let ([tmp s]) (cond [(symbol=? tmp 'x) e] ...))
- (define (case-transformer s)
- (match s
- [`(case ,subj [,sym* ,bdy*] ...)
- (let ([tmp (fresh! 'tmp)])
- `(let ([,tmp ,subj])
- (cond
- ,@(map (λ (sym bdy)
- `[(symbol=? ,tmp ',sym) ,bdy])
- sym*
- bdy*))))]
- [_ (error 'case-transformer "malformed case: ~e" s)]))
- #;; in regular racket:
- (define-syntax (case stx)
- (syntax-case stx ()
- [(case subj [sym bdy] ...)
- ;; note: the syntax/parse library has a more convenient
- ;; form for with-syntax
- (with-syntax ([tmp (generate-temporary #'subj)])
- ;; "pattern variables" (subj,sym,bdy,tmp) are automatically
- ;; substituted and cant be used outside of syntax quote #'
- #'(let ([tmp subj])
- (cond
- ;; these ellipsis take care of mapping
- [(symbol=? tmp 'sym) bdy] ...)))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement