问题
Is there a quicker way to write the following code?
(list var1 var2 var3 var4 var5 ... var100)
Basically all the elements are variables with prefix "var".
回答1:
You can do this with a macro:
#lang racket
(require syntax/parse/define (for-syntax racket/syntax))
(define-simple-macro (varN-range var:id arg:number ...)
#:with [varN ...]
(for/list ([N (apply in-range (syntax->datum #'(arg ...)))])
(format-id #'var "~a~a" #'var N))
(list varN ...))
Using it:
> (define var1 "a")
> (define var2 "b")
> (define var3 "c")
> (define var4 "d")
> (define var5 "e")
> (varN-range var 1 6)
(list "a" "b" "c" "d" "e")
来源:https://stackoverflow.com/questions/63120915/filling-a-list-with-a-series-of-known-elements