<p>I've been working through <a href="http://gigamonkeys.com/book" rel="nofollow noreferrer">Practical Common Lisp</a> and as an exercise decided to write a macro to determine if a number is a multiple of another number:</p>

<p><code>(defmacro multp (value factor)<br>
   `(= (rem ,value ,factor)  0))</code></p>

<p>so that :
<code>(multp 40 10)</code>
evaluates to true whilst
<code>(multp 40 13)</code>
does not </p>

<p>The question is does this macro <a href="http://gigamonkeys.com/book/macros-defining-your-own.html#plugging-the-leaks" rel="nofollow noreferrer">leak</a> in some way?  Also is this "good" Lisp?  Is there already an existing function/macro that I could have used?</p>

[{'answer_id': 60267, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': True, 'text': "<p>Siebel gives an extensive rundown (for simple cases anyway) of possible sources of leaks, and there aren't any of those here. Both <code>value</code> and <code>factor</code> are evaluated only once and in order, and <code>rem</code> doesn't have any side effects.</p>\n\n<p>This is not good Lisp though, because there's no reason to use a macro in this case. A function</p>\n\n<pre><code>(defun multp (value factor)\n  (zerop (rem value factor)))\n</code></pre>\n\n<p>is identical for all practical purposes. (Note the use of <code>zerop</code>. I think it makes things clearer in this case, but in cases where you need to highlight, that the value you're testing might still be meaningful if it's something other then zero, <code>(= ... 0)</code> might be better)</p>\n"}, {'answer_id': 60268, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>Your macro looks fine to me. I don\'t know what a leaky macro is, but yours is pretty straightforward and doesn\'t require any gensyms. As far as if this is "good" Lisp, my rule of thumb is to use a macro only when a function won\'t do, and in this case a function can be used in place of your macro. However, if this solution works for you there\'s no reason not to use it.</p>\n'}, {'answer_id': 81824, 'author': 'Matthias Benkard', 'author_id': 15517, 'author_profile': 'https://Stackoverflow.com/users/15517', 'pm_score': 1, 'selected': False, 'text': "<p>Well, in principle, a user could do this:</p>\n\n<pre><code>(flet ((= (&amp;rest args) nil))\n  (multp 40 10))\n</code></pre>\n\n<p>which would evaluate to NIL... except that ANSI CL makes it illegal to rebind most standard symbols, including CL:=, so you're on the safe side in this particular case.</p>\n\n<p>In generial, of course, you should be aware of both referential untransparency (capturing identifiers from the context the macro is expanded in) and macro unhygiene (leaking identifiers to expanded code).</p>\n"}, {'answer_id': 268416, 'author': 'Vatine', 'author_id': 34771, 'author_profile': 'https://Stackoverflow.com/users/34771', 'pm_score': 0, 'selected': False, 'text': '<p>No, no symbol introduced in the macro\'s "lexical closure" is released to the outside.</p>\n\n<p>Note that leaking isn\'t NECESSARILY a bad thing, even if accidental leaking almost always is. For one project I worked on, I found that a macro similar to this was useful:</p>\n\n<pre><code>(defmacro ana-and (&amp;rest forms)\n  (loop for form in (reverse forms)\n        for completion = form then `(let ((it ,form))\n                                      (when it\n                                       ,completion))\n        finally (return completion)))\n</code></pre>\n\n<p>This allowed me to get "short-circuiting" of things needed to be done in sequence, with arguments carried over from previous calls in the sequence (and a failure signalled by returning NIL). The specific context this code is from is for a hand-written parser for a configuration file that has a cobbled-together-enough syntax that writing a proper parser using a parser generator was more work than hand-rolling.</p>\n'}]
['https://Stackoverflow.com/questions/60260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5303/']