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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:52:36+00:00 2026-06-12T16:52:36+00:00

Common Lisp macros typically use included-prefix notation: (operator stuff…) However, the special quote macro

  • 0

Common Lisp macros typically use included-prefix notation: (operator stuff…)

However, the special quote macro ‘ uses concatenated-prefix notation: operator stuff , or alternatively operator(stuff).

I would like to create a custom macro in SBCL Common Lisp, let’s call it !, that uses concatenation-prefix syntax to operate on a following list (or even atom), in a similar manner to ‘ . So I could invoke it anywhere, e.g. (setq foo !(bar) ), without it being infixed in parentheses itself.

How can this be done? What would the defmacro syntax look like? Thanks.

  • 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-06-12T16:52:37+00:00Added an answer on June 12, 2026 at 4:52 pm

    I see 2 ways to do it.

    The first, simple variant is suitable, if you need it only for a single or very limited number of cases and you’re satisfied with using just a single symbol as a prefix (like ! in the example case). You can create a read-macro, more specifically set a macro-character to substitute such operator for, say, addition:

    (set-macro-character #\! (lambda (stream char)
                               (declare (ignore char))
                               (cons '+ (let ((next (read stream t nil t)))
                                     (if (consp next) next
                                         (list next)))))
    CL-USER> !1
    1
    CL-USER> !(1 2)
    3
    

    The other, more complex and more flexible way is to define a macro, inside which apply a custom transformation, that will turn concatenated-prefix code into included-prefix. This trick uses the fact, that ordinary Lisp reader will read the in the same way both foo(bar) and foo (bar), i.e. it will split them into 2 elements.

    The simple version of such macro may look like this:

    (defmacro with-prefix-syntax (&body body)
      `(progn ,@(loop :for tail :on body :while body
                      :collect (if (and (not (atom (second tail)))
                                        (fboundp (first tail)))
                                   (prog1 (cons (first tail) (second tail)
                                     (setf tail (rest tail)))
                                   (first tail)))))
    

    It will transform only top-level forms:

    CL-USER> (macroexpand-1 '(with-prefix-syntax
                               print(1)))
    (PROGN (PRINT 1))
    CL-USER> (macroexpand-1 '(with-prefix-syntax
                               1))
    (PROGN 1)
    CL-USER> (macroexpand-1 '(with-prefix-syntax
                               print(1)
                               2))
    (PROGN (PRINT 1) 2)
    

    But not work in the lower levels:

    CL-USER> (macroexpand-1 '(with-prefix-syntax
                               print(1)
                               (+ print(2))))
    (PROGN (PRINT 1) (+ PRINT (2)))
    

    Though it’s rather easy to make it recursively transform all the layers (which is left as an exercise to the reader 🙂

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

Sidebar

Related Questions

In Common Lisp (SBCL 1.0.58) why does the macro OR use a gensym, but
In Common LISP you can do the following (macro lambda (x) (list (quote car)
It seems that Common Lisp does treat (list 'quote 'x) in a special way.
I have been writing Common Lisp macros, so Scheme's R5Rs macros are a bit
When writing Common Lisp code, I use SLIME. In particular, I compile the buffer
Why would anyone prefer Scheme macros over Common Lisp macros (and I genuinely want
In Common Lisp you use the (null x) function to check for empty lists
In Common Lisp package definition, what is the difference between (defpackage #:foo (:use :cl)
In common lisp, we can use the remove function. It seems there is no
I'm reading Paul Graham's ANSI Common Lisp . In the chapter about macros he

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.