<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Beautiful Code</title>
	<atom:link href="http://programmingpraxis.com/2009/09/11/beautiful-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/09/11/beautiful-code/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Lautaro Pecile</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-4845</link>
		<dc:creator><![CDATA[Lautaro Pecile]]></dc:creator>
		<pubDate>Sat, 19 May 2012 01:34:47 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-4845</guid>
		<description><![CDATA[[sourcecode lang=&quot;python&quot;]
from operator import truth as not_empty

is_empty = lambda s: not not_empty(s)

def iterate(f, string):
    s = string
    while not_empty(s):
        yield f(s)
        s = s[1:]

def match (re, text):    
    if re.startswith(&#039;^&#039;):
        return matchhere(re[1:], text)
    return any(iterate(lambda t: matchhere(re, t), text))
        

def matchhere(re, text):
    if is_empty(re):
        return True
    if re.startswith(&#039;*&#039;, 1):
        return matchstar(re[0], re[2:], text)
    if re.startswith(&#039;$&#039;):
        return is_empty(text)
    if (not_empty(text) and (re.startswith(&#039;.&#039;) or re[0] == text[0])):
        return matchhere(re[1:], text[1:])
    return False
    
def matchstar(c, re, text):
    while True:                
        if matchhere(re, text):
            return True
        text = text[1:]
        if is_empty(text) or not (text.startswith(c) or c == &#039;.&#039;):
            break
    return False

[/sourcecode]]]></description>
		<content:encoded><![CDATA[<pre class="brush: python;">
from operator import truth as not_empty

is_empty = lambda s: not not_empty(s)

def iterate(f, string):
    s = string
    while not_empty(s):
        yield f(s)
        s = s[1:]

def match (re, text):    
    if re.startswith('^'):
        return matchhere(re[1:], text)
    return any(iterate(lambda t: matchhere(re, t), text))
        

def matchhere(re, text):
    if is_empty(re):
        return True
    if re.startswith('*', 1):
        return matchstar(re[0], re[2:], text)
    if re.startswith('$'):
        return is_empty(text)
    if (not_empty(text) and (re.startswith('.') or re[0] == text[0])):
        return matchhere(re[1:], text[1:])
    return False
    
def matchstar(c, re, text):
    while True:                
        if matchhere(re, text):
            return True
        text = text[1:]
        if is_empty(text) or not (text.startswith(c) or c == '.'):
            break
    return False

</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michel</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-720</link>
		<dc:creator><![CDATA[Michel]]></dc:creator>
		<pubDate>Sun, 25 Oct 2009 06:04:58 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-720</guid>
		<description><![CDATA[My Clojure solution. Clojure uses Java strings, which are immutable, so taking substring would be too expensive. Instead, I made match-here and match-* internal functions, and give them indices rather than strings to work with.

&lt;a href=&quot;http://github.com/hircus/clj-puzzles/blob/master/regex.clj&quot; rel=&quot;nofollow&quot;&gt;Solution, on GitHub&lt;/a&gt;]]></description>
		<content:encoded><![CDATA[<p>My Clojure solution. Clojure uses Java strings, which are immutable, so taking substring would be too expensive. Instead, I made match-here and match-* internal functions, and give them indices rather than strings to work with.</p>
<p><a href="http://github.com/hircus/clj-puzzles/blob/master/regex.clj" rel="nofollow">Solution, on GitHub</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luis Sergio Oliveira</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-711</link>
		<dc:creator><![CDATA[Luis Sergio Oliveira]]></dc:creator>
		<pubDate>Thu, 22 Oct 2009 06:50:49 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-711</guid>
		<description><![CDATA[My Common Lisp solution:
[sourcecode lang=&quot;css&quot;]
(defun empty (seq)
  (declare (inline))
  (= 0 (length seq)))

(defun match (re text)
  &quot;search for re anywhere in text&quot;
  (if (and (not (empty re)) (char= #\^ (elt re 0)))
      (matchhere (subseq re 1) text)
      ;; must look at empty string
      (cond ((matchhere re text))
	    ((empty text)
	     nil)
	    ((match re (subseq text 1))))))

(defun matchhere (re text)
  &quot;search for re at beginning of text&quot;
  (cond ((empty re))
	((and (&lt; 1 (length re)) (char= #\* (elt re 1)))
	 (matchstar (elt re 0) (subseq re 2) text))
	((and (char= #\$ (elt re 0)) (= 1 (length re)))
	 (empty text))
	((and (not (empty text))
	      (or (char= #\. (elt re 0)) (char= (elt re 0) (elt text 0))))
	 (matchhere (subseq re 1) (subseq text 1)))))

(defun matchstar (c re text)
  &quot;search for c*re at beginning of text&quot;
  (cond ((matchhere re text))
	((and (not (empty text))
	      (or (char= c (elt text 0)) (char= #\. c)))
	 (matchstar c re (subseq text 1)))))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Common Lisp solution:</p>
<pre class="brush: css;">
(defun empty (seq)
  (declare (inline))
  (= 0 (length seq)))

(defun match (re text)
  &quot;search for re anywhere in text&quot;
  (if (and (not (empty re)) (char= #\^ (elt re 0)))
      (matchhere (subseq re 1) text)
      ;; must look at empty string
      (cond ((matchhere re text))
	    ((empty text)
	     nil)
	    ((match re (subseq text 1))))))

(defun matchhere (re text)
  &quot;search for re at beginning of text&quot;
  (cond ((empty re))
	((and (&lt; 1 (length re)) (char= #\* (elt re 1)))
	 (matchstar (elt re 0) (subseq re 2) text))
	((and (char= #\$ (elt re 0)) (= 1 (length re)))
	 (empty text))
	((and (not (empty text))
	      (or (char= #\. (elt re 0)) (char= (elt re 0) (elt text 0))))
	 (matchhere (subseq re 1) (subseq text 1)))))

(defun matchstar (c re text)
  &quot;search for c*re at beginning of text&quot;
  (cond ((matchhere re text))
	((and (not (empty text))
	      (or (char= c (elt text 0)) (char= #\. c)))
	 (matchstar c re (subseq text 1)))))
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Beautiful Code « Programming Praxis &#124; what a beautiful day..</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-608</link>
		<dc:creator><![CDATA[Beautiful Code « Programming Praxis &#124; what a beautiful day..]]></dc:creator>
		<pubDate>Mon, 14 Sep 2009 03:06:17 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-608</guid>
		<description><![CDATA[[...] Beautiful Code « Programming Praxis. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Beautiful Code « Programming Praxis. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tordek</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-606</link>
		<dc:creator><![CDATA[Tordek]]></dc:creator>
		<pubDate>Sat, 12 Sep 2009 06:10:49 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-606</guid>
		<description><![CDATA[Assuming strings as lists of characters (They&#039;re usually atoms, so they should be transformed):

Prolog solution.

&lt;code&gt;&lt;pre&gt;
match([&#039;^&#039;&#124;Rs], T)          :- matchHere(Rs, T).
match(R, T)                 :- matchHere(R, T).
match(R, [_&#124;Ts])            :- matchHere(R, Ts).

matchHere([], _).
matchHere([&#039;$&#039;], []).
matchHere([R,&#039;*&#039;&#124;Rs], T)    :- matchStar(R, Rs, T).
matchHere([R&#124;Rs], [R&#124;Ts])   :- matchHere(Rs, Ts).
matchHere([&#039;.&#039;&#124;Rs], [_&#124;Ts]) :- matchHere(Rs, Ts).

matchStar(_, Rs, Ts)        :- matchHere(Rs, Ts).
matchStar(R, Rs, [R&#124;Ts])    :- matchHere(Rs, Ts).
matchStar(&#039;.&#039;, Rs, [_&#124;Ts])  :- matchHere(Rs, Ts).
matchStar(R, Rs, [R&#124;Ts])    :- matchStar(R, Rs, Ts).
matchStar(&#039;.&#039;, Rs, [_&#124;Ts])  :- matchStar(&#039;.&#039;, Rs, Ts).&lt;/pre&gt;&lt;/code&gt;]]></description>
		<content:encoded><![CDATA[<p>Assuming strings as lists of characters (They&#8217;re usually atoms, so they should be transformed):</p>
<p>Prolog solution.</p>
<p><code>
<pre>
match(['^'|Rs], T)          :- matchHere(Rs, T).
match(R, T)                 :- matchHere(R, T).
match(R, [_|Ts])            :- matchHere(R, Ts).

matchHere([], _).
matchHere(['$'], []).
matchHere([R,'*'|Rs], T)    :- matchStar(R, Rs, T).
matchHere([R|Rs], [R|Ts])   :- matchHere(Rs, Ts).
matchHere(['.'|Rs], [_|Ts]) :- matchHere(Rs, Ts).

matchStar(_, Rs, Ts)        :- matchHere(Rs, Ts).
matchStar(R, Rs, [R|Ts])    :- matchHere(Rs, Ts).
matchStar('.', Rs, [_|Ts])  :- matchHere(Rs, Ts).
matchStar(R, Rs, [R|Ts])    :- matchStar(R, Rs, Ts).
matchStar('.', Rs, [_|Ts])  :- matchStar('.', Rs, Ts).</pre>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-605</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 11 Sep 2009 17:12:31 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-605</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/09/11/programming-praxis-beautiful-code/ for a version with comments):

[sourcecode lang=&#039;css&#039;]
import Data.List

match :: String -&gt; String -&gt; Bool
match (&#039;^&#039;:r) = matchHere r
match r       = or . map (matchHere r) . tails

matchHere :: String -&gt; String -&gt; Bool
matchHere (c:&#039;*&#039;:r) xs  = matchStar c r xs
matchHere &quot;$&quot;       xs  = null xs
matchHere (r:rs) (x:xs) = (r == &#039;.&#039; &#124;&#124; r == x) &amp;&amp; matchHere rs xs
matchHere r      _      = null r

matchStar :: Char -&gt; String -&gt; String -&gt; Bool
matchStar _ r xs     &#124; matchHere r xs = True
matchStar c r (x:xs) = (c == &#039;.&#039; &#124;&#124; c == x) &amp;&amp; matchStar c r xs
matchStar _ _ _      = False
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/09/11/programming-praxis-beautiful-code/" rel="nofollow">http://bonsaicode.wordpress.com/2009/09/11/programming-praxis-beautiful-code/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.List

match :: String -&gt; String -&gt; Bool
match ('^':r) = matchHere r
match r       = or . map (matchHere r) . tails

matchHere :: String -&gt; String -&gt; Bool
matchHere (c:'*':r) xs  = matchStar c r xs
matchHere &quot;$&quot;       xs  = null xs
matchHere (r:rs) (x:xs) = (r == '.' || r == x) &amp;&amp; matchHere rs xs
matchHere r      _      = null r

matchStar :: Char -&gt; String -&gt; String -&gt; Bool
matchStar _ r xs     | matchHere r xs = True
matchStar c r (x:xs) = (c == '.' || c == x) &amp;&amp; matchStar c r xs
matchStar _ _ _      = False
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Beautiful Code &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/09/11/beautiful-code/#comment-604</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Beautiful Code &#171; Bonsai Code]]></dc:creator>
		<pubDate>Fri, 11 Sep 2009 17:12:16 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1309#comment-604</guid>
		<description><![CDATA[[...] Praxis &#8211; Beautiful&#160;Code By Remco Niemeijer  Today&#8217;s Programming Praxis is about beautiful code. Specifically, it concerns a bit of C code that can [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Beautiful&nbsp;Code By Remco Niemeijer  Today&#8217;s Programming Praxis is about beautiful code. Specifically, it concerns a bit of C code that can [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

