<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Constants In Lisp Macro Calls</title>
	<atom:link href="http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/feed/" rel="self" type="application/rss+xml" />
	<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/</link>
	<description>Miscellaneous ramblings on miscellaneous topics</description>
	<pubDate>Wed, 07 Jan 2009 08:32:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Head Geek</title>
		<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/comment-page-1/#comment-516</link>
		<dc:creator>Head Geek</dc:creator>
		<pubDate>Wed, 10 Oct 2007 21:52:18 +0000</pubDate>
		<guid isPermaLink="false">http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/#comment-516</guid>
		<description>I've discovered that the proper answer to this is to use SYMBOL-VALUE instead of EVAL. As described on &lt;a href="http://www.lisp.org/table/style.htm" rel="nofollow"&gt;this page&lt;/a&gt;:

&lt;blockquote&gt;Novices almost always misuse EVAL. When experts use EVAL, they often would be better off using APPLY, FUNCALL, or SYMBOL-VALUE. Use of EVAL when defining a macro should set off a warning bell -- macro definitions are already evaluated during expansion. See also the answer to question 3-12. The general rule of thumb about EVAL is: if you think you need to use EVAL, you're probably wrong.&lt;/blockquote&gt;

I've confirmed that SYMBOL-VALUE will do the trick for this too.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve discovered that the proper answer to this is to use SYMBOL-VALUE instead of EVAL. As described on <a href="http://www.lisp.org/table/style.htm" rel="nofollow">this page</a>:</p>
<blockquote><p>Novices almost always misuse EVAL. When experts use EVAL, they often would be better off using APPLY, FUNCALL, or SYMBOL-VALUE. Use of EVAL when defining a macro should set off a warning bell &#8212; macro definitions are already evaluated during expansion. See also the answer to question 3-12. The general rule of thumb about EVAL is: if you think you need to use EVAL, you&#8217;re probably wrong.</p></blockquote>
<p>I&#8217;ve confirmed that SYMBOL-VALUE will do the trick for this too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Head Geek</title>
		<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/comment-page-1/#comment-502</link>
		<dc:creator>Head Geek</dc:creator>
		<pubDate>Sun, 30 Sep 2007 04:35:40 +0000</pubDate>
		<guid isPermaLink="false">http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/#comment-502</guid>
		<description>Nifty, yes, but everything I've read suggests that you avoid using it. Although this would be a special case where it's okay to do so, because Lisp macros are processed at compile-time anyway.</description>
		<content:encoded><![CDATA[<p>Nifty, yes, but everything I&#8217;ve read suggests that you avoid using it. Although this would be a special case where it&#8217;s okay to do so, because Lisp macros are processed at compile-time anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua Lee</title>
		<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/comment-page-1/#comment-499</link>
		<dc:creator>Joshua Lee</dc:creator>
		<pubDate>Sun, 30 Sep 2007 00:54:54 +0000</pubDate>
		<guid isPermaLink="false">http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/#comment-499</guid>
		<description>(newbie lisper hat on)

(eval) is pretty nifty in general, an interpreter within an interpreter, if I remember correctly.</description>
		<content:encoded><![CDATA[<p>(newbie lisper hat on)</p>
<p>(eval) is pretty nifty in general, an interpreter within an interpreter, if I remember correctly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Head Geek</title>
		<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/comment-page-1/#comment-496</link>
		<dc:creator>Head Geek</dc:creator>
		<pubDate>Sat, 29 Sep 2007 01:13:08 +0000</pubDate>
		<guid isPermaLink="false">http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/#comment-496</guid>
		<description>Nice -- it does the trick. :-) Thanks, that'll make things a little easier.</description>
		<content:encoded><![CDATA[<p>Nice &#8212; it does the trick. <img src='http://geekblog.oakcircle.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> Thanks, that&#8217;ll make things a little easier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C-Squared</title>
		<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/comment-page-1/#comment-495</link>
		<dc:creator>C-Squared</dc:creator>
		<pubDate>Fri, 28 Sep 2007 15:25:56 +0000</pubDate>
		<guid isPermaLink="false">http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/#comment-495</guid>
		<description>You're right, the macro isn't translating the constant to its value before using it.  As an example of this:

(defconstant +shift-array-size+ 10)

(defmacro constant-macro (var)
  (format t "VAR is ~a~%" var))

(defun constant-macro-test ()
  (constant-macro +shift-array-size+))

When compiled, the following is printed:

VAR is +SHIFT-ARRAY-SIZE+

So, to get it to use the constant, you have to force it to evaluate the constant string inside the macro by using (eval).  For example:

(defconstant +shift-array-size+ 10)

(defmacro constant-macro (var)
  (format t "VAR is ~a~%" (eval var)))

(defun constant-macro-test ()
  (constant-macro +shift-array-size+))

When compiled, the following is printed:

VAR is 10

So, to have your shift-array-down function work like you want it you, you just need to have it evaluate the arr-length parameter:

(defmacro shift-array-down (arr arr-length newitem)
  `(shiftf ,@(loop for i from (1- (eval arr-length)) downto 0 collecting `(svref ,arr ,i)) ,newitem))

That will work both when the arr-length is a number and when it's a reference to a constant.</description>
		<content:encoded><![CDATA[<p>You&#8217;re right, the macro isn&#8217;t translating the constant to its value before using it.  As an example of this:</p>
<p>(defconstant +shift-array-size+ 10)</p>
<p>(defmacro constant-macro (var)<br />
  (format t &#8220;VAR is ~a~%&#8221; var))</p>
<p>(defun constant-macro-test ()<br />
  (constant-macro +shift-array-size+))</p>
<p>When compiled, the following is printed:</p>
<p>VAR is +SHIFT-ARRAY-SIZE+</p>
<p>So, to get it to use the constant, you have to force it to evaluate the constant string inside the macro by using (eval).  For example:</p>
<p>(defconstant +shift-array-size+ 10)</p>
<p>(defmacro constant-macro (var)<br />
  (format t &#8220;VAR is ~a~%&#8221; (eval var)))</p>
<p>(defun constant-macro-test ()<br />
  (constant-macro +shift-array-size+))</p>
<p>When compiled, the following is printed:</p>
<p>VAR is 10</p>
<p>So, to have your shift-array-down function work like you want it you, you just need to have it evaluate the arr-length parameter:</p>
<p>(defmacro shift-array-down (arr arr-length newitem)<br />
  `(shiftf ,@(loop for i from (1- (eval arr-length)) downto 0 collecting `(svref ,arr ,i)) ,newitem))</p>
<p>That will work both when the arr-length is a number and when it&#8217;s a reference to a constant.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Head Geek</title>
		<link>http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/comment-page-1/#comment-494</link>
		<dc:creator>Head Geek</dc:creator>
		<pubDate>Fri, 28 Sep 2007 04:12:14 +0000</pubDate>
		<guid isPermaLink="false">http://geekblog.oakcircle.com/2007/09/27/constants-in-lisp-macro-calls/#comment-494</guid>
		<description>It seems that I have no problem if I use the constant within the macro itself, but I'm not permitted to use it as a parameter to the macro. Which, again, doesn't make sense -- in this case, it means that I'd have to make a different macro for each different size of array I want to use this with. I'll keep looking for an answer.</description>
		<content:encoded><![CDATA[<p>It seems that I have no problem if I use the constant within the macro itself, but I&#8217;m not permitted to use it as a parameter to the macro. Which, again, doesn&#8217;t make sense &#8212; in this case, it means that I&#8217;d have to make a different macro for each different size of array I want to use this with. I&#8217;ll keep looking for an answer.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
