<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SamAuciello.com</title>
	<atom:link href="http://samauciello.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://samauciello.com</link>
	<description></description>
	<lastBuildDate>Thu, 16 Feb 2012 17:17:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Zsh Customization and Beautification</title>
		<link>http://samauciello.com/zsh-customization-and-beautification/</link>
		<comments>http://samauciello.com/zsh-customization-and-beautification/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 17:16:32 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=243</guid>
		<description><![CDATA[So I was working on a project in Ruby with my friend Isaac when I noticed that his terminal looked really cool. I asked him about it and he said it was zsh with a plugin called zsh-syntax-highlighting. I found &#8230; <a href="http://samauciello.com/zsh-customization-and-beautification/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I was working on a project in Ruby with my friend Isaac when I noticed that his terminal looked really cool.  I asked him about it and he said it was <code>zsh</code> with a plugin called <code>zsh-syntax-highlighting</code>.  I found the project&#8217;s <a href="https://github.com/zsh-users/zsh-syntax-highlighting">github page</a> and tried setting it up but it didn&#8217;t immediately work.<span id="more-243"></span></p>
<p>Following the instructions in the project&#8217;s <code>README.md</code> file, I cloned the project</p>

<div class="wp_codebox"><table><tr id="p2438"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p243code8"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone https:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>zsh-users<span style="color: #000000; font-weight: bold;">/</span>zsh-syntax-highlighting.git</pre></td></tr></table></div>

<p>then I added the line</p>

<div class="wp_codebox"><table><tr id="p2439"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p243code9"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>zsh-syntax-highlighting<span style="color: #000000; font-weight: bold;">/</span>zsh-syntax-highlighting.zsh</pre></td></tr></table></div>

<p>to my <code>.zshrc</code> file.</p>
<p>Unfortunately, when I tried re-instantiating my terminal, I got the following error message</p>

<div class="wp_codebox"><table><tr id="p24310"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p243code10"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>olleicua<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>zsh<span style="color: #000000; font-weight: bold;">/</span>zsh-syntax-highlighting<span style="color: #000000; font-weight: bold;">/</span>zsh-syntax-highlighting.zsh:<span style="color: #000000;">76</span>: parse error near <span style="color: #000000; font-weight: bold;">`</span><span style="color: #000000; font-weight: bold;">done</span><span style="color: #ff0000;">'</span></pre></td></tr></table></div>

<p>I spent a good forty-five minutes googling variations on &#8220;zsh-syntax-highlighting parse error near `done&#8217;&#8221; with and without my <code>zsh</code> version and OS.  Finally it occurred to me to try the search without <code>zsh-syntax-highlighting</code> since it my be an issue with my <code>zsh</code> setup specifically and I found <a href="http://www.zsh.org/mla/users/1998/msg00228.html">this</a> mailing list archive.  The mailing list suggested that one of the built-in <code>zsh</code> keywords that was being used in a control structure had been overwritten by an alias, in this case <code>fi</code>.  I reviewed the section of the plugin that was bugging out and noticed that it was using the keyword <code>do</code> in its control structures and recalled that I had aliased <code>do</code> to <code>cd</code> into my downloads directory.  I changed the alias to <code>dow</code> and now the plugin works fine.</p>
<p>The moral of this story is to always use <code>which</code> before naming an alias or binary in your path.  <code>which</code> looks for the specified name in it&#8217;s list of built-in commands, in your defined aliases and in your path.  It then tells you if it found it and if so where.  In this case</p>

<div class="wp_codebox"><table><tr id="p24311"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p243code11"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #000000; font-weight: bold;">do</span>: shell reserved word</pre></td></tr></table></div>

<p>It doesn&#8217;t complain when you overwrite things with aliases because you want to be able to do things like</p>

<div class="wp_codebox"><table><tr id="p24312"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p243code12"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">rm</span>=<span style="color: #ff0000;">&quot;rm -rf&quot;</span></pre></td></tr></table></div>

<p>if you&#8217;re feeling foolhardy.</p>
<h2>Coloring zsh</h2>
<p>Once I had gotten this to work, I was so happy with myself that I decided to make my <code>zsh</code> more colorful in general.  I had to do a bit of trial and error because I&#8217;m using both a prompt and a right-prompt. Before this change my shell looked like this</p>
<p><img src="/wp-content/themes/samauciello/images/zsh-screenshot-1.png" /></p>
<p>and was generated by the following in my <code>.zshrc</code> file</p>

<div class="wp_codebox"><table><tr id="p24313"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p243code13"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PROMPT</span>=<span style="color: #ff0000;">'
$ '</span>
&nbsp;
<span style="color: #007800;">RPROMPT</span>=<span style="color: #ff0000;">'| %~ | %D{%k:%M} | %h | Jack |'</span></pre></td></tr></table></div>

<p>I was able to add colors by changing this to</p>

<div class="wp_codebox"><table><tr id="p24314"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p243code14"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PROMPT</span>=<span style="color: #ff0000;">'%F{green}
$ %f'</span>
&nbsp;
<span style="color: #007800;">RPROMPT</span>=<span style="color: #ff0000;">'%F{green}|%f %F{cyan}%~%f %F{green}|%f %F{cyan}%D{%k:%M}%f %F{green}|%f %F{cyan}%h%f %F{green}|%f %F{cyan}Jack%f %F{green}|%f'</span></pre></td></tr></table></div>

<p>In this case <code>%F{COLOR}TEXT%f</code> is the syntax for coloring text in <code>zsh</code> prompts.  The result was</p>
<p><img src="/wp-content/themes/samauciello/images/zsh-screenshot-2.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/zsh-customization-and-beautification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Semester Studying Open Source Software</title>
		<link>http://samauciello.com/a-semester-studying-open-source-software/</link>
		<comments>http://samauciello.com/a-semester-studying-open-source-software/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 02:00:51 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=227</guid>
		<description><![CDATA[This post is the final paper I wrote for my self-taught open source software class this semester. This semester, in an attempt further my education in the art of making great software, I began looking at how software is made &#8230; <a href="http://samauciello.com/a-semester-studying-open-source-software/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="text-align:center;"><span class="pren">This post is the final paper I wrote for my self-taught open source software class this semester.</span></div>
<p>This semester, in an attempt further my education in the art of making great software, I began looking at how software is made in the open source world.  I chose to look at this field for several reasons.  I’ve been quite interested in open source software as long as I’ve known about it.  I appreciate the social/economic benefits of software that is made by individuals who want it to be of quality as opposed to corporations that simply want to make money (and are therefore frequently motivated to compromise quality for business reasons e.g. planned obsolescence etc.).  My experience with academic vs. professional programming also leads me to believe that people are far more likely to cut corners when they’re getting paid.  Another reason for looking into open source was that large-scale software is often made in teams.  I had had very little experience working on software with a group and, being at tiny isolated liberal arts school with a similarly tiny computer science department, I realized that this was a key missing component of my education.  This would also give me an opportunity to compare my coding to others’ so as to get a sense of how I need to improve.<span id="more-227"></span></p>
<p>I began the semester thinking I would be contributing to <a href="http://www.mozilla.org/">Mozilla</a> <a href="http://www.mozilla.org/en-US/firefox/">Firefox</a> and I spent the first few weeks looking into this.  In the process I was exposed the <a href="https://developer.mozilla.org/En/XUL"><code>XUL</code></a> and was able to build a very simple Firefox extension as well and getting a glimpse at the guts of Firefox.  I quickly realized that my lack of familiarity with <code>XUL</code>, <code>C++</code>, and Firefox development in general was going to make contributing difficult and that I would likely spend more time exploring these technologies than learning about open source.  For this reason, I switched my focus to <a href="http://wordpress.org/">WordPress</a>, a project I was already much more familiar with.  I had previously dabbled in creating plug-ins for WordPress so contributing to the core in <code>PHP</code> (a language I am quite familiar with) seemed far less daunting.  Although I would still love to someday learn <code>C++</code> and <code>XUL</code> and explore the Mozilla community, I’m glad I made this decision because it allowed me to actually engage with a community the maintains a popular <a href="http://en.wikipedia.org/wiki/Content_management_system">CMS</a> used by millions of bloggers.  I made several small contributions to the swiftly approaching release of <a href="http://codex.wordpress.org/Version_3.3">WordPress 3.3</a> (as of the time of this writing there is a <a href="http://core.trac.wordpress.org/ticket/19475">single ticket impeding the release</a>).  My contributions included three major patches and a bug report.  I got the opportunity to interact with several of the movers and shakers of the community and the experience was, on a whole, intimidating yet gratifying.  I intend to continue contributing to WordPress.</p>
<p>I also spent a some time exploring some of the tools of open source software such as version control systems.  Although WordPress uses Subversion, the software world at large seems to be slowly moving towards distributed version control systems like Git and Mercurial.  I’ve become somewhat familiar with Mercurial and I hope to find more excuses to work with it in the future.  I also looked a bit at software licenses.  In particular, I looked at the differences between restrictively open “free” licenses like GPL and LGPL and much simpler ones like the MIT license.  I didn’t spend much time on this subject as it doesn’t directly relate to programming and would probably be more interesting to a law student but it was good to get a sense of what exactly is meant by terms like open and free when they are applied to software.</p>
<p>I also spent some time this semester looking at subjects tangential to open source but directly related to becoming a well rounded programmer.  For example, I began formally acquainting myself with <code>Perl</code>.  <code>Perl</code> seems to be quite powerful though intimidatingly open-ended.  I’ve also begun learning <code>Ruby</code>, which I feel will soon replace <code>Python</code> as my go-to scripting language.  Finally I dove into Emacs which is increasingly my default text-editor.  I’ve spent the semester continuing to improve my general coding skills and using open source as focal point.  I set out to get exposure to a larger world of software technology specifically in the open source community and I feel that I have done this.  This is of course not to say that I’m done; the software world is enormous and there will always be more to learn.</p>
<h2>Contributing</h2>
<p>I did make some initial attempts to contribute to Mozilla Firefox.  I found the process inviting, with many helpful tutorials on how to get started.  The main barrier to entry for me was that most of the tickets marked as <a href="https://bugzilla.mozilla.org/buglist.cgi?quicksearch=sw:[good%20first%20bug]">“Good first bugs”</a> in Bugzilla were like Greek to me.  They often referred to aspects of <code>C++</code> or the Mozilla core that I wasn’t familiar with and assumed that I was.  There were a couple of bugs that I was able to understand well enough to reproduce.  The first was one involving Firefox’s handling of <code>&lt;wbr /&gt;</code> tags, which was inconsistent with other browsers.  There was some disagreement as to whether the behavior of Firefox was in fact incorrect.  Given the clear lack of agreement this seemed like a bad place to start.  The other bug involved a button in the bookmark interface that needed to be disabled in a certain circumstance.  This seemed like something that I might be able to fix but before I got the chance to a patch was submitted by someone else.  This was about the time I decided to switch to looking at WordPress.</p>
<p>WordPress was slightly less initially inviting.  Unlike with Firefox, there was no “so you want to contribute to WordPress” page with helpful step by step instructions.  There was a general description of how to get involved, which I followed.  The main difference was my familiarity with the underlying technologies.  About half of the tickets in <a href="http://core.trac.wordpress.org/">trac</a> were clear enough that I could begin looking into them without much trouble at all and the other half would become more straight forward if I looked at them more.  My first contribution was to <a href="http://core.trac.wordpress.org/ticket/18989">ticket #18314</a> which was about cleaning up duplicate entries in the admin style-sheet.  I took a look a the offending <code>CSS</code> file and quickly found a section where a large block of identical rules was being applied separately to three different selectors.  I merged the identical rules into one block, made a patch using <code>svn diff</code> and submitted it to trac.  My patch was soon committed and so I decided to do a once over of the <code>CSS</code>ticket #18866.  It referred to a bug in the error page displayed if the <code>wp-config.php</code> file is missing.  Another bug having to do with certain situations where the <code>URL</code> of the appropriate style-sheet for the error page might not be accessible had necessitated putting the <code>CSS</code> rules for the page in-line in the <code>HTML</code> head.  A couple of rules had been forgotten so I tracked down the file that was generating the page and added the necessary rules.  I found it necessary to include a reference to a background gradient image, causing the same problem as including a style-sheet would have, except that the difference between the page with and without this gradient was barely noticeable so this seemed like the solution most likely to work most of the time.  Another contributor suggested using <code>CSS</code> gradients but, as they are not supported by Microsoft Internet Explorer prior to version 10, my patch was used.</p>
<p>I also fixed a bug in the “Press This” interface, <a href="http://core.trac.wordpress.org/ticket/18989">ticket #18989</a>.  Press This is a bookmarklet that opens a pop-up window allowing bloggers to easily post about content they find while browsing other sites.  In addition to automatically pasting a link to the site into the post the bookmarklet grabs any selected text as well as providing an interface to include in-line images from the page.  A change from another ticket had caused a bug in the image including functionality.  Everything seemed to work fine but the image was not pasted into the post.  Another contributor had pointed to the offending bit of code.  It seems that an <code>HTML</code> input tag with the same id had been added to the interface and the <code>Jquery</code> selector that got the image <code>URL</code> to insert it into the post was now no longer specific enough.  I added a more specific selector but this didn’t fix the problem.  By adding <code>console.log()</code> statements, I was able to follow a relatively complex chain of seemingly unnecessary functions and discover a place where the <code>HTML</code> to be inserted into the post was being passed as the only argument to function that expected it as the second argument.  Not wanting to break another aspect of the application, I passed the parameter as both the first and second arguments and the bug was fixed.  If I had been more comfortable with general workings of the editing interface I’d have looked further to see why this was so excessively complex but this felt like a much larger project so I just made it work in a way that was unlikely to cause conflicts.  It strikes me that this sort of apathy is probably what allows this code to remain as complex as it is.  Perhaps I will look into cleaning it up for the next release.</p>
<p>I also found a bug and created <a href="http://core.trac.wordpress.org/ticket/19119">ticket #19119</a>.  A major feature had been added creating fly-out menus for the main admin navigation in <a href="http://core.trac.wordpress.org/ticket/18382">ticket #18382</a>.  I noticed relatively early that the changes being made had caused the collapsed menu fly-outs to be off by one pixel.  Since there were a lot of developers working on this at the time I assumed that someone was on it and was worried that too many cooks might spoil the sauce.  Apparently I was wrong because ticket #18382 was closed with the notice “If you encounter any bugs, please open a new ticket.”  The bug was still there so I opened a new ticket.  I tried submitting a patch to it but a better one was used by someone with a better understanding of the <code>CSS</code> involved.  I also made several smaller contributions in the form of testing patches and commenting on discussions.</p>
<h2>The Open Source Community</h2>
<p>My initial reaction to the communities I looked at was that they were very inviting.  This was unsurprising as it seems like a necessity to an organization fueled by volunteers.  It was interested to see how decision were made within these communities.  The Mozilla approach is a complex hierarchical structure that rewards longstanding contributors with the ability to make more serious decisions.  WordPress being smaller, has a much simpler approach.  There is a core WordPress team that has commit access to the Subversion repository.  Anyone else can check out the repository and submit changes to Trac in the form of patches (anyone can create an account in Trac).  Because WordPress is relatively small, it’s rather trivial for the core team to keep track of tickets with patches in them and commit things that need to be committed.</p>
<p>It was interesting to see the varying quality of documentation in WordPress code.  Any function intended to be used by plug-in/theme developers is described in detail in the <a href="http://codex.wordpress.org/">WordPress Codex</a> in terms that can be understood by someone who has very little programming experience.  Anything internal is lucky to have more than basic in-line comments next to it in the code.  This also varies widely from file to file.  Most of the files have a comment block at the top explaining the file’s purpose but this is often all you get and you have to hope the code is well written (which it only sometimes is).  It’s not clear to me why there is such a discrepancy between end user documentation and developer documentation.  It seems reasonable that user documentation would be prioritized as it is part of making the product usable but it seems like it would be just as important to provide support to developers so to help developers improve the core.</p>
<p>One thing that surprised me that seemed to be a major feature of the community was a general sense of inertia.  A debate came up in the <a href="http://codex.wordpress.org/Mailing_Lists#Hackers">wp-hackers email list</a> about a month ago about the differences between relative and absolute URLs.  Whenever an internal image or link is added to a post or page on a WordPress site, the editor stores an absolute URL in the database.  The strongest argument for this seems to be that absolute URLs will fully specify the correct location in any context and since the pages therefore always want to be served that way, they shouldn’t need to be processed for every page load.  The strongest argument against this seemed to be the issue of portability.  If development is at dev.example.com and a production is at example.com we shouldn’t need to fix the database any time we push the database to production.  Various other arguments were made and things got a bit out of hand at times but the end result was that since the core team was mostly in the former camp nothing was going to change in the WordPress core but it was fine to implement relative URLs in a plug-in (this was not particularly satisfactory for the proponents of relative URLs since the plug-in solution turned out the be somewhat clunky).  “Make it a plug-in” seems to be an unfortunately common answer to feature requests that don’t seem necessary to the use case that the core team has in mind.  For example re-arranging the order of your pages is an annoyingly complex task of meticulously changing a number in a field on each page.  There is a plug-in called <a href="http://wordpress.org/extend/plugins/my-page-order/">my page order</a> that provides a nice drag-and-drop interface for this but no one has considered incorporating it into the core.  It strikes me as unfortunate that this sort of inertia exists and I hope that it isn’t as prevalent in the open source community at large as it is in WordPress as it seems like a key barrier to competition with commercial software.</p>
<h2>Gaining Programming Skills</h2>
<p>One of my main goals for the semester was to gain skills in group development.  To this end, I spent a good deal of time acquainting myself with the relevant tools.  One of the most important tools in group development is version control.  The three version control systems I looked at were Subversion, Mercurial, and Git.  Subversion was clearly a vast improvement on its predecessor CVS, but it seems to be somewhat outdated at this point because the advantages of distributed version control are significant.  Unfortunately it is still quite prevalent.  Mercurial seems to be a really nice solution as it is fast, powerful, distributed, and relatively easy to learn.  Git seems to be the most powerful of the three but is harder to learn.  My current preference is for Mercurial which I have started using for some of my own projects.</p>
<p>The experience of working with other people’s code was helpful in general.  My experience with ticket #18989 in particular drove home the importance of keeping code simple and easy to follow.  It also re-affirmed my appreciation for the value of debug statements.</p>
<p>I also spent some time looking at Software licenses.  Particularly I looked at the differences between GPL, LGPL, and more permissive licenses like the MIT license.  GPL is restrictively free.  This sounds like a contradiction but the idea is basically that if a piece of software is licensed under GPL then any software built on top of it must also be GPL.  This ensures that the use of this code will never be subject to proprietary endeavors.  LGPL is less restrictive.  It allows proprietary software to be built on top of it so long as the licensed software is unmodified.  LGPL is common for programming languages and other situations where GPL would severely limit the products usability</p>
<p>I gained a lot of general knowledge/skills this semester that will help me in my goal of becoming a great software developer.  In addition to gaining familiarity to the WordPress core, I got exposure to <code>XUL</code>, Version Control, software licenses, <code>Perl</code>, and <code>Ruby</code>.  Although my semester was not as focused on contributing to open source as I originally intended it to be, I don’t see this as having been a problem.  The nature of software is that making it well is often as much about knowing how to use the tools as it is about understanding the concepts.  I feel like this semester gave me the opportunity to become acquainted with a number of powerful tools of the trade.  Even though it didn’t necessarily coalesce into a clearly structured curriculum, it gave me a chance to learn what I need to learn.</p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/a-semester-studying-open-source-software/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Processing with Convolution Functions</title>
		<link>http://samauciello.com/image-processing-with-convolution-functions/</link>
		<comments>http://samauciello.com/image-processing-with-convolution-functions/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 02:12:54 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=215</guid>
		<description><![CDATA[We&#8217;ve been working with convolution functions in my AI class and I&#8217;ve written a nifty little program in PHP to run convolutions of arbitrary 3&#215;3 kernels on gifs, pngs, and jpegs. You can try it out here. Be warned that &#8230; <a href="http://samauciello.com/image-processing-with-convolution-functions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been working with convolution functions in my AI class and I&#8217;ve written a nifty little program in PHP to run convolutions of arbitrary 3&#215;3 kernels on gifs, pngs, and jpegs.  You can try it out <a href="/image_processing/">here</a>.  Be warned that the page has no stylesheet.  I got immersed in the JQuery/AJAX/PHP coding and forgot to make the page look nice. In the end I decided that it had a cool retro feel to it and kept it.<span id="more-215"></span></p>
<h2>Convolutions</h2>
<p>I will herein describe convolution functions, if you are already familiar with the topic, feel free to skip ahead to the next heading.</p>
<p>The basic idea of a convolution is to combine to mathematical functions in a particular way.  The concept (like most things in mathematics) is described quite well on its <a href="http://en.wikipedia.org/wiki/Convolution">Wikipedia page</a>, so I won&#8217;t go into enormous detail here.  As it applies to image processing, convolution is a process of applying a grid of numerical values called a kernel to an image.  A simple example of a kernel is:</p>
<table class="centered kernel">
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
</tr>
</table>
<p>The process is quite simple: look at each pixel in the input image and set the corresponding output pixel to the weighted sum/average of the neighboring pixels where the weights are determined by the values in the kernel.  So for example, with the above kernel, each pixel diagonally adjacent to a given pixel will be given a weight of 1, each pixel orthogonally adjacent to the pixel will be given a weight of 2, and the pixel itself will be given a weight of 3.  This particular kernel has the effect of blurring the image.</p>
<p>All sorts of things can be done with different kernels.  One interesting use is finding edges which can be done with a kernel like:</p>
<table class="centered kernel">
<tr>
<td>-1</td>
<td>-2</td>
<td>-1</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>1</td>
</tr>
</table>
<h2>AJAX without AJAX</h2>
<p>The thing that excited me most in building this little program was the realization that although it uses JQuery to communicate with the server without reloading the page without using <code>AJAX</code>, at least not in the traditional sense.  It is technically still asynchronous, and it doesn&#8217;t use <code>XML</code> (but modern <code>AJAX</code> rarely does).</p>
<p>When you click the process image button, a couple of things happen.  First the image you selected is asynchronously uploaded using a JQuery plugin called <a href="http://lagoscript.org/jquery/upload">JQuery.upload</a>.  As it turns out the only way to asynchronously upload a file is using an iframe, which is exactly what this plugin does.  So not really <code>AJAX</code> as we typically think of it.  The second thing that happens is the <code>src</code> attribute of the modified image on the page is set to a php script that processes the newly uploaded image and spits out an image, definitely not <code>AJAX</code> as we normally think of it.</p>
<p>That&#8217;s basically it, besides boring implementation details (personally I find implementation details fascinating but this is a blog post not documentation).</p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/image-processing-with-convolution-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contributing to WordPress</title>
		<link>http://samauciello.com/contributing-to-wordpress/</link>
		<comments>http://samauciello.com/contributing-to-wordpress/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 14:47:07 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=207</guid>
		<description><![CDATA[I&#8217;ve been making some contributions to WordPress.org for my Open Source Software course. I&#8217;ve been helping out with the upcoming release of version 3.3 scheduled for the end of November. I&#8217;ll use this post to talk about some of the &#8230; <a href="http://samauciello.com/contributing-to-wordpress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been making some contributions to WordPress.org for my Open Source Software course. I&#8217;ve been helping out with the upcoming release of version 3.3 <a href="http://wpdevel.wordpress.com/version-3-3-project-schedule/">scheduled</a> for the end of November.  I&#8217;ll use this post to talk about some of the tickets I&#8217;ve been working on.<span id="more-207"></span></p>
<h2>Press This and Images</h2>
<p>WordPress has a feature called &#8220;Press This&#8221;, which allows bloggers to easily share content that they might find while browsing the web.  Using the &#8220;Press This&#8221; <a href="http://en.wikipedia.org/wiki/Bookmarklet">bookmarklet</a> pops up a new window which allows the user to post new content including anything highlighted on the page you were on.  One nice feature is that when you click the add images button from this new post interface, it loads all of the images from the page you were on and gives you the option of inserting them into your post.</p>
<p>There was a bug created however by some unrelated change leading up to the 3.3 release.  You see when you select an image from the page to insert into your post, you are prompted to add a description and then click the &#8220;insert photo&#8221; button to insert it.  This button uses the JQuery to determine the url and description of the image to insert with something like:</p>

<div class="wp_codebox"><table><tr id="p20717"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p207code17"><pre class="javascript" style="font-family:monospace;">url <span style="color: #339933;">=</span> JQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#this_photo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
desc <span style="color: #339933;">=</span> JQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#this_photo_description'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The problem was that another change had created two unrelated DOM objects with the ids <code>this_photo</code> and <code>this_photo_description</code>.  Consequently the JQuery, assuming that ids are unique <span class="pren">(as they should be)</span> grabbed the wrong DOM objects, found no values and then promptly failed.  To fix this I found the id of the parent div to the two DOM objects I wanted and replaced the code with something like this:</p>

<div class="wp_codebox"><table><tr id="p20718"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p207code18"><pre class="javascript" style="font-family:monospace;">url <span style="color: #339933;">=</span> JQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#parent_div #this_photo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
desc <span style="color: #339933;">=</span> JQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#parent_div #this_photo_description'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is probably not ideal but since I wasn&#8217;t sure where else these objects might be referenced by JavaScript and I didn&#8217;t want to create more bugs that might not be caught this close to a release I didn&#8217;t want to mess with changing the ids themselves.</p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/contributing-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More adversarial search, Tic-Tac-Toe and Othello</title>
		<link>http://samauciello.com/more-adversarial-search-tic-tac-toe-and-othello/</link>
		<comments>http://samauciello.com/more-adversarial-search-tic-tac-toe-and-othello/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 21:46:45 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Lisp]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=174</guid>
		<description><![CDATA[For my midterm project in the Artificial Intelligence course I applied the adversarial search program in Lisp that I started here to Tic-Tac-Toe and Othello. Dynamic Functions The API system I had set up involved using functions to represent moves &#8230; <a href="http://samauciello.com/more-adversarial-search-tic-tac-toe-and-othello/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For my midterm project in the Artificial Intelligence course I applied the adversarial search program in Lisp that I started <a href="http://samauciello.com/alpha-beta-min-max-search/">here</a> to <a href="http://en.wikipedia.org/wiki/Tic-tac-toe">Tic-Tac-Toe</a> and <a href="http://en.wikipedia.org/wiki/Othello_%28game%29">Othello</a>.<span id="more-174"></span></p>
<h3>Dynamic Functions</h3>
<p>The API system I had set up involved using functions to represent moves and both of these games have more possible moves than it would be sensible to define individually. Fortunately Lisp is great at generating code dynamically. It took a while to find the right syntax for this and what the method I eventually used was the back-tick quoting mechanism that <a href="http://www.marlboro.edu/academics/faculty/mahoney_jim/">Jim Mahoney</a> pointed me to. The following code generates the possible moves for my Tic-Tac-Toe program:</p>

<div class="wp_codebox"><table><tr id="p17427"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p174code27"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> all-moves
      '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>top-left <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>top-center <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>top-right <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>middle-left <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>center <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>middle-right <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>bottom-left <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>bottom-center <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>bottom-right <span style="color: #66cc66;">.</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>move all-moves<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><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> move<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> move<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;">eval</span> `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> <span style="color: #66cc66;">,</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>mark-square <span style="color: #66cc66;">,</span>num state<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></pre></td></tr></table></div>

<p>The backtick <code>`</code> notation here lets me quote the list and still add in evaluated pieces later with <code>,</code> so that I can construct the <code>defun</code> statement as I want it before evaluating the whole thing with <code>eval</code>.</p>
<h3>Side effects and Pointers</h3>
<p>Another issue I found was that while the adversarial search worked fine for my <a href="http://samauciello.com/alpha-beta-min-max-search/">stone game</a>. I had made a fundamental assumption. A given game state for the stone game is represented by and integer and as such making a function that returns a modified copy of the game state without affecting the original <span class="pren">(something I do a lot)</span> is simple and requires no thought. Unfortunately this is not the case with more complicated data structures like lists.</p>
<p>For both tic-tac-toe and othello, my games states were represented by lists and as such I needed to use <code>copy-list</code>. My tic-tac-toe games states are encoded as a list of length 10 with the first element being the current player <span class="pren">(X or O)</span> followed by the contents of each square in left to right top to bottom order. So for example the start state is:</p>

<div class="wp_codebox"><table><tr id="p17428"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p174code28"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>X <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>and if <code>X</code> goes in the center the state would become</p>

<div class="wp_codebox"><table><tr id="p17429"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p174code29"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>X <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> X <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>A naive version of my <code>mark-square</code> function referenced in the above code might look like this:</p>

<div class="wp_codebox"><table><tr id="p17430"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p174code30"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mark-square <span style="color: #66cc66;">&#40;</span>square state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Mark the designated square with the symbol of the current player's turn and toggle the turn&quot;</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>player <span style="color: #66cc66;">&#40;</span>first state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>board <span style="color: #66cc66;">&#40;</span>rest state<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;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> square board<span style="color: #66cc66;">&#41;</span> player<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq</span> 'x player<span style="color: #66cc66;">&#41;</span> 'y 'x<span style="color: #66cc66;">&#41;</span>
          board<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The problem with this is that <code>(rest state)</code> doesn&#8217;t return a copy of the <code>state</code> list starting with the second element, but rather it returns a pointer to the second cons cell in the <code>state</code> list. This means that we are dealing with same data in memory that was passed in and when we call <code>setf</code>, we modify the original <code>state</code>. More concretely if we try:</p>

<div class="wp_codebox"><table><tr id="p17431"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p174code31"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> state '<span style="color: #66cc66;">&#40;</span>x <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span>
                <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span>
                <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> new-state <span style="color: #66cc66;">&#40;</span>mark-square <span style="color: #cc66cc;">4</span> state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;old state: ~a~%&quot;</span> state<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;new state: ~a~%&quot;</span> new-state<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>we will get:</p>

<div class="wp_codebox"><table><tr id="p17432"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p174code32"><pre class="lisp" style="font-family:monospace;">old state<span style="color: #66cc66;">:</span> <span style="color: #66cc66;">&#40;</span>X <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> X <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span><span style="color: #66cc66;">&#41;</span>
new state<span style="color: #66cc66;">:</span> <span style="color: #66cc66;">&#40;</span>O <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> X <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span> <span style="color: #b1b100;">NIL</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This is what is known as a side-effect, a function that does something other than just return a value. It is a result of our using Common Lisp which is not a purely functional language. In a purely functional language this would not be possible. The fixed version of my <code>mark-square</code> function is:</p>

<div class="wp_codebox"><table><tr id="p17433"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p174code33"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mark-square <span style="color: #66cc66;">&#40;</span>square state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Mark the designated square with the symbol of the current player's turn and toggle the turn&quot;</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>player <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>board <span style="color: #66cc66;">&#40;</span>rest <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> state<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;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> square board<span style="color: #66cc66;">&#41;</span> player<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq</span> 'x player<span style="color: #66cc66;">&#41;</span> 'y 'x<span style="color: #66cc66;">&#41;</span>
          board<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The <code>copy-list</code> function creates new cons cells so the changes affect a new copy of the list.</p>
<h3>My Code</h3>
<p>I&#8217;m really happy with my code right now for this. You can download a copy of it by clicking <a href="/wp-content/files/adversarial.zip">here</a>.</p>
<p>You can run it at the command line with:</p>

<div class="wp_codebox"><table><tr id="p17434"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
</pre></td><td class="code" id="p174code34"><pre class="lisp" style="font-family:monospace;">$ cd /path/to/downloaded/directory
&nbsp;
$ clisp
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (load &quot;tic-tac-toe.lisp&quot;)</span>
T
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (play-game tic-tac-toe 10 'computer 'sam) ; the 10 here is maximum depth for adversarial search</span>
It is X's turn
 -----
&nbsp;
 -----
computer<span style="color: #66cc66;">:</span> <span style="color: #555;">BOTTOM-RIGHT</span>
&nbsp;
SAM's turn
It is Y's turn
 -----
&nbsp;
     X
 -----
&nbsp;
choose a move from
<span style="color: #66cc66;">&#40;</span>TOP-RIGHT BOTTOM-LEFT TOP-RIGHT TOP-LEFT CENTER MIDDLE-LEFT BOTTOM-CENTER
 MIDDLE-RIGHT TOP-CENTER<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span> <span style="color: #555;">top-left</span>
It is X's turn
 -----
 Y    
&nbsp;
     X
 -----
computer<span style="color: #66cc66;">:</span> <span style="color: #555;">TOP-RIGHT</span>
&nbsp;
SAM's turn
It is Y's turn
 -----
 Y   X
&nbsp;
     X
 -----
&nbsp;
choose a move from <span style="color: #66cc66;">&#40;</span>BOTTOM-LEFT CENTER MIDDLE-LEFT BOTTOM-CENTER MIDDLE-RIGHT TOP-CENTER<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span> <span style="color: #555;">middle-right</span>
It is X's turn
 -----
 Y   X
     Y
     X
 -----
computer<span style="color: #66cc66;">:</span> <span style="color: #555;">BOTTOM-LEFT</span>
&nbsp;
SAM's turn
It is Y's turn
 -----
 Y   X
     Y
 X   X
 -----
&nbsp;
choose a move from <span style="color: #66cc66;">&#40;</span>CENTER MIDDLE-LEFT BOTTOM-CENTER TOP-CENTER<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span> <span style="color: #555;">center</span>
It is X's turn
 -----
 Y   X
   Y Y
 X   X
 -----
computer<span style="color: #66cc66;">:</span> <span style="color: #555;">BOTTOM-CENTER</span>
It is Y's turn
 -----
 Y   X
   Y Y
 X X X
 -----
COMPUTER wins
<span style="color: #b1b100;">NIL</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; ^D</span>
&nbsp;
$ clisp
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (load &quot;othello&quot;)</span>
T
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; (play-game othello 5 'computer 'sam) ; the 5 here is maximum depth for adversarial search</span>
&nbsp;
SAM's turn
It is BLACK's turn
 ---------------
&nbsp;
       W B
       B W      
&nbsp;
 ---------------
&nbsp;
choose a move from <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>-<span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span>-<span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span>-<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span> <span style="color: #cc66cc;">3</span>-<span style="color: #cc66cc;">4</span>
It is WHITE's turn
 ---------------
&nbsp;
       B
       B B
       B W      
&nbsp;
 ---------------
computer<span style="color: #66cc66;">:</span> <span style="color: #cc66cc;">3</span>-<span style="color: #cc66cc;">3</span>
&nbsp;
SAM's turn
It is BLACK's turn
 ---------------
&nbsp;
     W B
       W B
       B W      
&nbsp;
 ---------------
&nbsp;
choose a move from <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>-<span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span>-<span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span>-<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span> <span style="color: #cc66cc;">5</span>-<span style="color: #cc66cc;">6</span>
It is WHITE's turn
 ---------------
&nbsp;
     W B
       W B
       B B B    
&nbsp;
 ---------------
computer<span style="color: #66cc66;">:</span> <span style="color: #cc66cc;">3</span>-<span style="color: #cc66cc;">5</span>
&nbsp;
SAM's turn
It is BLACK's turn
 ---------------
&nbsp;
     W W W
       W B
       B B B    
&nbsp;
 ---------------
&nbsp;
choose a move from <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>-<span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span>-<span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span>-<span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">2</span>-<span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span>
&nbsp;
<span style="color: #555;">etc</span><span style="color: #66cc66;">..</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/more-adversarial-search-tic-tac-toe-and-othello/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hard boiled eggs and the magic of shell scripts</title>
		<link>http://samauciello.com/hard-boiled-eggs-and-the-magic-of-shell-scripts/</link>
		<comments>http://samauciello.com/hard-boiled-eggs-and-the-magic-of-shell-scripts/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 21:36:40 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Shell Scripts]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=169</guid>
		<description><![CDATA[The fridge in the Science building at Marlboro College occasionally contains hard boiled eggs which can be obtained for 50¢ a piece. I say &#8220;occasionally&#8221; but sadly it would be more accurate to say &#8220;when Allison remembers to bring them&#8221;. &#8230; <a href="http://samauciello.com/hard-boiled-eggs-and-the-magic-of-shell-scripts/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The fridge in the Science building at Marlboro College occasionally contains hard boiled eggs which can be obtained for 50¢ a piece.  I say &#8220;occasionally&#8221; but sadly it would be more accurate to say &#8220;when Allison remembers to bring them&#8221;.  It occurred to me one day as I emailed her to remind her that I was doing something rather inefficient.  I remembered that I now live in a dorm with a nice kitchen.  I could purchase my own eggs for about $2 a dozen <span class="pren">(a 66% savings)</span> and boil them myself.  I finally got to town to buy eggs and today I have boiled a dozen eggs.  Just as the water was about to boil I realized I did not have an egg timer, so I made one.<span id="more-169"></span></p>
<p>There is of course nothing terribly exciting or new about the sleep command but the ability to quickly solve a practical problem with simple tools is always gratifying.  My egg timer is the following simple text file with executable bit set:</p>

<div class="wp_codebox"><table><tr id="p16936"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p169code36"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">600</span>;
osascript <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'beep 3'</span>
osascript <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'tell app &quot;System Events&quot; to display dialog &quot;Eggs Done!&quot;'</span></pre></td></tr></table></div>

<p>Yay eggs!</p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/hard-boiled-eggs-and-the-magic-of-shell-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alias fun and key bindings in zsh</title>
		<link>http://samauciello.com/alias-fun-and-key-bindings-in-zsh/</link>
		<comments>http://samauciello.com/alias-fun-and-key-bindings-in-zsh/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 19:10:10 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Shell Scripts]]></category>
		<category><![CDATA[ZSH]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=150</guid>
		<description><![CDATA[I use my terminal for everything. I use a program called Visor that keeps me a keystroke away from my terminal at all times. It is for this reason that I spend a considerable portion of my time customizing my &#8230; <a href="http://samauciello.com/alias-fun-and-key-bindings-in-zsh/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I use my terminal for everything.  I use a program called <a href="http://visor.binaryage.com/">Visor</a> that keeps me a keystroke away from my terminal at all times.  It is for this reason that I spend a considerable portion of my time customizing my shell.  I use <code>zsh</code> because it has some great features like considerably more helpful and customizable tab completion, and my personal favorite <code>autocd</code>.<span id="more-150"></span>  All you have to do is add the following to your <code>~/.zshrc</code> file:</p>

<div class="wp_codebox"><table><tr id="p15043"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p150code43"><pre class="bash" style="font-family:monospace;">setopt autocd</pre></td></tr></table></div>

<p>And suddenly instead of:</p>

<div class="wp_codebox"><table><tr id="p15044"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p150code44"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> some-directory<span style="color: #000000; font-weight: bold;">/</span>some-subdirectory</pre></td></tr></table></div>

<p>you can just use:</p>

<div class="wp_codebox"><table><tr id="p15045"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p150code45"><pre class="bash" style="font-family:monospace;">$ some-directory<span style="color: #000000; font-weight: bold;">/</span>some-subdirectory</pre></td></tr></table></div>

<p>and <code>zsh</code> will notice that it&#8217;s a directory and do the only sensible thing: go to it.</p>
<p>I also have a sizable number of time saving aliases:</p>

<div class="wp_codebox"><table><tr id="p15046"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p150code46"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">l</span>=<span style="color: #ff0000;">&quot;ls -lahG&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">mkd</span>=<span style="color: #ff0000;">&quot;mkdir -pv&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">dir</span>=<span style="color: #ff0000;">&quot;open ./&quot;</span></pre></td></tr></table></div>

<p><code>ls -lahG</code> is nice because it gives me so much information.  The <code>-l</code> flag formats the output into a vertical list that tells me more about each file.  The <code>-a</code> includes hidden files and directories that start with <code>.</code> like <code>.zshrc</code>.  The <code>-h</code> flag displays file sizes in a more readable format so if a file is 2262261 bytes long it will say <code>2.2M</code> instead of <code>2262261</code>.  The <code>-G</code> flag colors my files using a custom colorings scheme that I define in my <code>.zshenv</code> file.</p>
<p><code>mkdir -pv</code> is pretty straight forward.  The <code>-p</code> flag allows me to create nested directories with a single command so that if <code>~/foo</code> doesn&#8217;t exist then <code>mkd ~/foo/bar</code> will create both the foo and bar directories with one inside the other.  The <code>-v</code> just generates verbose output.</p>
<p><code>open ./</code> is a nifty Mac OS only command.  On a mac the open command will do the same thing as double clicking the file in the Finder.  It&#8217;s a handy link between my terminal and the rest of my mac.  In this case I can use it to quickly switch to the mac GUI interface for this directory if I need to for some reason.</p>
<p>I also have a number of alias that are simply shorter names for commands that I use all the time:</p>

<div class="wp_codebox"><table><tr id="p15047"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p150code47"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">m</span>=<span style="color: #ff0000;">&quot;less&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">e</span>=<span style="color: #ff0000;">&quot;edit&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">t</span>=<span style="color: #ff0000;">&quot;touch&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">sq</span>=<span style="color: #ff0000;">&quot;sqlite3&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">o</span>=<span style="color: #ff0000;">&quot;open&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ql</span>=<span style="color: #ff0000;">&quot;qlmanage -p&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">c</span>=<span style="color: #ff0000;">&quot;echo -n <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span> &gt;&quot;</span></pre></td></tr></table></div>

<p><code>edit</code> is a python script I made that determines my default text editor from a configuration file and then opens the file in that editor.  <code>qlmanage</code> is a mac specific program that does the same thing as pressing space-bar while highlighting a file in the Finder.</p>
<h3>Backspace vs. Delete</h3>
<p>Recently I had a problem with using <code>zsh</code> on certain ssh servers where my <code>delete</code> key <span class="pren">(the mac equivalent of windows&#8217; backspace)</span> was being mapped to the windows <code>del</code>.  That is to say it was deleting the next character instead of the previous.  I finally found <a href="http://macfreek.nl/mindmaster/Backspace_and_Delete_key_reversed">this wiki page</a> that explained the problem and how to fix it.  All I had to do was add the following to my <code>.zshrc</code> file:</p>

<div class="wp_codebox"><table><tr id="p15048"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p150code48"><pre class="bash" style="font-family:monospace;">bindkey <span style="color: #ff0000;">&quot;^?&quot;</span> backward-delete-char
bindkey <span style="color: #ff0000;">&quot;^[[3~&quot;</span> delete-char</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/alias-fun-and-key-bindings-in-zsh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alpha Beta min-max Search</title>
		<link>http://samauciello.com/alpha-beta-min-max-search/</link>
		<comments>http://samauciello.com/alpha-beta-min-max-search/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 14:49:11 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Lisp]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=127</guid>
		<description><![CDATA[I&#8217;ve been working on an adversarial search program for my Artificial Intelligence course. The idea is to make a program that can search the game tree of and play any two-player competitive game. It uses Lisp structures to define the &#8230; <a href="http://samauciello.com/alpha-beta-min-max-search/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on an adversarial search program for my Artificial Intelligence course.  The idea is to make a program that can search the game tree of and play any two-player competitive game.<span id="more-127"></span>  It uses Lisp structures to define the rules of a game.  The structure looks like this:</p>

<div class="wp_codebox"><table><tr id="p12756"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p127code56"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Game</span>
<span style="color: #66cc66;">&#40;</span>defstruct game
  <span style="color: #ff0000;">&quot;An object representing the rules of a game&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; The name of the game</span>
  <span style="color: #66cc66;">&#40;</span>initial-state <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; The state the game begins in</span>
  <span style="color: #66cc66;">&#40;</span>state-type '<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; A symbol representing the type of the state object</span>
  <span style="color: #66cc66;">&#40;</span>winning-score <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; The score associated with a win</span>
  <span style="color: #66cc66;">&#40;</span>score? <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; A function that takes a state and returns a score</span>
  <span style="color: #66cc66;">&#40;</span>terminal? <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; A function that takes a state and returns a score iff the state is</span>
                  <span style="color: #808080; font-style: italic;">; terminal, otherwise nil</span>
  <span style="color: #66cc66;">&#40;</span>moves? <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; a function that takes a state and returns a list of legal moves</span>
  <span style="color: #66cc66;">&#40;</span>print-state? <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; a function that prints out human readable version of the state</span></pre></td></tr></table></div>

<p>Additionally I created a function to generate a modified version of a game after a move has been made:</p>

<div class="wp_codebox"><table><tr id="p12757"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p127code57"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> game-progress <span style="color: #66cc66;">&#40;</span>game move<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Return a new game that is equivalent to the game but starts after move have been
 applied to its initial state&quot;</span>
  <span style="color: #66cc66;">&#40;</span>make-game
   <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>game-<span style="color: #b1b100;">name</span> game<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">initial-state</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> move <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span>game-state-type game<span style="color: #66cc66;">&#41;</span>
                                  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                  <span style="color: #66cc66;">&#40;</span>otherwise <span style="color: #66cc66;">&#40;</span>game-initial-state game<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;">:</span><span style="color: #555;">state-type</span> <span style="color: #66cc66;">&#40;</span>game-state-type game<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">winning-score</span> <span style="color: #66cc66;">&#40;</span>game-winning-score game<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">score</span>? <span style="color: #66cc66;">&#40;</span>game-score? game<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">terminal</span>? <span style="color: #66cc66;">&#40;</span>game-terminal? game<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">moves</span>? <span style="color: #66cc66;">&#40;</span>game-moves? game<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">print-state</span>? <span style="color: #66cc66;">&#40;</span>game-print-state? game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Note the use of <code>case</code> on line 6 to make sure that if the data-type of state is a list we are making a copy of the list, not a the original.</p>
<p>The other important structure I used was called <code>node</code> and I used it to keep track of positions in the search tree.</p>

<div class="wp_codebox"><table><tr id="p12758"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p127code58"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Node</span>
<span style="color: #66cc66;">&#40;</span>defstruct node
  <span style="color: #ff0000;">&quot;A position in a search tree&quot;</span>
  <span style="color: #66cc66;">&#40;</span>state <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>moves <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>score <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> new-node <span style="color: #66cc66;">&#40;</span>game <span style="color: #66cc66;">&amp;</span>key state moves<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Build a new node with a score generated by the game definition&quot;</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>new-state <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> state state <span style="color: #66cc66;">&#40;</span>game-initial-state game<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>make-node
     <span style="color: #66cc66;">:</span><span style="color: #555;">state</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span>game-state-type game<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> new-state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>otherwise new-state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">:</span><span style="color: #555;">moves</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> moves moves <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">:</span><span style="color: #555;">score</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-score? game<span style="color: #66cc66;">&#41;</span> new-state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> -- <span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Return a copy of the node with the opposite score&quot;</span>
  <span style="color: #66cc66;">&#40;</span>make-node
   <span style="color: #66cc66;">:</span><span style="color: #555;">state</span> <span style="color: #66cc66;">&#40;</span>node-state node<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">moves</span> <span style="color: #66cc66;">&#40;</span>node-moves node<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">score</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>node-score node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> do-move <span style="color: #66cc66;">&#40;</span>move node game<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Do the move to the node&quot;</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>new-state <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> move <span style="color: #66cc66;">&#40;</span>node-state node<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>make-node
     <span style="color: #66cc66;">:</span><span style="color: #555;">state</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span>game-state-type game<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> new-state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>otherwise new-state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">:</span><span style="color: #555;">moves</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> move <span style="color: #66cc66;">&#40;</span>node-moves node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">:</span><span style="color: #555;">score</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-score? game<span style="color: #66cc66;">&#41;</span> new-state<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></td></tr></table></div>

<p>Note the similar <code>case</code> statements.  The real meat of the program is the alpha-beta search itself:</p>

<div class="wp_codebox"><table><tr id="p12759"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p127code59"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; DFS</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> run-depth-first-search <span style="color: #66cc66;">&#40;</span>game max-depth<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;A wrapper function for the main DFS recursive function that should return the
 optimal move for the game&quot;</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>search-result
         <span style="color: #66cc66;">&#40;</span>depth-first-search max-depth game
                             <span style="color: #66cc66;">&#40;</span>make-node
                              <span style="color: #66cc66;">:</span><span style="color: #555;">state</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">:</span><span style="color: #555;">score</span> <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>game-winning-score game<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>make-node
                              <span style="color: #66cc66;">:</span><span style="color: #555;">state</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">:</span><span style="color: #555;">score</span> <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span>game-winning-score game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                             <span style="color: #66cc66;">&#40;</span>new-node game<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>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">last</span> <span style="color: #66cc66;">&#40;</span>node-moves <span style="color: #66cc66;">&#40;</span>first search-result<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>
&nbsp;
<span style="color: #808080; font-style: italic;">; basic algorithm based on Jim Mahoney's:</span>
<span style="color: #808080; font-style: italic;">; http://cs.marlboro.edu/courses/spring2010/programming/notes/mar_2.attachments/tictactoe.py_html</span>
<span style="color: #808080; font-style: italic;">; which in turn credits http://en.wikipedia.org/wiki/Alpha-beta_pruning</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> depth-first-search <span style="color: #66cc66;">&#40;</span>max-depth game alpha beta current <span style="color: #66cc66;">&amp;</span>optional <span style="color: #66cc66;">&#40;</span>depth <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Recursively search the game tree down to the max-depth&quot;</span>
  <span style="color: #66cc66;">&#40;</span>block loop
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> depth max-depth<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>return-from loop <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> current <span style="color: #b1b100;">nil</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;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-terminal? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>node-state current<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>return-from loop <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> current t<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;">do</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>possible-moves <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-moves? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>node-state current<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>move <span style="color: #66cc66;">&#40;</span>pop possible-moves<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pop possible-moves<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>result <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> move<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span>node-score beta<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>node-score alpha<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         result<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>search-result <span style="color: #66cc66;">&#40;</span>depth-first-search max-depth game <span style="color: #66cc66;">&#40;</span>-- beta<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>-- alpha<span style="color: #66cc66;">&#41;</span>
                                                <span style="color: #66cc66;">&#40;</span>do-move move current game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+ depth<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>effective-node <span style="color: #66cc66;">&#40;</span>-- <span style="color: #66cc66;">&#40;</span>pop search-result<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>terminal <span style="color: #66cc66;">&#40;</span>pop search-result<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;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>node-score effective-node<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>node-score alpha<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> alpha effective-node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> result <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> alpha terminal beta<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></pre></td></tr></table></div>

<p>The idea behind the <code>depth-first-search</code> function here is that is uses alpha and beta to keep track of nodes representing the best move it&#8217;s found for each player so far.  Each time through the loop checks to see if the current node is terminal or the maximum depth has been exceeded and returns in either case.  The return value <span class="pren">(lines 23 and 25)</span> is a list with the first value being the best node below this point and the second being whether or not the node is terminal <span class="pren">(This should probably be refactored to put this terminal flag in the <code>node</code> structure)</span>.</p>
<p>If we don&#8217;t immediately return we instead loop through the possible moves and for each one recursively call <code>depth-first-search</code>.  However in the call <span class="pren">(line 31)</span> we swap <span class="pren">(and &#8211;)</span> the <code>alpha</code> and <code>beta</code> nodes because we want to consider the best move for our opponent.</p>
<p>The actual alpha-beta pruning happens on line 29 where we set the condition for <code>do*</code> to end the loop through current possible moves.  The first condition is obvious, if we&#8217;ve checked all possible moves from this point then leave the loop.  The other, <code>(< (node-score beta) (node-score alpha))</code> is the alpha-beta part.  It ends the loop if a move has been found that proves this move to be worse than a previous best move.</p>
<p>I also made a simple </code><code>play-game</code> function that runs the game:</p>

<div class="wp_codebox"><table><tr id="p12760"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p127code60"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; Play</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> play-game <span style="color: #66cc66;">&#40;</span>game max-depth first-player second-player<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Play a game&quot;</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>terminal? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-terminal? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<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;">if</span> terminal? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
                    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-print-state? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~a wins~%&quot;</span>
                          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> terminal? <span style="color: #66cc66;">&#40;</span>game-winning-score game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                  first-player second-player<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;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eq</span> 'computer first-player<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>move <span style="color: #66cc66;">&#40;</span>run-depth-first-search game max-depth<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> <span style="color: #66cc66;">&#40;</span>game-print-state? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;computer: ~a~%&quot;</span> move<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>play-game <span style="color: #66cc66;">&#40;</span>game-progress game move<span style="color: #66cc66;">&#41;</span> max-depth second-player first-player<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
          <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~a's turn~%&quot;</span> first-player<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-print-state? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span><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>possible-moves
                  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> <span style="color: #66cc66;">&#40;</span>game-moves? game<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>game-initial-state game<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span>move <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>move <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
                                        <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;choose a move from ~a: &quot;</span>
                                                possible-moves<span style="color: #66cc66;">&#41;</span>
                                        <span style="color: #66cc66;">&#40;</span>read<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: #66cc66;">&#40;</span><span style="color: #b1b100;">member</span> move possible-moves<span style="color: #66cc66;">&#41;</span> move<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>play-game <span style="color: #66cc66;">&#40;</span>game-progress game move<span style="color: #66cc66;">&#41;</span>
                       max-depth second-player first-player<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;">&#41;</span></pre></td></tr></table></div>

<p>One nice thing about this function is that if neither <code>first-player</code> <code>second-player</code> is set to <code>'computer</code>, then two human players can play.</p>
<p>I made a simple game to test it out.  I call the game the stone game and it goes like this.  There are <code>n</code> stones in a pile.  Two players take turns removing 1, 2, or 3 stones.  The last player to remove a stone loses.  I defined my game in Lisp like so:</p>

<div class="wp_codebox"><table><tr id="p12761"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p127code61"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">; moves</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> take-<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>- state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> take-<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>- state <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> take-<span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>- state <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; game</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> starting-stones <span style="color: #cc66cc;">17</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> stone-game
      <span style="color: #66cc66;">&#40;</span>make-game
       <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;stone game&quot;</span>
       <span style="color: #66cc66;">:</span><span style="color: #555;">initial-state</span> starting-stones
       <span style="color: #66cc66;">:</span><span style="color: #555;">state-type</span> 'int
       <span style="color: #66cc66;">:</span><span style="color: #555;">winning-score</span> <span style="color: #cc66cc;">1</span>
       <span style="color: #66cc66;">:</span><span style="color: #555;">score</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> state<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> state<span style="color: #66cc66;">&#41;</span> -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>t <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">:</span><span style="color: #555;">terminal</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
                     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> state<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> state<span style="color: #66cc66;">&#41;</span> -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
                     <span style="color: #66cc66;">&#40;</span>t <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">:</span><span style="color: #555;">moves</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">2</span> state<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span> state<span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span>take-<span style="color: #cc66cc;">1</span> take-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>t '<span style="color: #66cc66;">&#40;</span>take-<span style="color: #cc66cc;">1</span> take-<span style="color: #cc66cc;">2</span> take-<span style="color: #cc66cc;">3</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;">:</span><span style="color: #555;">print-state</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>state<span style="color: #66cc66;">&#41;</span>
                       <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;There are ~a stone(s) left~%&quot;</span> state<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></td></tr></table></div>

<p>Here&#8217;s an example of the game in action:</p>

<div class="wp_codebox"><table><tr id="p12762"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p127code62"><pre class="sh" style="font-family:monospace;">[]&gt; (play-game stone-game 20 'sam 'computer)
SAM's turn
There are 17 stone(s) left
choose a move from (TAKE-1 TAKE-2 TAKE-3): take-2
There are 15 stone(s) left
computer: TAKE-2
SAM's turn
There are 13 stone(s) left
choose a move from (TAKE-1 TAKE-2 TAKE-3): take-1
There are 12 stone(s) left
computer: TAKE-3
SAM's turn
There are 9 stone(s) left
choose a move from (TAKE-1 TAKE-2 TAKE-3): take-3
There are 6 stone(s) left
computer: TAKE-1
SAM's turn
There are 5 stone(s) left
choose a move from (TAKE-1 TAKE-2 TAKE-3): take-2
There are 3 stone(s) left
computer: TAKE-2
There are 1 stone(s) left
COMPUTER wins</pre></td></tr></table></div>

<p>Note that the 20 on line 1 sets the maximum search depth for the ai not the number of stones.</p>
<p>My code could use some refactoring and I hope to revisit it at some point. I&#8217;m also working on making it solve tic-tac-toe.</p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/alpha-beta-min-max-search/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Problems with class inheritance in PHP</title>
		<link>http://samauciello.com/problems-with-class-inheritance-in-php/</link>
		<comments>http://samauciello.com/problems-with-class-inheritance-in-php/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 21:34:08 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Object-Oriented Programing]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=116</guid>
		<description><![CDATA[I was working on a PHP database model for work the other day. I should back up here and say that I was trying to do the sort of thing you might do in Django or RoR, to make classes &#8230; <a href="http://samauciello.com/problems-with-class-inheritance-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was working on a PHP database model for work the other day.  I should back up here and say that I was trying to do the sort of thing you might do in Django or RoR, to make classes that define a type of database record.  In retrospect I was probably making the mistake of thinking in terms of languages more powerful than PHP.<span id="more-116"></span>  Here is an example in Python of something like what I was trying to do:</p>

<div class="wp_codebox"><table><tr id="p11668"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p116code68"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DbRecord :
	table = <span style="color: #483d8b;">&quot;&quot;</span>
	fields = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> _fieldsForQuery<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span> :
		out = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> field <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">fields</span> :
			out.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s.%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>.<span style="color: black;">table</span> field<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;, &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>out<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> load<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span> :
		<span style="color: #808080; font-style: italic;"># load a record from the db</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> save<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span> :
		<span style="color: #808080; font-style: italic;"># save a record to the db</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Place<span style="color: black;">&#40;</span>DbRecord<span style="color: black;">&#41;</span> :
	table = <span style="color: #483d8b;">&quot;place&quot;</span>
	fields = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;id&quot;</span>, <span style="color: #483d8b;">&quot;name&quot;</span>, <span style="color: #483d8b;">&quot;description&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Person<span style="color: black;">&#40;</span>DbRecord<span style="color: black;">&#41;</span> :
	table = <span style="color: #483d8b;">&quot;person&quot;</span>
	fields = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;id&quot;</span>, <span style="color: #483d8b;">&quot;name&quot;</span>, <span style="color: #483d8b;">&quot;email&quot;</span>, <span style="color: #483d8b;">&quot;place_id&quot;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> getAddress<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span> :
		sql = <span style="color: #483d8b;">&quot;SELECT %s FROM %s, %s WHERE %s.id = %s&quot;</span> <span style="color: #66cc66;">%</span>
			<span style="color: black;">&#40;</span>Place.<span style="color: black;">fieldsForQuery</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
			<span style="color: #008000;">self</span>.<span style="color: black;">table</span>,
			Place.<span style="color: black;">table</span>,
			Place.<span style="color: black;">table</span>,
			<span style="color: #008000;">self</span>.<span style="color: black;">place_id</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">place</span> = Place.<span style="color: black;">load</span><span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>For starters, the PHP equivalent of this looks quite different.  Notably, there are no class variables in PHP.  For example:</p>

<div class="wp_codebox"><table><tr id="p11669"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p116code69"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>results in errors, while</p>

<div class="wp_codebox"><table><tr id="p11670"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p116code70"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$var</span> <span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>is fine.</p>
<p>But since I want to access these variables without instantiating the class <span class="pren">(see for example line 25 in the above python code)</span> this will not do.  There are class constants in php for example:</p>

<div class="wp_codebox"><table><tr id="p11671"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p116code71"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">const</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>but, among other issues, they cannot be used to store arrays.</p>
<p>This leaves us with few options.  The one I chose was to make functions with the sole purpose of returning a fixed value.  It ended up looking something like this:</p>

<div class="wp_codebox"><table><tr id="p11672"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p116code72"><pre class="php" style="font-family:monospace;">abstract <span style="color: #000000; font-weight: bold;">class</span> DbRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> _fieldsForQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fields</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$field</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$out</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span> <span style="color: #000088;">$field</span> <span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># load a record from the db
</span>	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;"># save a record to the db
</span>	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Place <span style="color: #000000; font-weight: bold;">extends</span> DbRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> table<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'place'</span> <span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">function</span> fields<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Person<span style="color: #009900;">&#40;</span>DbRecord<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> table<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'person'</span> <span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">function</span> fields<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'place_id'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">function</span> getAddress<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
			SELECT '</span> <span style="color: #339933;">.</span> Place<span style="color: #339933;">::</span><span style="color: #004000;">fieldsForQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'
			FROM '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', '</span> <span style="color: #339933;">.</span> Place<span style="color: #339933;">::</span><span style="color: #004000;">table</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'
			WHERE '</span> <span style="color: #339933;">.</span> Place<span style="color: #339933;">::</span><span style="color: #004000;">table</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.id = '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">place_id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'
		'</span> <span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">place</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Place<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">place</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Of course this doesn&#8217;t even work.  As it turns out the <code>fieldsForQuery</code> method called on line 27 here returns the formatted fields from <code>Person</code>, not from <code>Place</code>.  The moral of the story here is probably that PHP wasn&#8217;t designed to support non-trivial complex inheritance.</p>
]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/problems-with-class-inheritance-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Complex string operations in php</title>
		<link>http://samauciello.com/complex-string-operations-in-php/</link>
		<comments>http://samauciello.com/complex-string-operations-in-php/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 23:45:18 +0000</pubDate>
		<dc:creator>Sam Auciello</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://samauciello.com/?p=81</guid>
		<description><![CDATA[Since I&#8217;m in the process of revamping my website, I was looking at the rates page and I noticed that when I first put the site together I had manually entered all of the example prices as static HTML. The &#8230; <a href="http://samauciello.com/complex-string-operations-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since I&#8217;m in the process of revamping my website, I was looking at the <a href="/rates">rates</a> page and I noticed that when I first put the site together I had manually entered all of the example prices as static HTML.  The problem with this is that my basic hourly rate isn&#8217;t a static thing, and while it&#8217;s easy enough to change one number at the top, it&#8217;s kind of a hassle to do all of the multiplication out when I could easily make PHP do all the work form for me.  Or so I thought. <span id="more-81"></span></p>
<p>It would have been simple of course if I hadn&#8217;t just upgraded my site to WordPress.  If my site were a simpler PHP based site I could have done something like this:</p>

<div class="wp_codebox"><table><tr id="p8182"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p81code82"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
	<span style="color: #000000; font-weight: bold;">function</span> money<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hours</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$hourlyRate</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">35</span> <span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/number_format"><span style="color: #990000;">number_format</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hourlyRate</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$hours</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// the 2 here specifies how many decimal places to display</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">...</span>
&nbsp;
Some job that takes two and a half hours costs $<span style="color: #339933;">&lt;</span> ?php money<span style="color: #009900;">&#40;</span><span style="color:#800080;">2.5</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The problem with this approach is that WordPress stores the content of its pages in a database.  It does however provide ways to modify the content dynamically before it&#8217;s printed.  If you&#8217;re doing something small that doesn&#8217;t warrant writing a whole plugin, you can do this by editing the <code>functions.php</code> file in your WordPress theme.</p>
<p>WordPress provides a number of hooks, or events that you can attach your own php functions to to change how things work.  For this project the relevant hook is called <code>'the_content'</code> and can be invoked using</p>

<div class="wp_codebox"><table><tr id="p8183"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p81code83"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_function_name'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>where <code>my_function_name</code> is the name of a function that takes a string as an argument and returns a modified version of the string.</p>
<p>So the idea here is that when I edit my rates page in WordPress, I should be able to add tags like</p>

<div class="wp_codebox"><table><tr id="p8184"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p81code84"><pre class="html" style="font-family:monospace;">Some job that takes two and a half hours costs ${MONEY 2.5}</pre></td></tr></table></div>

<p>and my hooked function can use <a href="http://www.regular-expressions.info/reference.html">regular expressions</a> to replace the tag with the appropriate amount.  There&#8217;s a snag though; regular expressions can easily find the string using <code>/{MONEY ([\d\.]+)}/</code> but performing floating point multiplication is outside the scope of a simple <code>preg_replace</code> call.</p>
<p>The solution is an excellent little function called <code>preg_replace_callback</code>.  <code>preg_replace_callback</code> works like <code>preg_replace</code> except that it replaces the matched strings using function.  A simple solution to my problem that works as of PHP 5.3 would have been to add the following to my <code>functions.php</code> file</p>

<div class="wp_codebox"><table><tr id="p8185"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p81code85"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate_money<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$hourlyRate</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">35</span> <span style="color: #339933;">;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'/{MONEY ([\d\.]+)}/'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// a regex expression to find the tags</span>
		<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$hours</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// retrieve the parenthetical pattern</span>
			<span style="color: #b1b100;">return</span> <a href="http://www.php.net/number_format"><span style="color: #990000;">number_format</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hourlyRate</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$hours</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #000088;">$text</span>
	<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'calculate_money'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That nameless function on line 5 is called an anonymous function.  Anonymous functions were introduced in PHP 5.3. Before PHP 5.3 you would need to use:</p>

<div class="wp_codebox"><table><tr id="p8186"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p81code86"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate_money<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$hourlyRate</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">35</span> <span style="color: #339933;">;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'/{MONEY ([\d\.]+)}/'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// a regex expression to find the tags</span>
		<a href="http://www.php.net/create_function"><span style="color: #990000;">create_function</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'$matches'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'$hours = (float)$matches[1] ; // retrieve the parenthetical pattern
			return number_format('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$hourlyRate</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' * $hours, 2) ;'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #000088;">$text</span>
	<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'calculate_money'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There are a couple of important things to note here.  The first is that I&#8217;m using single quoted strings in my <code>create_function</code>.  If I use double quotes, I need to escape all of my dollar signs with <code>"\$"</code> to prevent PHP from attempting to interpolate variables.</p>
<p>The second important thing here is that the <code>$hourlyRate</code> variable that I defined outside of the function must be concatenated (or interpolated) into the string effectively hard-coding it into the anonymous function we are creating.  The reason this is necessary here and not in the PHP 5.3 version is that the anonymous function defined in the previous example forms what is called a closure. This means that it can see the local variables from the scope in which it was defined.</p>
<p>At any rate neither of these was what I wanted.  You see, I don&#8217;t just have a basic rate, I also have a rate for more complex work.  And sure I could have just defined two variables and had two kinds of tag but this is WordPress; I should be able to manage my site using the WordPress interface not by editing hard-coded values in <code>functions.php</code>.</p>
<p>My solution was to set up two kinds of tags, variable tags and money tags.  When I edit my rates page in WordPress I could make my page look something like the following:</p>

<div class="wp_codebox"><table><tr id="p8187"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p81code87"><pre class="html" style="font-family:monospace;">{VAR basicRate 35}
{VAR complexRate 40}
&nbsp;
...
&nbsp;
Some basic job that takes two and a half hours costs ${MONEY basicRate 2.5}</pre></td></tr></table></div>

<p>The code that I add to <code>functions.php</code> might look like:</p>

<div class="wp_codebox"><table><tr id="p8188"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p81code88"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate_money<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$vars</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// find the variable tags and store them in $matches</span>
	<a href="http://www.php.net/preg_match_all"><span style="color: #990000;">preg_match_all</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/{VAR ([a-zA-Z]+) ([\d\.]+)}/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// store the variables in the $vars array</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$vars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// remove the tags so they aren't displayed on the page</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/{VAR[^}]*}/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace_callback"><span style="color: #990000;">preg_replace_callback</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'/{MONEY (a-zA-Z]+) ([\d\.]+)}/'</span><span style="color: #339933;">,</span>
		<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// retrieve the first parenthetical pattern</span>
			<span style="color: #000088;">$rate</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vars</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// retrieve the second parenthetical pattern</span>
			<span style="color: #000088;">$hours</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <a href="http://www.php.net/number_format"><span style="color: #990000;">number_format</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rate</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$hours</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #000088;">$text</span>
	<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'calculate_money'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Of course prior to PHP 5.3 this would not be so simple because the anonymous function can&#8217;t see the <code>$vars</code> array without a closure.  One solution might look like this:</p>

<div class="wp_codebox"><table><tr id="p8189"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p81code89"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate_money<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$vars</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// find the variable tags and store them in $matches</span>
	<a href="http://www.php.net/preg_match_all"><span style="color: #990000;">preg_match_all</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/{VAR ([a-zA-Z]+) ([\d\.]+)}/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// store the variables in the $vars array</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$vars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// remove the tags so they aren't displayed on the page</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/{VAR[^}]*}/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vars</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace_callback"><span style="color: #990000;">preg_replace_callback</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'/{MONEY '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' ([\d\.]+)}/'</span><span style="color: #339933;">,</span>
			<a href="http://www.php.net/create_function"><span style="color: #990000;">create_function</span></a><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">'$matches'</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">'$hours = (float)$matches[1] ;
				return number_format('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' * $hours, 2) ;'</span>
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #000088;">$text</span>
		<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'calculate_money'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Another possible pre PHP 5.3 solution is:</p>

<div class="wp_codebox"><table><tr id="p8190"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p81code90"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> calculate_money<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$vars</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// find the variable tags and store them in $matches</span>
	<a href="http://www.php.net/preg_match_all"><span style="color: #990000;">preg_match_all</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/{VAR ([a-zA-Z]+) ([\d\.]+)}/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// store the variables in the $vars array</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$var</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$vars</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// remove the tags so they aren't displayed on the page</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/{VAR[^}]*}/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace_callback"><span style="color: #990000;">preg_replace_callback</span></a><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'/{MONEY (a-zA-Z]+) ([\d\.]+)}/'</span><span style="color: #339933;">,</span>
		<a href="http://www.php.net/create_function"><span style="color: #990000;">create_function</span></a><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'$matches'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'$hours = (float)$matches[1] ;
			$vars = '</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/var_export"><span style="color: #990000;">var_export</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vars</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' ;
			// the second argument above causes it to return instead of echoing
			$rate = $vars[$matches[2]] ;
			return number_format($rate * $hours, 2) ;'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #000088;">$text</span>
	<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'calculate_money'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://samauciello.com/complex-string-operations-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

