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

  • Home
  • SEARCH
  • 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 452703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:05:57+00:00 2026-05-12T22:05:57+00:00

Dear all, I now have a preliminary macro (defmacro key-if(test &key then else) `(cond

  • 0

Dear all, I now have a preliminary macro

(defmacro key-if(test &key then else)
  `(cond (,test
          ,then)
         (t,else)))

and it is now correctly working as

> (key-if (> 3 1) :then 'ok)

OK

> (key-if (< 5 3) :else 'ok)

OK

> (key-if (> 3 1) :else 'oops)

NIL

> (key-if (> 3 1) :else 'oops :then 'ok)

OK

Now I want to extend it a bit, which means I want to have arbitrary number of arguments followed by :then or :else (the keyword), so it will work like

> (key-if (> 3 1) :then)

NIL

> (key-if (> 3 1) :else 'oops :then (print 'hi) 'ok)

HI

OK

so I am now stuck on this point, I am a bit new to Lisp macros. I could think of using &rest for this extension, but don’t know how, so I really need your idea on how to get this extension to work.

Thanks a lot.

  • 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-12T22:05:57+00:00Added an answer on May 12, 2026 at 10:05 pm

    I assume that you are using some Common Lisp implementation.

    That style of argument parsing is not directly supported by the standard lambda lists used by DEFMACRO. You are right in thinking that you will have to parse the arguments yourself (you could use (test &rest keys-and-forms) to capture TEST, but extracting the :ELSE and :THEN parts would be up to you.

    I am no super-(Common-)Lisper, but the syntax you are inventing here seems very non-idiomatic. The first hint is that macro lambda lists do not support what you want. Also, there are already standard alternatives that are the same length or shorter in raw characters typed (the typing overhead can be reduced a bit by using a structure editor like paredit in an Emacs).

    (key-if blah :then foo bar)                       ; multiple then, no else
    (when blah foo bar)                               ; standard
    
    (key-if blah :else baz quux)                      ; no then, multiple else
    (unless blah baz quux)                            ; standard
    
    (key-if blah :then foo :else baz quux)            ; one then, multiple else
    (if blah foo (progn baz quux))                    ; standard
    (cond (blah foo) (t baz quux))
    
    (key-if blah :then foo bar :else baz)             ; multiple then, one else
    (if blah (progn foo bar) baz)                     ; standard
    (cond (blah foo bar) (t baz))
    
    (key-if blah :then foo bar :else baz quux)        ; multiple then, multiple else
    (if blah (progn foo bar) (progn baz quux))        ; standard
    (cond (blah foo bar) (t baz quux))                ; even shorter
    
    (key-if blah :else baz quux :then foo bar)        ; multiple else before multiple then
    (if (not blah) (progn baz quux) (progn foo bar))  ; only standard variation that is longer
    (cond ((not blah) baz quux) (t foo bar)           ; standard, shorter
    

    Common Lisp macros are very powerful code templating engines, but this particular usage makes me think that you are just not quite comfortable with the standard styles and idioms in Common Lisp. No matter which language you are using, it is almost always a good idea to “go with the flow” by adopting the ‘local’ styles and idioms instead of bringing styles and idioms from other languages with which you are more familiar.

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

Sidebar

Ask A Question

Stats

  • Questions 234k
  • Answers 234k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Manually changing the files using install_name_tool install_name_tool -change "@loader_path/libeng.dylib" "/Applications/MATLAB_R2009b.app/bin/maci64/libeng.dylib"… May 13, 2026 at 6:04 am
  • Editorial Team
    Editorial Team added an answer RtlCopyMemory is provided inline. It is defined in winnt.h as… May 13, 2026 at 6:04 am
  • Editorial Team
    Editorial Team added an answer It seems that the community has pulled back from Ferret… May 13, 2026 at 6:04 am

Related Questions

I am not so proficient in TSql as of now (writing since last 4/5
Dear all: In advance, thank you for your time. Lately, I have decided to
Exact Duplicate of: Getting the size (free,total) of a Windows Mobile phone drive using
I'd like to have your opinion on a specific case, please. This is about

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.