<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>jystic.com</title>
	
	<link>http://jystic.com</link>
	<description>Ceiling cat is watching you unsafePerformIO</description>
	<lastBuildDate>Thu, 12 Aug 2010 11:18:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Jystic" /><feedburner:info uri="jystic" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>IP addresses and MAC addresses in Haskell</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/yAtRF2tyIwk/</link>
		<comments>http://jystic.com/2010/08/12/ip-addresses-and-mac-addresses-in-haskell/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 11:18:20 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[networking]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=264</guid>
		<description><![CDATA[When opening a socket, if you don&#8217;t specify the address of the network interface you want to listen on, you&#8217;ll actually be listening on all of them. In a secure environment, this isn&#8217;t always what you want. That said, it&#8217;s quite useful to be able to show an administrator of your system a drop down [...]]]></description>
			<content:encoded><![CDATA[<p>When opening a socket, if you don&#8217;t specify the address of the network interface you want to listen on, you&#8217;ll actually be listening on all of them. In a secure environment, this isn&#8217;t always what you want. That said, it&#8217;s quite useful to be able to show an administrator of your system a drop down with the addresses of the various network interfaces available to the software.</p>
<p>When I set out to do this in Haskell I discovered that there wasn&#8217;t anything available that would let me get the address information for my network interfaces. So, I set out build <a href="http://hackage.haskell.org/package/network-info">network-info</a> and learned how to use the Foreign Function Interface (FFI) in the process.</p>
<p>Getting the IPv4, IPv6 and MAC addresses of your local network interfaces is now just a &#8220;cabal install&#8221; away:</p>
<pre>
cabal install network-info
</pre>
<p>Once you&#8217;ve got the library installed it&#8217;s simple to get your network details:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #000; font-weight: bold;">import</span> <span style="color: #555;">Network.Info</span>
&nbsp;
<span style="color: #900; font-weight: bold;">main</span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #000; font-weight: bold;">do</span>
    ns <span style="color: #000; font-weight: bold;">&lt;-</span> getNetworkInterfaces
    mapM_ <span style="color: #283352;"><span class="br0">&#40;</span></span>putStr <span style="color: #000; font-weight: bold;">.</span> showInterface<span style="color: #283352;"><span class="br0">&#41;</span></span> ns
&nbsp;
<span style="color: #900; font-weight: bold;">showInterface</span> n <span style="color: #000; font-weight: bold;">=</span> name n <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;<span style="font-weight: bold;">\n</span>&quot;</span>
               <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;  IPv4: &quot;</span> <span style="color: #000; font-weight: bold;">++</span> show <span style="color: #283352;"><span class="br0">&#40;</span></span>ipv4 n<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;<span style="font-weight: bold;">\n</span>&quot;</span>
               <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;  IPv6: &quot;</span> <span style="color: #000; font-weight: bold;">++</span> show <span style="color: #283352;"><span class="br0">&#40;</span></span>ipv6 n<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;<span style="font-weight: bold;">\n</span>&quot;</span>
               <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;  MAC:  &quot;</span> <span style="color: #000; font-weight: bold;">++</span> show <span style="color: #283352;"><span class="br0">&#40;</span></span>mac n<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot;<span style="font-weight: bold;">\n</span>&quot;</span></pre></div></div>

<p>Running this on my Ubuntu box yields something similar to this:</p>
<pre>
$ runghc getaddr.hs
lo
  IPv4: 127.0.0.1
  IPv6: 0:0:0:0:0:0:0:1
  MAC:  00:00:00:00:00:00
eth0
  IPv4: 10.0.2.5
  IPv6: fe80:0:0:0:f00:15ff:fcf9:c677
  MAC:  08:00:72:5f:6c:19
</pre>
<p>And on Windows 7:</p>
<pre>
> runghc getaddr.hs
Local Area Connection
  IPv4: 192.168.0.107
  IPv6: fe80:0:0:0:ef54:7861:7df:6b7c
  MAC:  00:26:c9:e1:87:8c
VirtualBox Host-Only Network
  IPv4: 192.168.56.1
  IPv6: fe80:0:0:0:14b4:b3af:557:ad4a
  MAC:  08:00:27:00:d8:2a
Loopback Pseudo-Interface 1
  IPv4: 127.0.0.1
  IPv6: 0:0:0:0:0:0:0:1
  MAC:  00:00:00:00:00:00
... snipped out some garbage network interfaces
</pre>
<p>You&#8217;ll notice that the IPv6 addresses don&#8217;t have fully collapsed groups of zeros just yet, but they are still valid addresses.</p>
<p>I&#8217;ve tested this on Ubuntu 10.04, Windows XP &#038; Windows 7. It won&#8217;t install on Mac OS X or BSD just yet because of restrictions in the cabal file. I need to check out how <a href="http://www.kernel.org/doc/man-pages/online/pages/man3/getifaddrs.3.html">getifaddrs</a> works on those systems so I know what to do in the FFI code.</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/yAtRF2tyIwk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2010/08/12/ip-addresses-and-mac-addresses-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2010/08/12/ip-addresses-and-mac-addresses-in-haskell/</feedburner:origLink></item>
		<item>
		<title>‘Edit with Vim’ on x64</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/mSFXmYvG4lM/</link>
		<comments>http://jystic.com/2010/07/28/edit-with-vim-x64/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 07:01:02 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=256</guid>
		<description><![CDATA[I&#8217;ve been using the 64-bit version of Vim so that I could get the &#8216;Edit with Vim&#8217; context menu in Windows Explorer. This was all fine and good except that I wanted to use something that requires vim to be compiled with +ruby. The vanilla x86 gVim72 installer you can get at vim.org is compiled [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using the <a href="http://code.google.com/p/vim-win3264/">64-bit version of Vim</a> so that I could get the &#8216;Edit with Vim&#8217; context menu in Windows Explorer. This was all fine and good except that I wanted to use something that requires vim to be compiled with +ruby. The vanilla x86 gVim72 installer you can get at <a href="http://www.vim.org/download.php">vim.org</a> is compiled with +ruby, but it doesn&#8217;t have the explorer context menu on a 64-bit computer :(</p>
<p>The solution was to install the x86 vim from vim.org, then take the gvimext.dll from the 64-bit vim, and apply the following registry tweaks.</p>
<pre>
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Vim\Gvim]
"path"="C:\\Program Files (x86)\\Vim\\vim72\\gvim.exe"

[HKEY_CLASSES_ROOT\CLSID\{51EEE242-AD87-11d3-9C1E-0090278BBD99}]
@="Vim Shell Extension"

[HKEY_CLASSES_ROOT\CLSID\{51EEE242-AD87-11d3-9C1E-0090278BBD99}\InProcServer32]
@="C:\\Program Files (x86)\\Vim\\vim72\\gvimext64.dll"
"ThreadingModel"="Apartment"
</pre>
<p>To save anyone else the hassle of getting the bits you need from the 64-bit vim, I&#8217;ve zipped up everything you need (<a href="http://jystic.com/wp-content/uploads/2010/07/gvimext64.zip">gvimext64.zip</a>). Just extract it to C:\Program Files (x86)\Vim\vim72 and run the gvimext64.reg file.</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/mSFXmYvG4lM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2010/07/28/edit-with-vim-x64/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2010/07/28/edit-with-vim-x64/</feedburner:origLink></item>
		<item>
		<title>GitHub Haskell colours for GeSHi</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/9wfmw-JgPH4/</link>
		<comments>http://jystic.com/2010/05/21/github-haskell-colours-for-geshi/#comments</comments>
		<pubDate>Fri, 21 May 2010 14:29:13 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[geshi]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[syntax highlighting]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=212</guid>
		<description><![CDATA[GeSHi is the php-based syntax highlighter that I use for this blog. The Haskell highlighting included with GeSHi is a bit below par compared to say GitHub, which uses pygments for highlighting.
Not to be outdone, I though I&#8217;d delve in to GeSHi&#8217;s regular expression features to see if I could improve upon the default. I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://qbnz.com/highlighter/">GeSHi</a> is the php-based syntax highlighter that I use for this blog. The Haskell highlighting included with GeSHi is a bit below par compared to say <a href="http://github.com/jystic/lambdasim/blob/master/Main.hs">GitHub</a>, which uses <a href="http://pygments.org/">pygments</a> for highlighting.</p>
<p>Not to be outdone, I though I&#8217;d delve in to GeSHi&#8217;s regular expression features to see if I could improve upon the default. I struggled a bit with things getting highlighted that had already been processed and matched to an alternate regular expression until I dug in to the GeSHi source to and found out how it escapes potential highlights. This led to some lovely regex like the one below as I attempted to work around things like &lt;PIPE&gt;, &lt;SEMI&gt; and other magical GeSHi escapes like &lt;|!REG3XP1!&gt;.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#91;</span>A<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #339933;">+&gt;|&lt;</span>\<span style="color: #339933;">|!</span>REG3XP<span style="color: #0000ff;">\d</span><span style="color: #339933;">*!&gt;.*?</span>\<span style="color: #339933;">|&gt;/</span></pre></div></div>

<p>One I figured this out things got a lot easier and I was able to get to a point that I&#8217;m pretty happy with. The symbol highlighting is still a bit hacky and I&#8217;d like to get to a point where all non-alphanumeric character groups are highlighted as symbols. For now though I&#8217;ve just added a few more (&lt;$&gt;, &lt;|&gt;, etc) to the hardcoded list that comes with GeSHi out of the box. So without any further ado, here are the results.</p>
<h3>Before</h3>

<div class="wp_syntax"><div class="code"><pre class="haskell-old" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span><span style="color: #339933; font-weight: bold;">.</span>Identity
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Parsec <span style="color: green;">&#40;</span>ParseError<span style="color: #339933; font-weight: bold;">,</span>parse<span style="color: #339933; font-weight: bold;">,</span>char<span style="color: #339933; font-weight: bold;">,</span>oneOf<span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;?&gt;</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;|&gt;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Parsec<span style="color: #339933; font-weight: bold;">.</span>ByteString
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Parsec<span style="color: #339933; font-weight: bold;">.</span>Expr
<span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">qualified</span> Data<span style="color: #339933; font-weight: bold;">.</span>ByteString<span style="color: #339933; font-weight: bold;">.</span>Char8 <span style="color: #06c; font-weight: bold;">as</span> S
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Expr <span style="color: #339933; font-weight: bold;">=</span> Op <span style="color: #cccc00; font-weight: bold;">Char</span> Expr Expr
          <span style="color: #339933; font-weight: bold;">|</span> Value <span style="color: #cccc00; font-weight: bold;">Char</span>
          <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: #cccc00; font-weight: bold;">Show</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- This is a comment</span>
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
  xs <span style="color: #339933; font-weight: bold;">&lt;-</span> S<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">getContents</span>
  <span style="color: #06c; font-weight: bold;">let</span> f <span style="color: #339933; font-weight: bold;">=</span> S<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">unlines</span> <span style="color: #339933; font-weight: bold;">.</span> translate <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">drop</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> S<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">lines</span>
  S<span style="color: #339933; font-weight: bold;">.</span><span style="font-weight: bold;">putStr</span> <span style="color: #339933; font-weight: bold;">$</span> f xs
  <span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">{- This is a multiline
    comment -}</span>
translate <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>S<span style="color: #339933; font-weight: bold;">.</span>ByteString<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>S<span style="color: #339933; font-weight: bold;">.</span>ByteString<span style="color: green;">&#93;</span>
translate <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="color: #339933; font-weight: bold;">$</span> f <span style="color: #339933; font-weight: bold;">.</span> readExpr
  <span style="color: #06c; font-weight: bold;">where</span> f <span style="color: green;">&#40;</span>Left err<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> S<span style="color: #339933; font-weight: bold;">.</span>pack <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">show</span> err
        f <span style="color: green;">&#40;</span>Right val<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> showRPN val
&nbsp;
showRPN <span style="color: #339933; font-weight: bold;">::</span> Expr <span style="color: #339933; font-weight: bold;">-&gt;</span> S<span style="color: #339933; font-weight: bold;">.</span>ByteString
showRPN <span style="color: green;">&#40;</span>Value v<span style="color: green;">&#41;</span>  <span style="color: #339933; font-weight: bold;">=</span> S<span style="color: #339933; font-weight: bold;">.</span>singleton v
showRPN <span style="color: green;">&#40;</span>Op o a b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> showRPN a `S<span style="color: #339933; font-weight: bold;">.</span>append` showRPN b `S<span style="color: #339933; font-weight: bold;">.</span>append` S<span style="color: #339933; font-weight: bold;">.</span>singleton o
&nbsp;
table <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>Operator S<span style="color: #339933; font-weight: bold;">.</span>ByteString st Identity Expr<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
table <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>op '<span style="color: #339933; font-weight: bold;">^</span>'<span style="color: #339933; font-weight: bold;">,</span> op '<span style="color: #339933; font-weight: bold;">/</span>'<span style="color: #339933; font-weight: bold;">,</span> op '<span style="color: #339933; font-weight: bold;">*</span>'<span style="color: #339933; font-weight: bold;">,</span> op '<span style="color: #339933; font-weight: bold;">-</span>'<span style="color: #339933; font-weight: bold;">,</span> op '<span style="color: #339933; font-weight: bold;">+</span>'<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
  <span style="color: #06c; font-weight: bold;">where</span> op c <span style="color: #339933; font-weight: bold;">=</span> Infix <span style="color: green;">&#40;</span>liftM Op <span style="color: #339933; font-weight: bold;">$</span> char c<span style="color: green;">&#41;</span> AssocLeft
&nbsp;
term <span style="color: #339933; font-weight: bold;">::</span> Parser Expr
term <span style="color: #339933; font-weight: bold;">=</span> brackets <span style="color: #339933; font-weight: bold;">&lt;|&gt;</span> value <span style="color: #339933; font-weight: bold;">&lt;?&gt;</span> <span style="background-color: #3cb371;">&quot;term&quot;</span>
&nbsp;
brackets <span style="color: #339933; font-weight: bold;">::</span> Parser Expr
brackets <span style="color: #339933; font-weight: bold;">=</span> char '<span style="color: green;">&#40;</span>' <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> expr <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> \x <span style="color: #339933; font-weight: bold;">-&gt;</span> char '<span style="color: green;">&#41;</span>' <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">return</span> x</pre></div></div>

<h3>After</h3>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #000; font-weight: bold;">module</span> <span style="color: #555;">Main <span style="color: #000; font-weight: bold;">where</span></span>
&nbsp;
<span style="color: #000; font-weight: bold;">import</span> <span style="color: #555;">Control.Monad.Identity</span>
<span style="color: #000; font-weight: bold;">import</span> <span style="color: #555;">Text.Parsec <span style="color: #283352;"><span class="br0">&#40;</span></span>ParseError<span style="color: #283352;">,</span>parse<span style="color: #283352;">,</span>char<span style="color: #283352;">,</span>oneOf<span style="color: #283352;">,<span class="br0">&#40;</span></span><span style="color: #000; font-weight: bold;">&lt;?&gt;</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #000; font-weight: bold;">&lt;|&gt;</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#41;</span></span></span>
<span style="color: #000; font-weight: bold;">import</span> <span style="color: #555;">Text.Parsec.ByteString</span>
<span style="color: #000; font-weight: bold;">import</span> <span style="color: #555;">Text.Parsec.Expr</span>
<span style="color: #000; font-weight: bold;">import</span> <span style="color: #000; font-weight: bold;">qualified</span> <span style="color: #555;">Data.ByteString.Char8 <span style="color: #000; font-weight: bold;">as</span> <span style="color: #458; font-weight: bold;">S</span></span>
&nbsp;
<span style="color: #000; font-weight: bold;">data</span> <span style="color: #458; font-weight: bold;">Expr</span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #458; font-weight: bold;">Op</span> <span style="color: #458; font-weight: bold;">Char</span> <span style="color: #458; font-weight: bold;">Expr</span> <span style="color: #458; font-weight: bold;">Expr</span>
          <span style="color: #000; font-weight: bold;">|</span> <span style="color: #458; font-weight: bold;">Value</span> <span style="color: #458; font-weight: bold;">Char</span>
          <span style="color: #000; font-weight: bold;">deriving</span> <span style="color: #458; font-weight: bold;">Show</span>
&nbsp;
<span style="color: #998; font-style: italic;">-- This is a comment</span>
<span style="color: #900; font-weight: bold;">main</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #458; font-weight: bold;">IO</span> <span style="color: #283352;"><span class="br0">&#40;</span><span class="br0">&#41;</span></span>
<span style="color: #900; font-weight: bold;">main</span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #000; font-weight: bold;">do</span>
  xs <span style="color: #000; font-weight: bold;">&lt;-</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>getContents
  <span style="color: #000; font-weight: bold;">let</span> f <span style="color: #000; font-weight: bold;">=</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>unlines <span style="color: #000; font-weight: bold;">.</span> translate <span style="color: #000; font-weight: bold;">.</span> <span style="color: #283352;"><span class="br0">&#40;</span></span>drop <span style="color: #099;">1</span><span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">.</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>lines
  <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>putStr <span style="color: #000; font-weight: bold;">$</span> f xs
  return <span style="color: #283352;"><span class="br0">&#40;</span><span class="br0">&#41;</span></span>
&nbsp;
<span style="color: #998; font-style: italic;">{- This is a multiline
    comment -}</span>
<span style="color: #900; font-weight: bold;">translate</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #283352;"><span class="br0">&#91;</span></span><span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span><span style="color: #458; font-weight: bold;">ByteString</span><span style="color: #283352;"><span class="br0">&#93;</span></span> <span style="color: #000; font-weight: bold;">-&gt;</span> <span style="color: #283352;"><span class="br0">&#91;</span></span><span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span><span style="color: #458; font-weight: bold;">ByteString</span><span style="color: #283352;"><span class="br0">&#93;</span></span>
<span style="color: #900; font-weight: bold;">translate</span> <span style="color: #000; font-weight: bold;">=</span> map <span style="color: #000; font-weight: bold;">$</span> f <span style="color: #000; font-weight: bold;">.</span> readExpr
  <span style="color: #000; font-weight: bold;">where</span> f <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Left</span> err<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>pack <span style="color: #000; font-weight: bold;">$</span> show err
        f <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Right</span> val<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">=</span> showRPN val
&nbsp;
<span style="color: #900; font-weight: bold;">showRPN</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #458; font-weight: bold;">Expr</span> <span style="color: #000; font-weight: bold;">-&gt;</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span><span style="color: #458; font-weight: bold;">ByteString</span>
<span style="color: #900; font-weight: bold;">showRPN</span> <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Value</span> v<span style="color: #283352;"><span class="br0">&#41;</span></span>  <span style="color: #000; font-weight: bold;">=</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>singleton v
<span style="color: #900; font-weight: bold;">showRPN</span> <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Op</span> o a b<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">=</span> showRPN a <span style="color: #000; font-weight: bold;">`</span><span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>append<span style="color: #000; font-weight: bold;">`</span> showRPN b <span style="color: #000; font-weight: bold;">`</span><span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>append<span style="color: #000; font-weight: bold;">`</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span>singleton o
&nbsp;
<span style="color: #900; font-weight: bold;">table</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#91;</span></span><span style="color: #458; font-weight: bold;">Operator</span> <span style="color: #458; font-weight: bold;">S</span><span style="color: #000; font-weight: bold;">.</span><span style="color: #458; font-weight: bold;">ByteString</span> st <span style="color: #458; font-weight: bold;">Identity</span> <span style="color: #458; font-weight: bold;">Expr</span><span style="color: #283352;"><span class="br0">&#93;</span><span class="br0">&#93;</span></span>
<span style="color: #900; font-weight: bold;">table</span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#91;</span></span>op <span style="color: #d14;">'^'</span><span style="color: #283352;">,</span> op <span style="color: #d14;">'/'</span><span style="color: #283352;">,</span> op <span style="color: #d14;">'*'</span><span style="color: #283352;">,</span> op <span style="color: #d14;">'-'</span><span style="color: #283352;">,</span> op <span style="color: #d14;">'+'</span><span style="color: #283352;"><span class="br0">&#93;</span><span class="br0">&#93;</span></span>
  <span style="color: #000; font-weight: bold;">where</span> op c <span style="color: #000; font-weight: bold;">=</span> <span style="color: #458; font-weight: bold;">Infix</span> <span style="color: #283352;"><span class="br0">&#40;</span></span>liftM <span style="color: #458; font-weight: bold;">Op</span> <span style="color: #000; font-weight: bold;">$</span> char c<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #458; font-weight: bold;">AssocLeft</span>
&nbsp;
<span style="color: #900; font-weight: bold;">term</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #458; font-weight: bold;">Parser</span> <span style="color: #458; font-weight: bold;">Expr</span>
<span style="color: #900; font-weight: bold;">term</span> <span style="color: #000; font-weight: bold;">=</span> brackets <span style="color: #000; font-weight: bold;">&lt;|</span><span style="color: #000; font-weight: bold;">&gt;</span> value <span style="color: #000; font-weight: bold;">&lt;?&gt;</span> <span style="color: #d14;">&quot;term&quot;</span>
&nbsp;
<span style="color: #900; font-weight: bold;">brackets</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #458; font-weight: bold;">Parser</span> <span style="color: #458; font-weight: bold;">Expr</span>
<span style="color: #900; font-weight: bold;">brackets</span> <span style="color: #000; font-weight: bold;">=</span> char <span style="color: #d14;">'<span class="br0">&#40;</span>'</span> <span style="color: #000; font-weight: bold;">&gt;&gt;</span> expr <span style="color: #000; font-weight: bold;">&gt;&gt;=</span> <span style="color: #900; font-weight: bold;">\</span>x <span style="color: #000; font-weight: bold;">-&gt;</span> char <span style="color: #d14;">'<span class="br0">&#41;</span>'</span> <span style="color: #000; font-weight: bold;">&gt;&gt;</span> return x</pre></div></div>

<p>You can download my modifications to the Haskell syntax file for GeSHi <a href='/wp-content/uploads/2010/05/haskell.php_.txt'>here</a>. If I get some more time I&#8217;ll probably submit it back to the GeSHi project as a replacement or an alternative for Haskell.</p>
<p>One thing to note is that I&#8217;ve stripped out the url linking of Prelude functions. I felt that it wasn&#8217;t really that useful as I rarely want to know the details of those functions, it&#8217;s all the other ones that I want to know about! I toyed with the idea of linking functions and types to <a href="http://www.haskell.org/hoogle/">Hoogle</a> or <a href="http://holumbus.fh-wedel.de/hayoo/hayoo.html">Hayoo!</a>, that might be worthwhile in the future.</p>
<p><strong>Download <a href='/wp-content/uploads/2010/05/haskell.php_.txt'>haskell.php</a></strong></p>
<p><em>The syntax file above was built and tested on GeSHi 1.0.8.7, it will probably work on other versions, but you never know :)</em></p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/9wfmw-JgPH4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2010/05/21/github-haskell-colours-for-geshi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2010/05/21/github-haskell-colours-for-geshi/</feedburner:origLink></item>
		<item>
		<title>The Skyline Problem</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/GKqP-7Oj264/</link>
		<comments>http://jystic.com/2010/05/16/the-skyline-problem/#comments</comments>
		<pubDate>Sat, 15 May 2010 18:16:31 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code challenge]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=177</guid>
		<description><![CDATA[One of my friends recently had a job interview where he was asked to complete The Skyline Problem. It sounded interesting so I thought I&#8217;d see if my Haskell skills were up to the task.

My first attempt took me a few hours to come up with. It&#8217;s been a little while since I&#8217;ve played with [...]]]></description>
			<content:encoded><![CDATA[<p>One of my friends recently had a job interview where he was asked to complete <a href="http://online-judge.uva.es/p/v1/105.html">The Skyline Problem</a>. It sounded interesting so I thought I&#8217;d see if my Haskell skills were up to the task.</p>
<p><img src="http://jystic.com/wp-content/uploads/2010/05/skyline-problem.gif" alt="The Skyline Problem" width="764" height="247" class="aligncenter size-full wp-image-200" /></p>
<p>My first attempt took me a few hours to come up with. It&#8217;s been a little while since I&#8217;ve played with Haskell so a lot of time was spent fighting with the type checker. Eventually though I produced a solution that solved the problem. It works by exploding each building in to a set of discrete points, where a building (1,11,3) becomes the coordinates [(1,11),(2,11),(3,11)]. The points are then sorted and various filters are run across the data until we get the answer.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #000; font-weight: bold;">import</span> <span style="color: #555;">Data.List</span>
&nbsp;
<span style="color: #900; font-weight: bold;">main</span> <span style="color: #000; font-weight: bold;">=</span>
  putStrLn <span style="color: #000; font-weight: bold;">$</span>
  unwords <span style="color: #000; font-weight: bold;">$</span> 
  concatMap <span style="color: #283352;"><span class="br0">&#40;</span></span>map show <span style="color: #000; font-weight: bold;">.</span> list <span style="color: #000; font-weight: bold;">.</span> head<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">$</span>
  groupBy' snd <span style="color: #000; font-weight: bold;">$</span>
  map last <span style="color: #000; font-weight: bold;">$</span>
  groupBy' fst <span style="color: #000; font-weight: bold;">$</span>
  sort <span style="color: #000; font-weight: bold;">$</span>
  concatMap explode buildings
&nbsp;
<span style="color: #900; font-weight: bold;">list</span> <span style="color: #283352;"><span class="br0">&#40;</span></span>x<span style="color: #283352;">,</span>y<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #283352;"><span class="br0">&#91;</span></span>x<span style="color: #283352;">,</span>y<span style="color: #283352;"><span class="br0">&#93;</span></span>
<span style="color: #900; font-weight: bold;">groupBy'</span> f <span style="color: #000; font-weight: bold;">=</span> groupBy <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #900; font-weight: bold;">\</span>x y <span style="color: #000; font-weight: bold;">-&gt;</span> f x <span style="color: #000; font-weight: bold;">==</span> f y<span style="color: #283352;"><span class="br0">&#41;</span></span>
<span style="color: #900; font-weight: bold;">explode</span> <span style="color: #283352;"><span class="br0">&#40;</span></span>l<span style="color: #283352;">,</span>h<span style="color: #283352;">,</span>r<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span>x<span style="color: #283352;">,</span>h<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">|</span> x <span style="color: #000; font-weight: bold;">&lt;-</span> <span style="color: #283352;"><span class="br0">&#91;</span></span>l<span style="color: #000; font-weight: bold;">..</span>r<span style="color: #000; font-weight: bold;">-</span><span style="color: #099;">1</span><span style="color: #283352;"><span class="br0">&#93;</span><span class="br0">&#93;</span></span> <span style="color: #000; font-weight: bold;">++</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span>r<span style="color: #283352;">,</span><span style="color: #099;">0</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#93;</span></span>
&nbsp;
<span style="color: #900; font-weight: bold;">buildings</span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span><span style="color: #099;">1</span><span style="color: #283352;">,</span><span style="color: #099;">11</span><span style="color: #283352;">,</span><span style="color: #099;">5</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #099;">2</span><span style="color: #283352;">,</span><span style="color: #099;">6</span><span style="color: #283352;">,</span><span style="color: #099;">7</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #099;">3</span><span style="color: #283352;">,</span><span style="color: #099;">13</span><span style="color: #283352;">,</span><span style="color: #099;">9</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #099;">12</span><span style="color: #283352;">,</span><span style="color: #099;">7</span><span style="color: #283352;">,</span><span style="color: #099;">16</span><span style="color: #283352;"><span class="br0">&#41;</span>,</span>
             <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #099;">14</span><span style="color: #283352;">,</span><span style="color: #099;">3</span><span style="color: #283352;">,</span><span style="color: #099;">25</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #099;">19</span><span style="color: #283352;">,</span><span style="color: #099;">18</span><span style="color: #283352;">,</span><span style="color: #099;">22</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #099;">23</span><span style="color: #283352;">,</span><span style="color: #099;">13</span><span style="color: #283352;">,</span><span style="color: #099;">29</span><span style="color: #283352;"><span class="br0">&#41;</span>,<span class="br0">&#40;</span></span><span style="color: #099;">24</span><span style="color: #283352;">,</span><span style="color: #099;">4</span><span style="color: #283352;">,</span><span style="color: #099;">28</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#93;</span></span></pre></div></div>

<p>This seemed like it wasn&#8217;t the most efficient way to do things and I wasn&#8217;t reading my data from an input file yet. So, after a bit more thinking (and some <a href="http://stackoverflow.com/questions/1066234/the-skyline-problem">cheating</a>) I got some ideas on how to implement the program coming from a different angle. At some point I also wanted to try a golf solution (using as few characters as possible) so I stuck to using only the Prelude.</p>
<p>The revised solution works by scanning each building for every x-coordinate (1 &#8211; 9999) and calculating the height at that coordinate. The output is built by showing the current coordinates every time the maximum height changes. The thing I like about the revised solution is that it is basically just restating the problem in Haskell. I&#8217;ve included the type signatures below for completeness, but as always, they&#8217;re not required.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #900; font-weight: bold;">main</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #458; font-weight: bold;">IO</span> <span style="color: #283352;"><span class="br0">&#40;</span><span class="br0">&#41;</span></span>
<span style="color: #900; font-weight: bold;">main</span> <span style="color: #000; font-weight: bold;">=</span> interact <span style="color: #000; font-weight: bold;">$</span> showSkyline <span style="color: #000; font-weight: bold;">.</span> skyline <span style="color: #000; font-weight: bold;">.</span> readBuildings
&nbsp;
<span style="color: #900; font-weight: bold;">readBuildings</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #458; font-weight: bold;">String</span> <span style="color: #000; font-weight: bold;">-&gt;</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#91;</span></span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;"><span class="br0">&#93;</span><span class="br0">&#93;</span></span>
<span style="color: #900; font-weight: bold;">readBuildings</span> <span style="color: #000; font-weight: bold;">=</span> map <span style="color: #283352;"><span class="br0">&#40;</span></span>map read <span style="color: #000; font-weight: bold;">.</span> words<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">.</span> lines
&nbsp;
<span style="color: #900; font-weight: bold;">skyline</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#91;</span></span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;"><span class="br0">&#93;</span><span class="br0">&#93;</span></span> <span style="color: #000; font-weight: bold;">-&gt;</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;">,</span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#93;</span></span>
<span style="color: #900; font-weight: bold;">skyline</span> buildings <span style="color: #000; font-weight: bold;">=</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span>x<span style="color: #283352;">,</span> height x<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">|</span> x <span style="color: #000; font-weight: bold;">&lt;-</span> <span style="color: #283352;"><span class="br0">&#91;</span></span><span style="color: #099;">1</span><span style="color: #000; font-weight: bold;">..</span><span style="color: #099;">9999</span><span style="color: #283352;"><span class="br0">&#93;</span>,</span> isDiff x<span style="color: #283352;"><span class="br0">&#93;</span></span>
  <span style="color: #000; font-weight: bold;">where</span> isDiff  x <span style="color: #000; font-weight: bold;">=</span> height x <span style="color: #000; font-weight: bold;">/=</span> height <span style="color: #283352;"><span class="br0">&#40;</span></span>x <span style="color: #000; font-weight: bold;">-</span> <span style="color: #099;">1</span><span style="color: #283352;"><span class="br0">&#41;</span></span>
        height    <span style="color: #000; font-weight: bold;">=</span> maximum <span style="color: #000; font-weight: bold;">.</span> heights
        heights x <span style="color: #000; font-weight: bold;">=</span> <span style="color: #099;">0</span> <span style="color: #000; font-weight: bold;">:</span> <span style="color: #283352;"><span class="br0">&#91;</span></span>h <span style="color: #000; font-weight: bold;">|</span> <span style="color: #283352;"><span class="br0">&#91;</span></span>s<span style="color: #283352;">,</span>h<span style="color: #283352;">,</span>e<span style="color: #283352;"><span class="br0">&#93;</span></span> <span style="color: #000; font-weight: bold;">&lt;-</span> buildings<span style="color: #283352;">,</span> s <span style="color: #000; font-weight: bold;">&lt;=</span> x<span style="color: #283352;">,</span> x <span style="color: #000; font-weight: bold;">&lt;</span> e<span style="color: #283352;"><span class="br0">&#93;</span></span>
&nbsp;
<span style="color: #900; font-weight: bold;">showSkyline</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;">,</span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#93;</span></span> <span style="color: #000; font-weight: bold;">-&gt;</span> <span style="color: #458; font-weight: bold;">String</span>
<span style="color: #900; font-weight: bold;">showSkyline</span> <span style="color: #000; font-weight: bold;">=</span> unwords <span style="color: #000; font-weight: bold;">.</span> <span style="color: #283352;"><span class="br0">&#40;</span></span>map showPoint<span style="color: #283352;"><span class="br0">&#41;</span></span>
&nbsp;
<span style="color: #900; font-weight: bold;">showPoint</span> <span style="color: #000; font-weight: bold;">::</span> <span style="color: #283352;"><span class="br0">&#40;</span></span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;">,</span><span style="color: #458; font-weight: bold;">Int</span><span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">-&gt;</span> <span style="color: #458; font-weight: bold;">String</span>
<span style="color: #900; font-weight: bold;">showPoint</span> <span style="color: #283352;"><span class="br0">&#40;</span></span>x<span style="color: #283352;">,</span> y<span style="color: #283352;"><span class="br0">&#41;</span></span> <span style="color: #000; font-weight: bold;">=</span> show x <span style="color: #000; font-weight: bold;">++</span> <span style="color: #d14;">&quot; &quot;</span> <span style="color: #000; font-weight: bold;">++</span> show y</pre></div></div>

<p>Finally, I reduced the above solution to as few characters as possible so I could write an answer for the <a href="http://stackoverflow.com/questions/1066234/the-skyline-problem">Stack Overflow</a> code golf question.</p>
<p>It turned out to be 149 characters, which I was pretty happy with. I could have shaved off four more by inlining &#8216;b&#8217;, but that made the program take a lot longer to execute when using runghc for some reason. I&#8217;m not sure if it would have made a difference if I compiled it.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #900; font-weight: bold;">main</span><span style="color: #000; font-weight: bold;">=</span>interact f
<span style="color: #900; font-weight: bold;">f</span> i<span style="color: #000; font-weight: bold;">=</span>unwords<span style="color: #283352;"><span class="br0">&#91;</span></span>show x<span style="color: #000; font-weight: bold;">++</span><span style="color: #d14;">&quot; &quot;</span><span style="color: #000; font-weight: bold;">++</span>show<span style="color: #283352;"><span class="br0">&#40;</span></span>h x<span style="color: #283352;"><span class="br0">&#41;</span></span><span style="color: #000; font-weight: bold;">|</span>x<span style="color: #000; font-weight: bold;">&lt;-</span><span style="color: #283352;"><span class="br0">&#91;</span></span><span style="color: #099;">1</span><span style="color: #000; font-weight: bold;">..</span><span style="color: #099;">9999</span><span style="color: #283352;"><span class="br0">&#93;</span>,</span>h x<span style="color: #000; font-weight: bold;">/=</span>h<span style="color: #283352;"><span class="br0">&#40;</span></span>x<span style="color: #000; font-weight: bold;">-</span><span style="color: #099;">1</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#93;</span></span> <span style="color: #000; font-weight: bold;">where</span>
 h x<span style="color: #000; font-weight: bold;">=</span>maximum<span style="color: #000; font-weight: bold;">$</span><span style="color: #099;">0</span><span style="color: #000; font-weight: bold;">:</span><span style="color: #283352;"><span class="br0">&#91;</span></span>y<span style="color: #000; font-weight: bold;">|</span><span style="color: #283352;"><span class="br0">&#91;</span></span>l<span style="color: #283352;">,</span>y<span style="color: #283352;">,</span>r<span style="color: #283352;"><span class="br0">&#93;</span></span><span style="color: #000; font-weight: bold;">&lt;-</span>b<span style="color: #283352;">,</span>l<span style="color: #000; font-weight: bold;">&lt;=</span>x<span style="color: #283352;">,</span>x<span style="color: #000; font-weight: bold;">&lt;</span>r<span style="color: #283352;"><span class="br0">&#93;</span></span>
 b<span style="color: #000; font-weight: bold;">=</span>map<span style="color: #283352;"><span class="br0">&#40;</span></span>map read<span style="color: #000; font-weight: bold;">.</span>words<span style="color: #283352;"><span class="br0">&#41;</span></span><span style="color: #000; font-weight: bold;">$</span>lines i</pre></div></div>

<p>If you discount reading the input or writing the output in a particular format, then it can be trimmed down to 75 characters. The input, stored in &#8216;b&#8217;, is the &#8216;buildings&#8217; list from the first solution.</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #900; font-weight: bold;">h</span> x<span style="color: #000; font-weight: bold;">=</span>maximum<span style="color: #000; font-weight: bold;">$</span><span style="color: #099;">0</span><span style="color: #000; font-weight: bold;">:</span><span style="color: #283352;"><span class="br0">&#91;</span></span>y<span style="color: #000; font-weight: bold;">|</span><span style="color: #283352;"><span class="br0">&#40;</span></span>l<span style="color: #283352;">,</span>y<span style="color: #283352;">,</span>r<span style="color: #283352;"><span class="br0">&#41;</span></span><span style="color: #000; font-weight: bold;">&lt;-</span>b<span style="color: #283352;">,</span>l<span style="color: #000; font-weight: bold;">&lt;=</span>x<span style="color: #283352;">,</span>x<span style="color: #000; font-weight: bold;">&lt;</span>r<span style="color: #283352;"><span class="br0">&#93;</span></span>
<span style="color: #900; font-weight: bold;">print</span><span style="color: #283352;"><span class="br0">&#91;</span><span class="br0">&#40;</span></span>x<span style="color: #283352;">,</span>h x<span style="color: #283352;"><span class="br0">&#41;</span></span><span style="color: #000; font-weight: bold;">|</span>x<span style="color: #000; font-weight: bold;">&lt;-</span><span style="color: #283352;"><span class="br0">&#91;</span></span><span style="color: #099;">1</span><span style="color: #000; font-weight: bold;">..</span><span style="color: #099;">9999</span><span style="color: #283352;"><span class="br0">&#93;</span>,</span>h x<span style="color: #000; font-weight: bold;">/=</span>h<span style="color: #283352;"><span class="br0">&#40;</span></span>x<span style="color: #000; font-weight: bold;">-</span><span style="color: #099;">1</span><span style="color: #283352;"><span class="br0">&#41;</span><span class="br0">&#93;</span></span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/Jystic/~4/GKqP-7Oj264" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2010/05/16/the-skyline-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2010/05/16/the-skyline-problem/</feedburner:origLink></item>
		<item>
		<title>Code Bubbles</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/1QYkZT7wQ5w/</link>
		<comments>http://jystic.com/2010/04/05/code-bubbles/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 02:37:41 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ide]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=169</guid>
		<description><![CDATA[Today I came across a really cool UI concept for an IDE called Code Bubbles. You can check out the video here.
I love the idea of having a massive workspace and being able to see relevant functions side by side. It was also really cool how you can email workspaces around with annotations.
]]></description>
			<content:encoded><![CDATA[<p>Today I came across a really cool UI concept for an IDE called Code Bubbles. You can check out the video <a href="http://www.cs.brown.edu/people/acb/codebubbles_site.htm">here</a>.</p>
<p>I love the idea of having a massive workspace and being able to see relevant functions side by side. It was also really cool how you can email workspaces around with annotations.</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/1QYkZT7wQ5w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2010/04/05/code-bubbles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2010/04/05/code-bubbles/</feedburner:origLink></item>
		<item>
		<title>#haskell is awesome!</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/r5OU_1mdd2g/</link>
		<comments>http://jystic.com/2009/10/23/the-haskell-channel-is-awesome/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 17:34:53 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cabal]]></category>
		<category><![CDATA[happstack]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=145</guid>
		<description><![CDATA[I was trying to follow along with a few different happstack tutorials and I found that I needed to cabal install a few things. Unfortunately when I did &#8220;cabal install happs-tutorial&#8221; I got the following:

Happstack/Helpers/DirBrowse.hs:88:105:
    Couldn't match expected type `String' against inferred type `Bool'
    In the third argument of [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to follow along with a <a href="http://softwaresimply.blogspot.com/2009/04/basic-happstack-blog-app.html">few</a> <a href="http://tutorial.happstack.com">different</a> happstack tutorials and I found that I needed to cabal install a few things. Unfortunately when I did &#8220;cabal install happs-tutorial&#8221; I got the following:</p>
<pre>
Happstack/Helpers/DirBrowse.hs:88:105:
    Couldn't match expected type `String' against inferred type `Bool'
    In the third argument of `hscolour', namely `False'
    In the second argument of `(.)', namely
        `(hscolour defaultColourPrefs False False f)'
    In the first argument of `($)', namely
        `BrowseHtmlString . (hscolour defaultColourPrefs False False f)'
cabal: Error: some packages failed to install:
happs-tutorial-0.9.0 depends on happstack-helpers-0.30 which failed to
install.
happstack-helpers-0.30 failed during the building phase. The exception was:
exit: ExitFailure 1
</pre>
<p>It turns out that happstack-helpers is listed as dependening on hscolour >= 1.0 &#038;&#038; < 2.0 when in reality it fails to compile against v1.15 of hscolour. I was thinking, damn, I'm going to have to manually download the sources for happstack-helpers, edit the cabal file so it has the right references and compile it manually. That's horribly convoluted for something that must happen all the time, there must be a better way!</p>
<p>I've been sitting on <a href="http://www.haskell.org/haskellwiki/IRC_channel">#haskell</a> recently so I asked the question, and got some very speedy responses:</p>
<pre>cabal install happs-tutorial --constraint="hscolor == 1.14"</pre>
<p>That did the trick, and with a lot less effort!</p>
<p>I&#8217;ve asked quite a few questions on #haskell in the last few days and the response has been almost immediate every time and I&#8217;ve had my problem solved within minutes. Granted I&#8217;m still a Haskell newbie so my questions are probably trivial, but even still, it puts to shame the support I&#8217;ve received from a lot of commercial companies.</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/r5OU_1mdd2g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2009/10/23/the-haskell-channel-is-awesome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2009/10/23/the-haskell-channel-is-awesome/</feedburner:origLink></item>
		<item>
		<title>Happstack win!</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/z9zHwlglAcc/</link>
		<comments>http://jystic.com/2009/10/21/happstack-win/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 17:59:55 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cabal]]></category>
		<category><![CDATA[happstack]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=120</guid>
		<description><![CDATA[Hackage finally came back up, but I ran in to trouble with some guy called trhsx. Every time I tried to &#8216;cabal install happstack&#8217; I got the following error:

Configuring hsp-0.4.5...
Preprocessing library hsp-0.4.5...
Building hsp-0.4.5...
ghc: could not execute: trhsx
cabal: Error: some packages failed to install:
happstack-0.3.2 depends on hsp-0.4.5 which failed to install.
hsp-0.4.5 failed during the building phase. [...]]]></description>
			<content:encoded><![CDATA[<p>Hackage finally came back up, but I ran in to trouble with some guy called trhsx. Every time I tried to &#8216;cabal install happstack&#8217; I got the following error:</p>
<pre>
Configuring hsp-0.4.5...
Preprocessing library hsp-0.4.5...
Building hsp-0.4.5...
ghc: could not execute: trhsx
cabal: Error: some packages failed to install:
happstack-0.3.2 depends on hsp-0.4.5 which failed to install.
hsp-0.4.5 failed during the building phase. The exception was:
exit: ExitFailure 1
</pre>
<p>After some digging I found out that I was supposed to have ~/.cabal/bin in my path, who would have thought? Anyway, I added the following to my .bashrc:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PATH</span>=<span style="color: #007800;">$PATH</span>:~<span style="color: #000000; font-weight: bold;">/</span>.cabal<span style="color: #000000; font-weight: bold;">/</span>bin</pre></div></div>

<p>&#8230;and happstack installed without a hitch.</p>
<p>Win!</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/z9zHwlglAcc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2009/10/21/happstack-win/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2009/10/21/happstack-win/</feedburner:origLink></item>
		<item>
		<title>Hackage is down :(</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/sYzfk7UIqtg/</link>
		<comments>http://jystic.com/2009/10/19/hackage-is-down/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 13:24:21 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[happstack]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=107</guid>
		<description><![CDATA[Tonight I decided it was time to sink my teeth in to Happstack, the Haskell application server stack. I&#8217;m very keen to see how well Haskell handles web based applications and I really like the ideas behind Happstack.State. Unfortunately it looks like I may have picked the worst time of all as Hackage seems to [...]]]></description>
			<content:encoded><![CDATA[<p>Tonight I decided it was time to sink my teeth in to <a href="http://happstack.com">Happstack</a>, the Haskell application server stack. I&#8217;m very keen to see how well Haskell handles web based applications and I really like the ideas behind Happstack.State. Unfortunately it looks like I may have picked the worst time of all as <a href="http://hackage.haskell.org">Hackage</a> seems to be down, so no &#8220;cabal install&#8221; for me!</p>
<p>Lets see how I go getting the dependencies by hand&#8230;</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/sYzfk7UIqtg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2009/10/19/hackage-is-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2009/10/19/hackage-is-down/</feedburner:origLink></item>
		<item>
		<title>What’s with NullReferenceExceptions anyway?</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/cdUCdKtB5Bc/</link>
		<comments>http://jystic.com/2009/09/08/whats-with-nullreferenceexceptions-anyway/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 14:33:04 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[maybe monad]]></category>
		<category><![CDATA[monads]]></category>

		<guid isPermaLink="false">http://jystic.com/2009/09/08/whats-with-nullreferenceexceptions-anyway/</guid>
		<description><![CDATA[Tony Hoare recently gave a talk at QCon about what he calls his billion dollar mistake, null references. It certainly rings true for me &#8211; not knowing whether a value can be null or not has been quite a major source of frustration for me. Conversely, checking for null when a method can never or [...]]]></description>
			<content:encoded><![CDATA[<p>Tony Hoare recently gave a talk at QCon about what he calls his <a href="http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake">billion dollar mistake</a>, null references. It certainly rings true for me &#8211; not knowing whether a value can be null or not has been quite a major source of frustration for me. Conversely, checking for null when a method can never or should never return null is horrible as well, it bloats the code and distracts from the intent of the method.</p>
<p>ReSharper has some useful attributes that can be used to mitigate this somewhat. NotNullAttribute and CanBeNullAttribute give the ReSharper engine some hints that it can use to warn developers about potential mistakes, but at the end of the day you still end up with a boilerplate mess of if statements:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Apple apple <span style="color: #008000;">=</span> PickApple<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>apple <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
Orange orange <span style="color: #008000;">=</span> PickOrange<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>orange <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
FruitSalad fruitSalad <span style="color: #008000;">=</span> MakeFruitSalad<span style="color: #000000;">&#40;</span>apple, orange<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>fruitSalad <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">return</span> PackageForSale<span style="color: #000000;">&#40;</span>fruitSalad<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Or perhaps even worse, the lovely arrow-head pattern. Reaching for the far edge of the screen with every possible null:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Apple apple <span style="color: #008000;">=</span> PickApple<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>apple <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Orange orange <span style="color: #008000;">=</span> PickOrange<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>orange <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        FruitSalad fruitSalad <span style="color: #008000;">=</span> MakeFruitSalad<span style="color: #000000;">&#40;</span>apple, orange<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>fruitSalad <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> PackageForSale<span style="color: #000000;">&#40;</span>fruitSalad<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>A solution to this problem that I’ve been using recently is straight out of the Haskell playbook, the Maybe monad. This simple tool wraps up the concept of uncertainty so that it is composable. This allows you to rewrite the above code like this using LINQ:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">return</span> from apple <span style="color: #0600FF;">in</span> PickApple<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
       from orange <span style="color: #0600FF;">in</span> PickOrange<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
       from fruitSalad <span style="color: #0600FF;">in</span> MakeFruitSalad<span style="color: #000000;">&#40;</span>apple, orange<span style="color: #000000;">&#41;</span>
       select PackageForSale<span style="color: #000000;">&#40;</span>fruitSalad<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Sweet! So it&#8217;s just like an IEnumerable&lt;T&gt; that is either empty, or contains a single value. So how does it work? Lets look at the type signature for PickApple:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Maybe<span style="color: #008000;">&lt;</span>Apple<span style="color: #008000;">&gt;</span> PickApple<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>Instead of returning an Apple directly, we return Maybe&lt;Apple&gt; (I like to pronounce this “maybe an apple”). This gives us two advantages. Firstly, it captures in the type system that we might not return a value. Secondly, it allows us to define a raft of common functions for working with Maybe&lt;T&gt; values.</p>
<p>So what does Maybe&lt;T&gt; look like? Here are the basics:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> Maybe<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> <span style="color: #FF0000;">bool</span> m_HasValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> T m_Value<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> Maybe<span style="color: #000000;">&#40;</span>T value<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        m_Value <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        m_HasValue <span style="color: #008000;">=</span> value <span style="color: #008000;">!=</span> null<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> T Value
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> m_Value<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> HasValue
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> m_HasValue<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> HasNothing
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">!</span>m_HasValue<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">implicit</span> <span style="color: #0600FF;">operator</span> Maybe<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T value<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Maybe<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Maybe<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Nothing
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>Maybe<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It’s important to note that Maybe&lt;T&gt; is a struct. If it was a class then Maybe could be null, nothing or contain a value. So you’d need to check for three possible cases and we wouldn’t have gained much at all!</p>
<p>So far this is looking pretty similar to <a href="http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx">Nullable&lt;T&gt;</a> from the BCL. Unfortunately we can’t use Nullable&lt;T&gt; because it constrains T to structs only. This does bother me too much, I think Maybe&lt;T&gt; is a better name anyway.</p>
<p>So how do we do the funky LINQ stuff? We have to define what’s called the bind operator in  functional languages. It describes how we can chain one maybe on to the next, and what to do if we encounter a maybe which has nothing in it. Unfortunately, because LINQ was designed to have a familiar SQL feel to it, this bind operation needs to be an extension method called SelectMany. This enables C# and Visual Basic to make use of it with the LINQ syntax.</p>
<p>So this is what “bind” looks like for our Maybe monad:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Maybe<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span> SelectMany<span style="color: #008000;">&lt;</span>A, B<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Maybe<span style="color: #008000;">&lt;</span>A<span style="color: #008000;">&gt;</span> maybeA, Func<span style="color: #008000;">&lt;</span>A, Maybe<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;&gt;</span> aToMaybeB<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>maybeA.<span style="color: #0000FF;">HasNothing</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> Maybe<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">Nothing</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> aToMaybeB<span style="color: #000000;">&#40;</span>maybeA.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It’s nothing special, it takes maybe an A and a function which goes from an A to maybe a B. The important thing here is that if A doesn’t have a value then the function aToMaybeB isn’t called. We’re still missing a small piece of the puzzle, but we’re able to write our original function as this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">return</span> PickApple<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>apple <span style="color: #008000;">=&gt;</span>
       PickOrange<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>orange <span style="color: #008000;">=&gt;</span>
       MakeFruitSalad<span style="color: #000000;">&#40;</span>apple, orange<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>fruitSalad <span style="color: #008000;">=&gt;</span>
       PackageForSale<span style="color: #000000;">&#40;</span>fruitSalad<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The query syntax does work yet because LINQ requires one extra overload of SelectMany. This overload will describe how to chain two maybes together to yield a third. It’s purely an optimisation, so we’re not going to bother taking advantage of it in this case. We’ll just implement it in terms of our singular SelectMany:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Maybe<span style="color: #008000;">&lt;</span>C<span style="color: #008000;">&gt;</span> SelectMany<span style="color: #008000;">&lt;</span>A, B, C<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> Maybe<span style="color: #008000;">&lt;</span>A<span style="color: #008000;">&gt;</span> maybeA, Func<span style="color: #008000;">&lt;</span>A, Maybe<span style="color: #008000;">&lt;</span>B<span style="color: #008000;">&gt;&gt;</span> aToMaybeB, Func<span style="color: #008000;">&lt;</span>A, B, Maybe<span style="color: #008000;">&lt;</span>C<span style="color: #008000;">&gt;&gt;</span> abToMaybeC<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> maybeA.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>a <span style="color: #008000;">=&gt;</span> aToMaybeB<span style="color: #000000;">&#40;</span>a<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SelectMany</span><span style="color: #000000;">&#40;</span>b <span style="color: #008000;">=&gt;</span> abToMaybeC<span style="color: #000000;">&#40;</span>a, b<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Ouch! That’s one scary looking method! You don’t need to worry too much about this guy. Unless you’re planning to do optimisations it’s exactly the same for every single monad. Having said that, you should see similarities between it and our usage of SelectMany in the previous example.</p>
<p>Once we have our “double bind” operation defined, the linq syntax should work as described earlier:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">return</span> from apple <span style="color: #0600FF;">in</span> PickApple<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
       from orange <span style="color: #0600FF;">in</span> PickOrange<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
       from fruitSalad <span style="color: #0600FF;">in</span> MakeFruitSalad<span style="color: #000000;">&#40;</span>apple, orange<span style="color: #000000;">&#41;</span>
       select PackageForSale<span style="color: #000000;">&#40;</span>fruitSalad<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now that you have the basics you can add all sorts of extra functions for dealing with Maybe&lt;T&gt;. If I’m feeling keen I might post a follow up article on the additional functions that I’ve found useful. OrNull is a handy one for “unboxing” a Maybe&lt;T&gt; back to a naked value and OrThrow is another useful one which gets the inner value but throws if the Maybe&lt;T&gt; contains nothing.</p>
<p>So now you can say goodbye to if (a != null) right? Well, not quite, naked values always have the possibility of being null. It’s just an uncertainty that we’re going to have to deal with for the time being. However, if you always use Maybe&lt;T&gt; when a method might return “nothing” then you can fairly safely avoid checking for null when you’re working with values returned from your own code.</p>
<p>Hopefully we’ll be able to lock down naked values completely when we get code contracts in .NET 4.0. I’d love to see something which automatically annotates all arguments and return values with a contract ensuring that they can never be null.</p>
<p>A world without null, luxury!</p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/cdUCdKtB5Bc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2009/09/08/whats-with-nullreferenceexceptions-anyway/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://jystic.com/2009/09/08/whats-with-nullreferenceexceptions-anyway/</feedburner:origLink></item>
		<item>
		<title>Jystic theme for Visual Studio</title>
		<link>http://feedproxy.google.com/~r/Jystic/~3/Q7hb-6K_ICY/</link>
		<comments>http://jystic.com/2009/08/02/jystic-theme-for-visual-studio/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 07:39:01 +0000</pubDate>
		<dc:creator>Jacob Stanley</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[syntax highlighting]]></category>

		<guid isPermaLink="false">http://jystic.com/?p=68</guid>
		<description><![CDATA[When working with code I prefer to use themes with a dark background as I find they&#8217;re easier on the eyes. I love Rob Conery&#8217;s TextMate Theme and I&#8217;ve used it as a basis for my own theme.
The Jystic Theme uses a dark blue background and different colours for classes, interfaces, structs, delegates and enums. In [...]]]></description>
			<content:encoded><![CDATA[<p>When working with code I prefer to use themes with a dark background as I find they&#8217;re easier on the eyes. I love Rob Conery&#8217;s <a href="http://blog.wekeroad.com/blog/textmate-theme-for-visual-studio-take-2/">TextMate Theme</a> and I&#8217;ve used it as a basis for my own theme.</p>
<p>The Jystic Theme uses a dark blue background and different colours for classes, interfaces, structs, delegates and enums. In addition to C# highlighting, it also supports xaml, aspx and css.</p>
<p>C#</p>
<p><a href="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-csharp.png"><img class="alignnone size-full wp-image-74" title="Jystic Theme C#" src="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-csharp.png" alt="Jystic Theme C#" width="440" height="326" /></a></p>
<p>XAML</p>
<p><a href="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-xaml.png"><img class="alignnone size-full wp-image-79" title="Jystic Theme Xaml" src="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-xaml.png" alt="Jystic Theme Xaml" width="445" height="152" /></a></p>
<p>ASPX</p>
<p><a href="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-aspx.png"><img class="alignnone size-full wp-image-77" title="Jystic Theme Aspx" src="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-aspx.png" alt="Jystic Theme Aspx" width="419" height="291" /></a></p>
<p>CSS</p>
<p><a href="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-css.png"><img class="alignnone size-full wp-image-78" title="Jystic Theme Css" src="http://jystic.com/wp-content/uploads/2009/06/jystic-theme-css.png" alt="Jystic Theme Css" width="386" height="347" /></a></p>
<p><a title="Download Jystic Theme" href="http://jystic.com/wp-content/uploads/2009/06/Jystic.vssettings">Download Jystic Theme</a></p>
<p>The font I like to use is DejaVu Sans Mono.</p>
<p><a href="http://jystic.com/wp-content/uploads/2009/06/dejavu-sans-mono.png"><img class="alignnone size-full wp-image-81" title="DejaVu Sans Mono" src="http://jystic.com/wp-content/uploads/2009/06/dejavu-sans-mono.png" alt="DejaVu Sans Mono" width="294" height="73" /></a></p>
<p><a href="http://dejavu-fonts.org/wiki/index.php?title=Download">Download DejaVu Sans Mono</a></p>
<img src="http://feeds.feedburner.com/~r/Jystic/~4/Q7hb-6K_ICY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jystic.com/2009/08/02/jystic-theme-for-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jystic.com/2009/08/02/jystic-theme-for-visual-studio/</feedburner:origLink></item>
	</channel>
</rss>
