Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6177053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:12:21+00:00 2026-05-24T00:12:21+00:00

In order to teach myself more advanced macros in racket, I set about creating

  • 0

In order to teach myself more advanced macros in racket, I set about creating a macro to increment a field in a mutable struct:


(increment! instance name field)

=>

(set-name-field instance (get-name-field instance))

I produced a macro that works, and decided it would be useful to share between multiple modules. Unfortunately, since the structure mutators are not in the scope of the module defining the macro, an expansion error occurs.

The following is a contrived example demonstrating the problem. I would like to know:

  1. Did I write the macro code in idiomatic-racket style? Is this the correct approach?

  2. How can I control the expansion of the macro, so that it operates in the presence of identifiers not found in it’s original context?

Thanks.

#lang racket/load

(module util racket

  (define-syntax increment!
    (lambda (stx)
      (syntax-case stx ()
        [(increment! s sn fn i)
         (with-syntax 
             ([set! (string->symbol 
                     (format "set-~a-~a!" (syntax-e #'sn) (syntax-e #'fn)))]
              [get (string->symbol 
                    (format "~a-~a" (syntax-e #'sn) (syntax-e #'fn)))])
           #'(set! s (+ i (get s))))]
        ;; default increment of 1
        [(increment! s sn fn) #'(increment! s sn fn 1)])))

  (provide increment!)
  )

(module bank racket
  (require 'util)
  (struct money (dollars pounds euros) #:mutable #:transparent)

  (let ([m (money 0 50 20)])
    (increment! m money pounds 100)
    (increment! m money dollars)
    m)
  )

(require 'bank)

Results in

expand: unbound identifier in module in: set-money-pounds!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:12:22+00:00Added an answer on May 24, 2026 at 12:12 am

    You can’t do just that. The problem is that you’re generating symbols with the correct names, but you just return the symbols as-is, which means that with-syntax gives them some default (and wrong) lexical context. Instead, you should use datum->syntax and give it the correct context.

    Below is a revision of your code that works as you expect. To see more about it, see my recent blog post on the subject of unhygienic macros.

    However, this is not a robust solution. What happens when the setters and getters have different names? A more robust solution would be to use the struct name and extract the right information from it (at syntax time, in the macro) — see the manual for details on this. It’s also good to ask questions about it on the mailing list, since there might be a better way to get what you want, or a better solution if you’re looking for some dot-notation-like functionality.

    #lang racket/load
    
    (module util racket
      (define-syntax increment!
        (lambda (stx)
          (syntax-case stx ()
            [(increment! s sn fn i)
             (let ([id (lambda (fmt)
                         (let ([str (format fmt (syntax-e #'sn) (syntax-e #'fn))])
                           (datum->syntax #'sn (string->symbol str))))])
               (with-syntax ([set! (id "set-~a-~a!")]
                             [get (id "~a-~a")])
                 #'(set! s (+ i (get s)))))]
            ;; default increment of 1
            [(increment! s sn fn) #'(increment! s sn fn 1)])))
      (provide increment!))
    
    (module bank racket
      (require 'util)
      (struct money (dollars pounds euros) #:mutable #:transparent)
      (let ([m (money 0 50 20)])
        (increment! m money pounds 100)
        (increment! m money dollars)
        m))
    
    (require 'bank)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a little fitness tracker in order to teach myself Django. I
So I've been trying to teach myself to use VBOs, in order to boost
I'm basically trying to teach myself how to code and I want to follow
To teach myself Javascript, I'm trying to make a web page that gives users
I've been trying to teach myself to create and deploy Django apps. I've created
I am trying to teach myself Lucene.Net to implement on my site. I understand
I'm just starting to teach myself Clojure. As part of supplementing my studies I've
i am trying to teach myself SQL and of course I would like to
I have been trying to teach myself Lambdaj and it doesn't seem to want
I'm struggling trying to teach myself Mockito. Consider the following method, hasInventory() , which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.