Last active
June 26, 2022 16:05
-
-
Save hww/c3580c40d3c944c36f2aaa49d8bfa3d9 to your computer and use it in GitHub Desktop.
Racket - Build the function name in the macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require (for-syntax racket/base)) | |
(require (for-syntax racket/syntax racket/base syntax/parse)) | |
(require racket/syntax) | |
;; /////////////////////////////////////////////// | |
;; In case if the argumens are anything but not ID | |
;; /////////////////////////////////////////////// | |
(define (get-1) 100) | |
(define (get-2) 200) | |
(define-syntax (KK stx) | |
(syntax-parse stx | |
[(_ arg0 ...) | |
(define get-args (for/list ([arg (syntax->list #'(arg0 ...))]) (format-id stx "get-~a" (syntax->datum arg)))) | |
(with-syntax (((get-arg ...) get-args)) | |
(syntax/loc stx | |
(begin | |
(get-arg) ...)))])) | |
(KK 1 2) | |
> 100 | |
> 200 | |
;; //////////////////////////////// | |
;; In case if the arguments are IDs | |
;; //////////////////////////////// | |
(define (get-a) 100) | |
(define (get-b) 200) | |
(define-syntax (KK stx) | |
(syntax-parse stx | |
[(_ arg0 ...) | |
(define get-args (for/list ([arg (syntax->list #'(arg0 ...))]) (format-id stx "get-~a" arg))) | |
(with-syntax (((get-arg ...) get-args)) | |
(syntax/loc stx | |
(begin | |
(get-arg) ...)))])) | |
(KK a b) | |
> 100 | |
> 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment