Skip to content

Instantly share code, notes, and snippets.

@hww
Last active June 26, 2022 16:05
Show Gist options
  • Save hww/c3580c40d3c944c36f2aaa49d8bfa3d9 to your computer and use it in GitHub Desktop.
Save hww/c3580c40d3c944c36f2aaa49d8bfa3d9 to your computer and use it in GitHub Desktop.
Racket - Build the function name in the macro
#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