Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
macros, &REST and REMF
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sam Steingold  
View profile  
 More options Nov 29 2002, 9:11 pm
Newsgroups: comp.lang.lisp
From: Sam Steingold <s...@gnu.org>
Date: Sat, 30 Nov 2002 02:11:19 GMT
Local: Fri, Nov 29 2002 9:11 pm
Subject: macros, &REST and REMF
Is this the right idiom:

(defmacro with-foo ((&key foo) &body body)
  `(... ,foo ... ,@body))

(defmacro with-foo-bar ((&rest opts &key bar &allow-other-keys) &body body)
  (remf opts :bar)
  `(with-foo (,@opts)
     ... ,bar ... ,@body))

I.e., I want all options for WITH-FOO to be available in WITH-FOO-BAR.

thanks.

--
Sam Steingold (http://www.podval.org/~sds) running RedHat8 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
Isn't "Microsoft Works" an advertisement lie?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Nov 30 2002, 1:18 am
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Sat, 30 Nov 2002 06:15:03 GMT
Local: Sat, Nov 30 2002 1:15 am
Subject: Re: macros, &REST and REMF
In article <m365ufhkl1....@loiso.podval.org>,
Sam Steingold  <s...@gnu.org> wrote:

>Is this the right idiom:

>(defmacro with-foo ((&key foo) &body body)
>  `(... ,foo ... ,@body))

>(defmacro with-foo-bar ((&rest opts &key bar &allow-other-keys) &body body)
>  (remf opts :bar)
>  `(with-foo (,@opts)
>     ... ,bar ... ,@body))

>I.e., I want all options for WITH-FOO to be available in WITH-FOO-BAR.

It's a pretty common style.  Another possibility is:

(defmacro with-foo-bar (...)
  `(with-foo (:allow-other-keys t ,@opts)
     ... ,bar ... ,@body))

However, this prevents WITH-FOO from doing any keyword validation of the
remaining arguments.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Warnock  
View profile  
 More options Nov 30 2002, 3:58 am
Newsgroups: comp.lang.lisp
From: r...@rpw3.org (Rob Warnock)
Date: Sat, 30 Nov 2002 02:58:40 -0600
Local: Sat, Nov 30 2002 3:58 am
Subject: Re: macros, &REST and REMF
Sam Steingold  <s...@gnu.org> wrote:
+---------------
| Is this the right idiom:
| (defmacro with-foo ((&key foo) &body body)
|   `(... ,foo ... ,@body))
|
| (defmacro with-foo-bar ((&rest opts &key bar &allow-other-keys) &body body)
|   (remf opts :bar)
|   `(with-foo (,@opts)
|      ... ,bar ... ,@body))
+---------------

Side question for the group: What are the rules for a &REST arg
in macros? If they're the same as functions, then could you possibly
get in trouble with Sam's idiom due to REMF destructively modifying
the "opts" list? That is, does one really need to do this instead?

  (defmacro with-foo-bar ((&rest opts &key bar &allow-other-keys) &body body)
    (let ((mutable-opts (copy-list opts)))
      (remf mutable-opts :bar)
      `(with-foo (,@mutable-opts)
         ... ,bar ... ,@body)))

-Rob

-----
Rob Warnock, PP-ASEL-IA         <r...@rpw3.org>
627 26th Avenue                 <URL:http://www.rpw3.org/>
San Mateo, CA 94403             (650)572-2607


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Nov 30 2002, 2:09 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 30 Nov 2002 19:09:25 +0000
Local: Sat, Nov 30 2002 2:09 pm
Subject: Re: macros, &REST and REMF
* Rob Warnock
| Side question for the group: What are the rules for a &REST arg in
| macros? If they're the same as functions, then could you possibly get in
| trouble with Sam's idiom due to REMF destructively modifying the "opts"
| list?

  I believe that is the actual question, presented in his usual turbid way.
  The "idiom" is a recipe for disaster for macros and functions precisely
  because it may clobber a list that it does not own, but it is, of course,
  not a question about `remf´.  The issue of ownership of the argument list,
  which has been answered many times over and which does not change just
  because of some particular operator, should be pretty clear: Do not mutate
  the argument list.  This is close to a principle, and the answer does not
  change depending on the operator used to transmogrify it, obviously.

(defun sans (plist &rest keys)
  (let ((sans ()))
    (loop
      (let ((tail (nth-value 2 (get-properties plist keys))))
        ;; this is how it ends
        (unless tail
          (return (nreconc sans plist)))
        ;; copy all the unmatched keys
        (loop until (eq plist tail) do
              (push (pop plist) sans)
              (push (pop plist) sans))
        ;; skip the matched key
        (setq plist (cddr plist))))))

  I wrote this some time ago when I wanted to find a use for `nreconc´.

  It can be called as `(apply <function> (sans <arglist> :foo))´.  Its main
  features are that it conses minimally and is more efficient than making
  multiple passes over the same list for more than one key.  (It is assumed
  that `get-properties´ is fast.)

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »