<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.6" -->
<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/"
	>

<channel>
	<title>David Tran's blog</title>
	<link>http://davidtran.doublegifts.com/blog</link>
	<description></description>
	<pubDate>Fri, 02 Dec 2011 03:56:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.6</generator>
	<language>en</language>
			<item>
		<title>Macbook Pro</title>
		<link>http://davidtran.doublegifts.com/blog/?p=169</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=169#comments</comments>
		<pubDate>Fri, 02 Dec 2011 03:56:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Life</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=169</guid>
		<description><![CDATA[2011-11-23 BlackFriday purchased macbook pro
2011-11-30 Received
2011-12-01 First post via macbook pro

]]></description>
			<content:encoded><![CDATA[<p>2011-11-23 BlackFriday purchased macbook pro<br />
2011-11-30 Received<br />
2011-12-01 First post via macbook pro
</p>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=169</wfw:commentRss>
		</item>
		<item>
		<title>My Java 7 Presentation</title>
		<link>http://davidtran.doublegifts.com/blog/?p=168</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=168#comments</comments>
		<pubDate>Mon, 19 Sep 2011 18:20:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Java</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=168</guid>
		<description><![CDATA[Slides and Demos

]]></description>
			<content:encoded><![CDATA[<p><a href="http://davidtran.doublegifts.com/blog/files/2011-09-14_java7.zip">Slides and Demos</a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=168</wfw:commentRss>
		</item>
		<item>
		<title>Sum Columns</title>
		<link>http://davidtran.doublegifts.com/blog/?p=167</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=167#comments</comments>
		<pubDate>Tue, 24 May 2011 02:13:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Haskell</category>

		<category>Ruby</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=167</guid>
		<description><![CDATA[
Problem:
  Input .... : a "matrix" of numbers.
  Output ... : each column's sum.

Example:
Input (stdin):
1 2 3 4 5
2 6 10 1 1
3 3 3 4 4
5 4 3 2 0

Output (stdout):
11 15 19 11 10

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ruby one-liner
==============
puts readlines.map{&#124;l&#124;l.split.map(&#038;:to_i)}.transpose.map{&#124;c&#124;c.inject(&#038;:+)}.join(' ')

Haskell almost one-liner
========================
import Data.List (transpose)
main = interact $ unwords . map (show . sum . [...]]]></description>
			<content:encoded><![CDATA[<pre><code>
Problem:
  Input .... : a "matrix" of numbers.
  Output ... : each column's sum.

Example:
Input (stdin):
1 2 3 4 5
2 6 10 1 1
3 3 3 4 4
5 4 3 2 0

Output (stdout):
11 15 19 11 10

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ruby one-liner
==============
puts readlines.map{|l|l.split.map(&#038;:to_i)}.transpose.map{|c|c.inject(&#038;:+)}.join(' ')

Haskell almost one-liner
========================
import Data.List (transpose)
main = interact $ unwords . map (show . sum . map read) . transpose . map words . lines
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=167</wfw:commentRss>
		</item>
		<item>
		<title>Ugly Numbers</title>
		<link>http://davidtran.doublegifts.com/blog/?p=166</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=166#comments</comments>
		<pubDate>Mon, 23 May 2011 00:33:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Haskell</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=166</guid>
		<description><![CDATA[

{-
 - Problem: Ugly Numbers  ( http://uva.onlinejudge.org/external/1/136.html )
 -
 - Ugly numbers are numbers whose only prime factors are 2, 3 or 5.
 - The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
 - shows the first 11 ugly numbers. By convention, 1 is included.
 -
 - Write a [...]]]></description>
			<content:encoded><![CDATA[<p><code>
<pre>
{-
 - Problem: Ugly Numbers  ( http://uva.onlinejudge.org/external/1/136.html )
 -
 - Ugly numbers are numbers whose only prime factors are 2, 3 or 5.
 - The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
 - shows the first 11 ugly numbers. By convention, 1 is included.
 -
 - Write a program to find and print the 1500'th ugly number.
 -}

ugly :: [Integer]
ugly = 1 : merge (map (2*) ugly) (map (3*) ugly) (map (5*) ugly)
  where merge xxs@(x:xs) yys@(y:ys) zzs@(z:zs)
          | x < y &#038;&#038; x < z  =  x : merge xs yys zzs
          | y < x &#038;&#038; y < z  =  y : merge xxs ys zzs
          | z < x &#038;&#038; z < y  =  z : merge xxs yys zs
          | x == y          =      merge xs yys zzs
          | y == z          =      merge xxs ys zzs
          | z == x          =      merge xxs yys zs

main = putStrLn $ "The 1500'th ugly number is " ++ show (ugly !! 1499) ++ "."
</pre>
<p></code>
</p>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=166</wfw:commentRss>
		</item>
		<item>
		<title>My first two BlackBerry PlayBook applications</title>
		<link>http://davidtran.doublegifts.com/blog/?p=165</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=165#comments</comments>
		<pubDate>Sun, 27 Mar 2011 02:54:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Flex / ActionScript</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=165</guid>
		<description><![CDATA[First application: Chinese Calendar and Zodiac
Start around 2010-12-03; part time work on it; application finished and submitted on 2011-01-21.
Approved by RIM on 2011-02-14 and get a free PlayBook offer email on 2011-02-15.
Second application: Peg Solitaires
Start on 2011-01-27; part time work on it; application finished and submitted on 2011-02-13 and approved by RIM on 2011-02-21.
PlayBook will [...]]]></description>
			<content:encoded><![CDATA[<li>First application: <a href="http://www.doublegifts.com/product/ccalendar/ccalendar.html">Chinese Calendar and Zodiac</a></li>
<p>Start around 2010-12-03; part time work on it; application finished and submitted on 2011-01-21.<br />
Approved by RIM on 2011-02-14 and get a free PlayBook offer email on 2011-02-15.</p>
<li>Second application: <a href="http://www.doublegifts.com/product/peg-solitaire/peg-solitaire.html">Peg Solitaires</a></li>
<p>Start on 2011-01-27; part time work on it; application finished and submitted on 2011-02-13 and approved by RIM on 2011-02-21.</p>
<p>PlayBook will be my first tablet!
</p>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=165</wfw:commentRss>
		</item>
		<item>
		<title>Bracket balance checking regex (2)</title>
		<link>http://davidtran.doublegifts.com/blog/?p=164</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=164#comments</comments>
		<pubDate>Thu, 07 Oct 2010 00:19:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Ruby</category>

		<category>Regex</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=164</guid>
		<description><![CDATA[
=begin
Bracket balance checking regex (2)
==================================

&#60;s&#62; ::= &#60;p&#62;*
&#60;p&#62; ::= &#60;non_bracket_chars&#62;
&#160;&#160;&#160;&#160;&#160;&#160;&#124; ( &#60;s&#62; )
&#160;&#160;&#160;&#160;&#160;&#160;&#124; [ &#60;s&#62; ]

=end

def&#160;check(s); s =~
/^
&#160;&#160;(?&#60;p&#62;
&#160;&#160;&#160;&#160;[^&#92;(&#92;)&#92;[&#92;]]+&#160;&#160;&#160;&#160;# &#60;non_bracket_chars&#62;
&#160;&#160;&#124;
&#160;&#160;&#160;&#160;&#92;(&#160;&#92;g&#60;p&#62;*&#160;&#92;)&#160;&#160;&#160;&#160;# (&#160;&#60;p&#62;*&#160;)
&#160;&#160;&#124;
&#160;&#160;&#160;&#160;&#92;[&#160;&#92;g&#60;p&#62;*&#160;&#92;]&#160;&#160;&#160;&#160;# [&#160;&#60;p&#62;* ]
&#160;&#160;)*
$/x
end

]]></description>
			<content:encoded><![CDATA[<pre><code><font face="monospace">
<font color="#0000ff">=begin</font>
<font color="#0000ff">Bracket balance checking regex (2)</font>
<font color="#0000ff">==================================</font>

<font color="#0000ff">&lt;s&gt; ::= &lt;p&gt;*</font>
<font color="#0000ff">&lt;p&gt; ::= &lt;non_bracket_chars&gt;</font>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| ( &lt;s&gt; )</font>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| [ &lt;s&gt; ]</font>

<font color="#0000ff">=end</font>

<font color="#a020f0">def</font>&nbsp;<font color="#008080">check</font>(s); s =~
<font color="#6a5acd">/</font><font color="#6a5acd">^</font>
<font color="#ff00ff">&nbsp;&nbsp;</font><font color="#6a5acd">(?&lt;p&gt;</font>
<font color="#ff00ff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#6a5acd">[^</font><font color="#6a5acd">&#92;(&#92;)&#92;[&#92;]</font><font color="#6a5acd">]</font><font color="#6a5acd">+</font><font color="#ff00ff">&nbsp;&nbsp;&nbsp;&nbsp;# &lt;non_bracket_chars&gt;</font>
<font color="#ff00ff">&nbsp;&nbsp;</font><font color="#6a5acd">|</font>
<font color="#ff00ff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#6a5acd">&#92;(</font><font color="#ff00ff">&nbsp;</font><font color="#6a5acd">&#92;g&lt;p&gt;</font><font color="#6a5acd">*</font><font color="#ff00ff">&nbsp;</font><font color="#6a5acd">&#92;)</font><font color="#ff00ff">&nbsp;&nbsp;&nbsp;&nbsp;# </font><font color="#6a5acd">(</font><font color="#ff00ff">&nbsp;&lt;p&gt;</font><font color="#6a5acd">*</font><font color="#ff00ff">&nbsp;</font><font color="#6a5acd">)</font>
<font color="#ff00ff">&nbsp;&nbsp;</font><font color="#6a5acd">|</font>
<font color="#ff00ff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#6a5acd">&#92;[</font><font color="#ff00ff">&nbsp;</font><font color="#6a5acd">&#92;g&lt;p&gt;</font><font color="#6a5acd">*</font><font color="#ff00ff">&nbsp;</font><font color="#6a5acd">&#92;]</font><font color="#ff00ff">&nbsp;&nbsp;&nbsp;&nbsp;# </font><font color="#6a5acd">[</font><font color="#ff00ff">&nbsp;&lt;p&gt;* </font><font color="#6a5acd">]</font>
<font color="#ff00ff">&nbsp;&nbsp;</font><font color="#6a5acd">)</font><font color="#6a5acd">*</font>
<font color="#6a5acd">$</font><font color="#6a5acd">/x</font>
<font color="#a020f0">end</font>
</font></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=164</wfw:commentRss>
		</item>
		<item>
		<title>Brackets balance checking</title>
		<link>http://davidtran.doublegifts.com/blog/?p=163</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=163#comments</comments>
		<pubDate>Mon, 04 Oct 2010 18:22:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Haskell</category>

		<category>Ruby</category>

		<category>Java</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=163</guid>
		<description><![CDATA[
Brackets balance checking
=========================

Quiz ..&#46;..&#46; : write a method/function to check the blance of 
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(nested) brackets [] and () for a given string.
Example ..&#46; : &#34;..([..])..((..))..[[]]..&#34;&#160;&#160;==&#62; True
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#34;(()&#34;&#160;&#160;or &#34;..[..(..]..)..&#34;&#160;&#160;==&#62; False
Source ..&#46;. : http://yen3rc.blogspot.com/2010/09/haskell-practice-parenthesis-balance.html


My Java/Haskell/Ruby solutions:

Java
-&#45;-&#45;
&#160;&#160;public static boolean check(String s) {
&#160;&#160;&#160;&#160;assert(s != null);
&#160;&#160;&#160;&#160;final Character OPEN_PARENTHESE = &#39;(&#39;;
&#160;&#160;&#160;&#160;final Character OPEN_BRACKET = &#39;[&#39;;
&#160;&#160;&#160;&#160;Stack&#60;Character&#62; stack = new Stack&#60;Character&#62;();
&#160;&#160;&#160;&#160;for (char c : [...]]]></description>
			<content:encoded><![CDATA[<pre><code><font face="monospace">
Brackets balance checking
=========================

Quiz ..&#46;..&#46; : write a method/function to check the blance of 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(nested) brackets [] and () for a given string.
Example ..&#46; : &quot;..([..])..((..))..[[]]..&quot;&nbsp;&nbsp;==&gt; True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;(()&quot;&nbsp;&nbsp;or &quot;..[..(..]..)..&quot;&nbsp;&nbsp;==&gt; False
Source ..&#46;. : <a href="http://yen3rc.blogspot.com/2010/09/haskell-practice-parenthesis-balance.html">http://yen3rc.blogspot.com/2010/09/haskell-practice-parenthesis-balance.html</a>


My Java/Haskell/Ruby solutions:

Java
-&#45;-&#45;
&nbsp;&nbsp;public static boolean check(String s) {
&nbsp;&nbsp;&nbsp;&nbsp;assert(s != null);
&nbsp;&nbsp;&nbsp;&nbsp;final Character OPEN_PARENTHESE = &#39;(&#39;;
&nbsp;&nbsp;&nbsp;&nbsp;final Character OPEN_BRACKET = &#39;[&#39;;
&nbsp;&nbsp;&nbsp;&nbsp;Stack&lt;Character&gt; stack = new Stack&lt;Character&gt;();
&nbsp;&nbsp;&nbsp;&nbsp;for (char c : s.toCharArray()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (c == &#39;(&#39;) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack.push(OPEN_PARENTHESE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (c == &#39;[&#39;) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stack.push(OPEN_BRACKET);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (c == &#39;)&#39;) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stack.empty() || stack.pop() != OPEN_PARENTHESE) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if (c == &#39;]&#39;) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stack.empty() || stack.pop() != OPEN_BRACKET) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return stack.empty();
&nbsp;&nbsp;}

Haskell
-&#45;-&#45;-&#45;-
check :: String -&gt; Bool
check = check&#39; [] where
&nbsp;&nbsp;check&#39; []&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; []&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = True
&nbsp;&nbsp;check&#39; _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = False
&nbsp;&nbsp;check&#39; ss&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (&#39;(&#39;:xs) = check&#39; (&#39;(&#39;:ss) xs
&nbsp;&nbsp;check&#39; ss&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (&#39;[&#39;:xs) = check&#39; (&#39;[&#39;:ss) xs
&nbsp;&nbsp;check&#39; (&#39;(&#39;:ss) (&#39;)&#39;:xs) = check&#39; ss xs
&nbsp;&nbsp;check&#39; _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&#39;)&#39;:xs) = False
&nbsp;&nbsp;check&#39; (&#39;[&#39;:ss) (&#39;]&#39;:xs) = check&#39; ss xs
&nbsp;&nbsp;check&#39; _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&#39;]&#39;:xs) = False
&nbsp;&nbsp;check&#39; ss&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (_&nbsp;&nbsp;:xs) = check&#39; ss xs

Ruby (version 1.9)
-&#45;-&#45;-&#45;-&#45;-&#45;-&#45;-&#45;-&#45;-&#45;
YEN3 said that no matter what language, they solve this almost the same way.
I disagree with her; in fact, with the power of recursive regular expression,
the ruby version could solve it on one liner code and without any explicit stack.

def check(s)
&nbsp;&nbsp;s =~ /^(?&lt;p&gt;[^&#92;(&#92;)&#92;[&#92;]]*(&#92;(&#92;g&lt;p&gt;*&#92;)|&#92;[&#92;g&lt;p&gt;*&#92;])*)*$/
end

For more detail about recursive regular expression and 
detail analyze of this regex, have look at <a href="http://davidtran.doublegifts.com/blog/?p=162">http://davidtran.doublegifts.com/blog/?p=162</a>

</font></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=163</wfw:commentRss>
		</item>
		<item>
		<title>Recursive Regular Expressions</title>
		<link>http://davidtran.doublegifts.com/blog/?p=162</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=162#comments</comments>
		<pubDate>Mon, 04 Oct 2010 18:20:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Regex</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=162</guid>
		<description><![CDATA[
Recursive Regular Expressions
=============================

on Ruby 1.9
(?&#60;name&#62;..&#46;) ==&#62; define named group
&#92;g&#60;name&#62;&#160;&#160;&#160;&#160; ==&#62; subexp call by group name

Example #1
-&#45;-&#45;-&#45;-&#45;-&#45;
Regex matchs the set {a^nb^n &#124; n &#62;= 1} such as &#34;ab&#34;, &#34;aabb&#34;, &#34;aaabbb&#34;, ..&#46; etc.

/^(?&#60;c&#62;a&#92;g&#60;c&#62;*b)$/&#160;&#160; # define subexp group &#60;c&#62; and recursive reuse

Example #2
-&#45;-&#45;-&#45;-&#45;-&#45;
BNF of arithmetic expressions grammar:
&#60;E&#62; ::= &#60;T&#62; + &#60;E&#62;
&#60;E&#62; ::= &#60;T&#62; - &#60;E&#62;
&#60;E&#62; ::= &#60;T&#62;
&#60;T&#62; ::= [...]]]></description>
			<content:encoded><![CDATA[<pre><code><font face="monospace">
Recursive Regular Expressions
=============================

on Ruby 1.9
(?&lt;name&gt;..&#46;) ==&gt; define named group
&#92;g&lt;name&gt;&nbsp;&nbsp;&nbsp;&nbsp; ==&gt; subexp call by group name

Example #1
-&#45;-&#45;-&#45;-&#45;-&#45;
Regex matchs the set {a^nb^n | n &gt;= 1} such as &quot;ab&quot;, &quot;aabb&quot;, &quot;aaabbb&quot;, ..&#46; etc.

/^(?&lt;c&gt;a&#92;g&lt;c&gt;*b)$/&nbsp;&nbsp; # define subexp group &lt;c&gt; and recursive reuse

Example #2
-&#45;-&#45;-&#45;-&#45;-&#45;
BNF of arithmetic expressions grammar:
&lt;E&gt; ::= &lt;T&gt; + &lt;E&gt;
&lt;E&gt; ::= &lt;T&gt; - &lt;E&gt;
&lt;E&gt; ::= &lt;T&gt;
&lt;T&gt; ::= &lt;F&gt; * &lt;T&gt;
&lt;T&gt; ::= &lt;F&gt; / &lt;T&gt;
&lt;T&gt; ::= &lt;F&gt;
&lt;F&gt; ::= ( &lt;E&gt; )
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| number

Below regex matchs the expression &lt;E&gt;

%r{
^
&nbsp;&nbsp;(?&lt;E&gt;
&nbsp;&nbsp;&nbsp;&nbsp;(?&lt;T&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(?&lt;F&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#92;( &#92;g&lt;E&gt; &#92;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # &lt;F&gt; ::= ( &lt;E&gt; )
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([1-9]&#92;d* | 0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# &lt;F&gt; ::= number
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;( ( &#92;* | &#92;/ ) &#92;g&lt;T&gt; )?&nbsp;&nbsp;# &lt;T&gt; ::= &lt;F&gt;&nbsp;&nbsp;|&nbsp;&nbsp;&lt;F&gt; * &lt;T&gt;&nbsp;&nbsp;|&nbsp;&nbsp;&lt;F&gt; / &lt;T&gt;
&nbsp;&nbsp;&nbsp;&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;(&nbsp;&nbsp; ( &#92;+ | &#92;- ) &#92;g&lt;E&gt; )?&nbsp;&nbsp;# &lt;E&gt; ::= &lt;T&gt;&nbsp;&nbsp;|&nbsp;&nbsp;&lt;T&gt; + &lt;E&gt;&nbsp;&nbsp;|&nbsp;&nbsp;&lt;T&gt; - &lt;E&gt;
&nbsp;&nbsp;)
$
}x

Example #3
-&#45;-&#45;-&#45;-&#45;-&#45;
Checking the balance of (nested) brakets [] and (); such
&quot;..([..])..((..))..[[]]..&quot;&nbsp;&nbsp;==&gt; True
&quot;(()&quot;&nbsp;&nbsp;or &quot;..[..(..]..)..&quot;&nbsp;&nbsp;==&gt; False
Below regex will do the job.

/^
&nbsp;&nbsp;(?&lt;p&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# named group &lt;p&gt;
&nbsp;&nbsp;&nbsp;&nbsp;[^&#92;(&#92;)&#92;[&#92;]]*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 0 or more non-bracket chars

&nbsp;&nbsp;&nbsp;&nbsp;(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#92;(&nbsp;&nbsp;&#92;g&lt;p&gt;*&nbsp;&nbsp;&#92;)&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp;( &lt;p&gt;* )
&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# or
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#92;[&nbsp;&nbsp;&#92;g&lt;p&gt;*&nbsp;&nbsp;&#92;]&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp;[ &lt;p&gt;* ]
&nbsp;&nbsp;&nbsp;&nbsp;)*
&nbsp;&nbsp;)*
$/x
</font></code></pre>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=162</wfw:commentRss>
		</item>
		<item>
		<title>Negating Regular Expression</title>
		<link>http://davidtran.doublegifts.com/blog/?p=157</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=157#comments</comments>
		<pubDate>Sun, 03 Oct 2010 14:47:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Regex</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=157</guid>
		<description><![CDATA[
Negating Regular Expression

Examples:
* Like grep -v option

* Negate a regular expression so that a search finds everything that does NOT match the pattern.
/^((?! exp ).)*$/  or  /^(.(?&#60;! exp ))*$/

* Match string that does NOT content "hello" word.
/^((?!hello).)*$/  or  /^(.(?&#60;!hello))*$/

]]></description>
			<content:encoded><![CDATA[<pre>
Negating Regular Expression

Examples:
* Like grep -v option

* Negate a regular expression so that a search finds everything that does NOT match the pattern.
/^((?! exp ).)*$/  or  /^(.(?&lt;! exp ))*$/

* Match string that does NOT content "hello" word.
/^((?!hello).)*$/  or  /^(.(?&lt;!hello))*$/
</pre>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=157</wfw:commentRss>
		</item>
		<item>
		<title>Prime numbers</title>
		<link>http://davidtran.doublegifts.com/blog/?p=156</link>
		<comments>http://davidtran.doublegifts.com/blog/?p=156#comments</comments>
		<pubDate>Sun, 18 Jul 2010 02:07:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category>Haskell</category>

		<guid isPermaLink="false">http://davidtran.doublegifts.com/blog/?p=156</guid>
		<description><![CDATA[
{-
    Program : Prime.hs
    Author  : David Tran
    Date    : 2010-07-17

    Below references show many ways to construct prime numbers:
    * http://www.haskell.org/haskellwiki/Prime_numbers
    * Melissa E. O’Neill, The Genuine Sieve of Eratosthenes
   [...]]]></description>
			<content:encoded><![CDATA[<pre>
{-
    Program : Prime.hs
    Author  : David Tran
    Date    : 2010-07-17

    Below references show many ways to construct prime numbers:
    * http://www.haskell.org/haskellwiki/Prime_numbers
    * Melissa E. O’Neill, The Genuine Sieve of Eratosthenes
      http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
      http://lambda-the-ultimate.org/node/3127

    The classic one-liner is very elegant:
    primes = sieve [2..] where sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p > 0]
    However, it may be also the slowest&#8230;

    The reason that I wrote my own version is because, after read yen3&#8217;s blog
    (Chinese) http://yen3rc.blogspot.com/2010/03/haskell-practice-prime-2.html
    I like to try the idea of list 6n+1 and 6n+5.

-}

module Prime (isPrime, primes) where

primes :: [Integer]
primes = 2:3:5: filter isPrime&#39; ns
  where ns = foldr (&#92;n acc -> 6*n+1 : 6*n+5 : acc) [] [1..]
-&#45;      ns = concat $ zipWith (&#92;x y -> [x,y]) [7, 13..] [11, 17..]

isPrime&#39; :: Integer -> Bool
isPrime&#39; n = check primes
  where check (p:ps) | p < n'    = if (n `mod` p == 0) then False else check ps
                     | otherwise = True
        n' = truncate (sqrt $ fromIntegral n) + 1

isPrime :: Integer -> Bool
isPrime n | n < 2     = False
          | otherwise = isPrime' n

{-
isPrime :: Integer -> Bool
isPrime n = n `elem` takeWhile (<=n) primes
-}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://davidtran.doublegifts.com/blog/?feed=rss2&amp;p=156</wfw:commentRss>
		</item>
	</channel>
</rss>

