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 6249613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:16:18+00:00 2026-05-24T13:16:18+00:00

This seems to work, it’s a macro that expands to successive integers depending on

  • 0

This seems to work, it’s a macro that expands to successive integers depending on how many times it has been expanded.

;; Library (test macro-state)
(library
 (test macro-state)
 (export get-count incr-count)
 (import (rnrs))

 (define *count* 0)
 (define (get-count) *count*)
 (define (incr-count) (set! *count* (+ *count* 1)))

 )

;; Program
(import (rnrs) (for (test macro-state) expand))

(define-syntax m
  (lambda (x)
    (syntax-case x ()
      ((m) (begin (incr-count) (datum->syntax #'m (get-count)))))))

(write (list (m) (m) (m)))
(newline)
;; prints (1 2 3)

But it’s clumsy to me because the macro state *count* and the macro m itself are in different modules. Is there a better way to do this in r6rs, preferably one that doesn’t split the implementation over two modules?

EDIT

I should make it clear that although this example is just a single macro, in reality I’m looking for a method that works when multiple macros need to share state.

  • 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-24T13:16:19+00:00Added an answer on May 24, 2026 at 1:16 pm

    You can make the state local to the macro transformer:

    (define-syntax m
      (let ()
        (define *count* 0)
        (define (get-count) *count*)
        (define (incr-count) (set! *count* (+ *count* 1)))
        (lambda (x)
          (syntax-case x ()
            ((m) (begin (incr-count) (datum->syntax #'m (get-count))))))))
    

    Edited to add: In Racket, you can also do this:

    (begin-for-syntax
      (define *count* 0)
      (define (get-count) *count*)
      (define (incr-count) (set! *count* (+ *count* 1))))
    (define-syntax m
      (lambda (x)
        (syntax-case x ()
          ((m) (begin (incr-count) (datum->syntax #'m (get-count)))))))
    

    But I don’t think R6RS has anything that corresponds to begin-for-syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just discovered, quite by accident, that this seems to work: Public Interface Ix
This seems to work fine in C#: class A : System.Attribute { public A()
This expression seems to work: gci . | % { gc $_} This also
Getting friends of friend are pretty easy, I got this which seems to work
This seems in my head like it should work but I cant figure out
Hi this seems like it should work, from something in collectionofsomestuff select new SelectListItem(){Text
This seems to happen all the time to me: I work on the parent
This seems painfully simple, but I can't work out how to do it: I
So this seems pretty basic but I can't get it to work. I have
Ok this seems so silly but I'm having some trouble getting this to work.

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.