<?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: Lisp Troubles: Fabricating a Closure&#8230;</title>
	<atom:link href="http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/</link>
	<description>software development and consulting</description>
	<lastBuildDate>Fri, 30 Dec 2011 03:29:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: pat</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-375</link>
		<dc:creator>pat</dc:creator>
		<pubDate>Thu, 08 Oct 2009 16:06:31 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-375</guid>
		<description>I thought about that for a bit here, too.  In this case though, I would then need to be sure that special was appropriately bound during each invocation of my lambda.</description>
		<content:encoded><![CDATA[<p>I thought about that for a bit here, too.  In this case though, I would then need to be sure that special was appropriately bound during each invocation of my lambda.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raito</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-374</link>
		<dc:creator>raito</dc:creator>
		<pubDate>Thu, 08 Oct 2009 15:55:49 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-374</guid>
		<description>I had a similar problem some time ago, where EVAL took place in a null lexical environment, but needed to use some stuff defined in the function that contained the EVAL. My solution was to declare the stuff I needed inside the EVAl as special, giving it dynamic scope. Because the EVAl took place in the dynamic scope that included the special variables, I was able to access them from inside the EVAL. It looed something like this:

[cc lang=&quot;lisp&quot;]
;not all the code, but you get the picture.
(defun name (arg1 arg2)
     (declare (special arg1 arg2))
...
     ; the code in (nth item results) can access arg1 and arg2
     (eval (nth item results)))

[/cc]</description>
		<content:encoded><![CDATA[<p>I had a similar problem some time ago, where EVAL took place in a null lexical environment, but needed to use some stuff defined in the function that contained the EVAL. My solution was to declare the stuff I needed inside the EVAl as special, giving it dynamic scope. Because the EVAl took place in the dynamic scope that included the special variables, I was able to access them from inside the EVAL. It looed something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;not all the code, but you get the picture.</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> <span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>arg1 arg2<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>declare <span style="color: #66cc66;">&#40;</span>special arg1 arg2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">...</span>
     <span style="color: #808080; font-style: italic;">; the code in (nth item results) can access arg1 and arg2</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> item results<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: pat</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-372</link>
		<dc:creator>pat</dc:creator>
		<pubDate>Thu, 08 Oct 2009 14:11:44 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-372</guid>
		<description>That... Is... Crafty....

Very nice.  Thank you.</description>
		<content:encoded><![CDATA[<p>That&#8230; Is&#8230; Crafty&#8230;.</p>
<p>Very nice.  Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tfb</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-371</link>
		<dc:creator>tfb</dc:creator>
		<pubDate>Thu, 08 Oct 2009 13:48:52 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-371</guid>
		<description>You were close with
[cc lang=&quot;lisp&quot;]
(defun generate-setter (array pre post)
  (eval `(lambda (index value)
           (setf (aref array ,@pre index ,@post) value))))
[/cc]

If you generate a function that, when called, returns the closure, you can get what you were trying for there.

[cc lang=&quot;lisp&quot;]
(defun generate-setter (array pre post)
  (let ((make-closure
         (eval `(lambda (array)
                  (lambda (index value)
                    (setf (aref array ,@pre index ,@post) value))))))
    (funcall make-closure array)))
[/cc]

And of course, you probably want to replace EVAL with COMPILE...</description>
		<content:encoded><![CDATA[<p>You were close with</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> generate-setter <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">array</span> pre post<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>index <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> <span style="color: #b1b100;">array</span> <span style="color: #66cc66;">,</span>@pre index <span style="color: #66cc66;">,</span>@post<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>If you generate a function that, when called, returns the closure, you can get what you were trying for there.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> generate-setter <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">array</span> pre post<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>make-closure
         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">array</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>index <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> <span style="color: #b1b100;">array</span> <span style="color: #66cc66;">,</span>@pre index <span style="color: #66cc66;">,</span>@post<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> make-closure <span style="color: #b1b100;">array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>And of course, you probably want to replace EVAL with COMPILE&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pat</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-370</link>
		<dc:creator>pat</dc:creator>
		<pubDate>Thu, 08 Oct 2009 12:52:20 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-370</guid>
		<description>I thought that the &lt;b&gt;(APPLY&#160;...)&lt;/b&gt; would totally mess with &lt;b&gt;(SETF&#160;...)&lt;/b&gt;&#039;s head.  Though, I suppose that I didn&#039;t try it that way.  That was my first implementation of the non-SETF version of this function.  I see now where there is an example of using &lt;b&gt;(APPLY&#160;#&#039;AREF&#160;...)&lt;/b&gt; in a &lt;b&gt;(SETF&#160;...)&lt;/b&gt; in the Hyperspec.  Thanks for pointing that out.

If I can just get the closure part to work then, I&#039;ll have a much more readable version than all this messing with &lt;b&gt;ARRAY-ROW-MAJOR-INDEX&lt;/b&gt;.

(As for the tag:  It wasn&#039;t tagged Lisp because I&#039;m still lame with Wordpress.  I typed in &quot;lisp&quot; into the tags box.  It showed me the list of auto-completes, and I thought I was done.  I forgot that I have to commit the tags with an &quot;enter&quot; or a mouse click... having the tags there when I hit &quot;Publish&quot; isn&#039;t enough.  Oops.)</description>
		<content:encoded><![CDATA[<p>I thought that the <b>(APPLY&nbsp;&#8230;)</b> would totally mess with <b>(SETF&nbsp;&#8230;)</b>&#8216;s head.  Though, I suppose that I didn&#8217;t try it that way.  That was my first implementation of the non-SETF version of this function.  I see now where there is an example of using <b>(APPLY&nbsp;#&#8217;AREF&nbsp;&#8230;)</b> in a <b>(SETF&nbsp;&#8230;)</b> in the Hyperspec.  Thanks for pointing that out.</p>
<p>If I can just get the closure part to work then, I&#8217;ll have a much more readable version than all this messing with <b>ARRAY-ROW-MAJOR-INDEX</b>.</p>
<p>(As for the tag:  It wasn&#8217;t tagged Lisp because I&#8217;m still lame with WordPress.  I typed in &#8220;lisp&#8221; into the tags box.  It showed me the list of auto-completes, and I thought I was done.  I forgot that I have to commit the tags with an &#8220;enter&#8221; or a mouse click&#8230; having the tags there when I hit &#8220;Publish&#8221; isn&#8217;t enough.  Oops.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pat</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-368</link>
		<dc:creator>pat</dc:creator>
		<pubDate>Thu, 08 Oct 2009 12:31:56 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-368</guid>
		<description>No... I haven&#039;t looked at Bordeaux FFT.  It didn&#039;t come up when I searched for FFT on the Cliki or when I searched for FFT and Lisp together on Google.  I will look at it.

Actually, it&#039;s on my first page of Google results at the moment.  I don&#039;t know what I had searched for before, or how I had missed it.  Definitely, it could use a mention on the Cliki.</description>
		<content:encoded><![CDATA[<p>No&#8230; I haven&#8217;t looked at Bordeaux FFT.  It didn&#8217;t come up when I searched for FFT on the Cliki or when I searched for FFT and Lisp together on Google.  I will look at it.</p>
<p>Actually, it&#8217;s on my first page of Google results at the moment.  I don&#8217;t know what I had searched for before, or how I had missed it.  Definitely, it could use a mention on the Cliki.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zach</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-367</link>
		<dc:creator>Zach</dc:creator>
		<pubDate>Thu, 08 Oct 2009 12:21:35 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-367</guid>
		<description>The simple, straightforward implementation could look like this due to how APPLY and AREF are specified to work together:

[cc lang=&quot;lisp&quot;]
(defun (setf row-ref) (value row index)
  (with-slots (array pre post) row
    (setf (apply #&#039;aref array (append pre (list index) post)) value)))
[/cc]

(Why isn&#039;t this post tagged &quot;lisp&quot;?)</description>
		<content:encoded><![CDATA[<p>The simple, straightforward implementation could look like this due to how APPLY and AREF are specified to work together:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> row-ref<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">value</span> row index<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>with-slots <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">array</span> pre post<span style="color: #66cc66;">&#41;</span> row
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> #'<span style="color: #b1b100;">aref</span> <span style="color: #b1b100;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> pre <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> index<span style="color: #66cc66;">&#41;</span> post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>(Why isn&#8217;t this post tagged &#8220;lisp&#8221;?)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: michaelw</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-365</link>
		<dc:creator>michaelw</dc:creator>
		<pubDate>Thu, 08 Oct 2009 08:54:21 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-365</guid>
		<description>Hi, 
not answering your question, really, but have you looked at &lt;a href=&quot;http://vintage-digital.com/hefner/software/bordeaux-fft/manual.html&quot; rel=&quot;nofollow&quot;&gt;bordeaux-fft&lt;/a&gt;?</description>
		<content:encoded><![CDATA[<p>Hi,<br />
not answering your question, really, but have you looked at <a href="http://vintage-digital.com/hefner/software/bordeaux-fft/manual.html" rel="nofollow">bordeaux-fft</a>?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pat</title>
		<link>http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/comment-page-1/#comment-363</link>
		<dc:creator>pat</dc:creator>
		<pubDate>Thu, 08 Oct 2009 06:57:45 +0000</pubDate>
		<guid isPermaLink="false">http://nklein.com/?p=951#comment-363</guid>
		<description>Feh, I suppose that I can get away with it if I pass in the buffer each time.  I&#039;d rather have a closure, but this will work at the expense of having to pull the buffer out of my &lt;b&gt;virtual-row&lt;/b&gt; each call:
[cc lang=&quot;lisp&quot;]
(defun generate-setter (pre post)
  (eval `(lambda (buffer index value)
           (setf (aref buffer ,@pre index ,@post) value))))
[/cc]</description>
		<content:encoded><![CDATA[<p>Feh, I suppose that I can get away with it if I pass in the buffer each time.  I&#8217;d rather have a closure, but this will work at the expense of having to pull the buffer out of my <b>virtual-row</b> each call:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> generate-setter <span style="color: #66cc66;">&#40;</span>pre post<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>buffer index <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">aref</span> buffer <span style="color: #66cc66;">,</span>@pre index <span style="color: #66cc66;">,</span>@post<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
]]></content:encoded>
	</item>
</channel>
</rss>

