1
votes

I am trying to define symbols to themselves in quote. I can do this define by define like so:

#lang racket
(define a 'a)
(define b 'b)

But if I want to do this for a lot of values how would I go about it? I know there is define-values but how can I extract the variable names from a list?

(define vs '(a b))
(define-values ??? (apply values vs))

EDIT: soegaard's answer works well. As a follow-up, how would you do the same for define-syntax-rule (or an equivalent using other macro definition functions) i.e. have a be replaced by 'a on a syntax level?

1

1 Answers

3
votes

The macro below will turn

(define-symbols a b c)

into

(begin
  (define a 'a)
  (define b 'b)
  (define c 'c))

The macro:

#lang racket
(require (for-syntax syntax/parse))

(define-syntax (define-symbols stx)
  (syntax-parse stx
    [(_define-symbols id:id ...)
     (syntax/loc stx
       (begin
         (define id 'id)
         ...))]
    [_ (raise-syntax-error 'define-symbols
                           "Expected (define-symbols <identifier> ...)"
                           stx)]))

(define-symbols a b c)
(list a b c a b c)

EDIT

Here is a syntax-rules version:

(define-syntax define-symbols
  (syntax-rules ()
    [(_define-symbols id ...)
     (begin
       (define id 'id)
       ...)]))

Note however that this version does not check that the id's are identifiers.

For example (define-symbols 3) will lead to a misleading error message.