<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Developers, Developers, Developers! &#187; functional programming</title>
	<atom:link href="http://maksim.sorokin.dk/it/tag/functional-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://maksim.sorokin.dk/it</link>
	<description>Maksim Sorokin IT Blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 19:37:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Memory Leak in Haskell During File Read 3</title>
		<link>http://maksim.sorokin.dk/it/2010/05/23/memory-leak-in-haskell-during-file-read-3/</link>
		<comments>http://maksim.sorokin.dk/it/2010/05/23/memory-leak-in-haskell-during-file-read-3/#comments</comments>
		<pubDate>Sun, 23 May 2010 05:13:47 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[success story]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=213</guid>
		<description><![CDATA[Problem described in previous two posts (1 2) has finally been fully understood and solved. Guys from #haskell channel on freenode IRC servers pointed me to the problem. The thing is that in the memory leak example I took length of read file and appended it to accumulator, which was sent further in the tail [...]]]></description>
			<content:encoded><![CDATA[<p>Problem described in previous two posts (<a href="http://maksim.sorokin.dk/it/2010/05/18/memory-leak-in-haskell-during-file-read/">1</a> <a href="http://maksim.sorokin.dk/it/2010/05/19/memory-leak-in-haskell-during-file-read-2/">2</a>) has finally been fully understood and solved. Guys from #haskell channel on <a href="http://freenode.net/irc_servers.shtml">freenode IRC servers</a> pointed me to the problem.</p>
<p>The thing is that <span id="more-213"></span>in the memory leak <a href="http://maksim.sorokin.dk/it/2010/05/18/memory-leak-in-haskell-during-file-read/">example</a> I took length of read file and appended it to accumulator, which was sent further in the tail call. And this accumulator was evaluated lazily! After adding <a href="http://users.aber.ac.uk/afc/stricthaskell.html#seq">strict sequencing operator</a>, the memory leak gone.</p>
<p>So instead of tail call:</p>
<pre class="brush: plain; title: ;">test fns $ acc + (B.length contents)</pre>
<p>I should have used:</p>
<pre class="brush: plain; title: ;">test fns $! acc + (B.length contents)</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/05/23/memory-leak-in-haskell-during-file-read-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Memory Leak in Haskell During File Read 2</title>
		<link>http://maksim.sorokin.dk/it/2010/05/19/memory-leak-in-haskell-during-file-read-2/</link>
		<comments>http://maksim.sorokin.dk/it/2010/05/19/memory-leak-in-haskell-during-file-read-2/#comments</comments>
		<pubDate>Wed, 19 May 2010 19:15:26 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[memory leak]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=193</guid>
		<description><![CDATA[UPD.: Issue has been resolved Things started to getting clearer. Here is a nice page explaining laziness during file read in Haskell (thanks to Vadim Platonov), section "hGetContents, hClose, readFile". I have tried samples from that resource which should solve unwanted laziness, but memory leak still occurred. Moreover, those samples ran for ages on just [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPD.:</strong> Issue has been <a href="http://maksim.sorokin.dk/it/2010/05/23/memory-leak-in-haskell-during-file-read-3/">resolved</a></p>
<p>Things started to getting clearer. <a href="http://users.aber.ac.uk/afc/stricthaskell.html#semiclosed">Here</a> is a nice page explaining laziness during file read in Haskell (thanks to <a href="http://lt.linkedin.com/pub/vadim-platonov/9/315/1b">Vadim Platonov</a>), section "hGetContents, hClose, readFile".<br />
I have tried samples from that resource which should solve unwanted laziness, but memory leak still occurred. Moreover, those samples ran for ages on just several thousands of files and ate much more memory comparing to standard <code>System.IO</code> functions.</p>
<p>But my colleague just informed me, that the version <a href="http://maksim.sorokin.dk/it/2010/05/18/memory-leak-in-haskell-during-file-read/">which I posted previously</a> runs without leaks on newer ghci version. I am too f*cked up after working day, so I hope to figure that out tomorrow.</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/05/19/memory-leak-in-haskell-during-file-read-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Config Files in Haskell</title>
		<link>http://maksim.sorokin.dk/it/2010/05/09/config-files-in-haskell/</link>
		<comments>http://maksim.sorokin.dk/it/2010/05/09/config-files-in-haskell/#comments</comments>
		<pubDate>Sun, 09 May 2010 08:09:41 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=170</guid>
		<description><![CDATA[Here is an easy and simple way to read configuration files in Haskell using ConfigFile package. Here is a simple configuration file test.cfg example: key: value And here is the program which reads the configuration file: import Data.ConfigFile import Data.Either.Utils main = do val &#60;- readfile emptyCP &#34;test.cfg&#34; let cp = forceEither val putStrLn $ [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an easy and simple way to read configuration files in Haskell using <a href="http://hackage.haskell.org/package/ConfigFile">ConfigFile</a> package.<br />
<span id="more-170"></span></p>
<p>Here is a simple configuration file <code>test.cfg</code> example:</p>
<pre class="brush: plain; title: ;">
key: value
</pre>
<p>And here is the program which reads the configuration file:</p>
<pre class="brush: plain; title: ;">
import Data.ConfigFile
import Data.Either.Utils

main =
 do val &lt;- readfile emptyCP &quot;test.cfg&quot;
    let cp = forceEither val
    putStrLn $ forceEither $ get cp &quot;&quot; &quot;key&quot;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/05/09/config-files-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Haskell and MySQL</title>
		<link>http://maksim.sorokin.dk/it/2010/04/24/haskell-and-mysql/</link>
		<comments>http://maksim.sorokin.dk/it/2010/04/24/haskell-and-mysql/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 09:38:48 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=154</guid>
		<description><![CDATA[In this post I will describe how to connect and use MySQL with Haskell. In this example we will use Ubuntu machine. We will use MySQL driver for HDBC from HackageDB. First, we need to install MySQL server: sudo apt-get install mysql-server Then we need MySQL client library: sudo apt-get install libmysqlclient-dev Then, we install [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will describe how to connect and use MySQL with Haskell.<span id="more-154"></span></p>
<p>In this example we will use Ubuntu machine. We will use <a href="http://hackage.haskell.org/package/HDBC-mysql">MySQL driver for HDBC</a> from HackageDB.</p>
<p>First, we need to install MySQL server:</p>
<pre class="brush: plain; title: ;">sudo apt-get install mysql-server</pre>
<p>Then we need MySQL client library:</p>
<pre class="brush: plain; title: ;">sudo apt-get install libmysqlclient-dev</pre>
<p>Then, we install <a href="http://hackage.haskell.org/package/HDBC-mysql">HDBC MySQL driver</a> using <a href="http://www.haskell.org/cabal/">Cabal</a>:</p>
<pre class="brush: plain; title: ;">http://hackage.haskell.org/package/HDBC-mysql</pre>
<p>And you are ready to write some code!</p>
<pre class="brush: plain; title: ;">
module DatabaseTest where

import Database.HDBC
import Database.HDBC.MySQL

main =
   do conn &lt;- connectMySQL defaultMySQLConnectInfo {
                  mysqlHost = &quot;localhost&quot;,
                  mysqlDatabase = &quot;db&quot;,
                  mysqlUser = &quot;user&quot;,
                  mysqlPassword = &quot;pass&quot;,
                  mysqlUnixSocket = &quot;/var/run/mysqld/mysqld.sock&quot; }
      quickQuery conn &quot;INSERT INTO test VALUES (1)&quot; []
</pre>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/04/24/haskell-and-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Printing Data Structures With Standard ML</title>
		<link>http://maksim.sorokin.dk/it/2010/03/26/printing-data-structures-with-standard-ml/</link>
		<comments>http://maksim.sorokin.dk/it/2010/03/26/printing-data-structures-with-standard-ml/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 16:49:05 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Standard ML]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=111</guid>
		<description><![CDATA[Standard ML is a nice functional programming language. But unfortunately it is painful to debug applications. At least for me after using Haskell for some time. The problem is that there is no way to easy print out the data structure in order to debug it in good old "sysout" way. For certain reason, I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Standard_ML">Standard ML</a> is a nice functional programming language. But unfortunately it is painful to debug applications. At least for me after using Haskell for some time. The problem is that there is no way to easy print out the data structure in order to debug it in good old "sysout" way.<br />
<span id="more-111"></span></p>
<p>For certain reason, I had to modify <a href="http://en.wikipedia.org/wiki/Prolog">Prolog</a> interpreter written in Standard ML. I used <a href="http://www.itu.dk/~sestoft/mosml.html">Moscow ML</a> implementation. Interpreter had structures which look like:</p>
<pre class="brush: plain; title: ;">...
datatype Term = Var of string * pos

              | Fun of string * Term list * pos

type Atom = string * Term list

datatype Query = And of Query * Query * pos
               | Or of Query * Query * pos
...</pre>
<p>So in order to print those structure, one can do that in the top level mode of Moscow ML. But this may be somewhat inconvenient. So I ended up writing my own <code>PrettyPrinter</code> module where I created separate data type for types I wanted to print:</p>
<pre class="brush: plain; title: ;">datatype PrintType =
         Clause of Prolog.Clause
       | ClauseList of Prolog.Clause list
       | Atom of Prolog.Atom
       | Query of Prolog.Query
       | Term of Prolog.Term
       ...</pre>
<p>And had following <code>print</code> and <code>show</code> functions:</p>
<pre class="brush: plain; title: ;">fun show (Clause (atom, query)) =
		(show (Atom atom)) ^ &quot; &quot; ^ (show (Query query))
  | show (ClauseList cl) =
		&quot;[&quot; ^ (showList (ClauseList cl))  ^ &quot;]&quot;
  | show (Atom (atomName, terms)) =
  ...
and showList(Clause _) = &quot;&quot;             (* shouldn't happen *)
  | showList(ClauseList []) = &quot;&quot;
  | showList(ClauseList (c::cs)) =
		(show (Clause c)) ^ (getListSeparator cs) ^ (showList (ClauseList cs))
  ...

fun print pt = BasicIO.print (show pt ^ &quot;\n&quot;)</pre>
<p>Annoying.</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/03/26/printing-data-structures-with-standard-ml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Project On Data Mining</title>
		<link>http://maksim.sorokin.dk/it/2010/03/23/new-project-on-data-mining/</link>
		<comments>http://maksim.sorokin.dk/it/2010/03/23/new-project-on-data-mining/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 18:25:42 +0000</pubDate>
		<dc:creator>Maksim Sorokin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[data mining]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://maksim.sorokin.dk/it/?p=108</guid>
		<description><![CDATA[I am happy to announce, that I am starting working on my first research project for Copenhagen University. This project is about data mining on huge base of XML documents. Since we all like functional languages at our faculty, most part of it will be implemented using them. So I hope soon there will be [...]]]></description>
			<content:encoded><![CDATA[<p>I am happy to announce, that I am starting working on my first research project for <a href="http://diku.dk/">Copenhagen University</a>. This project is about data mining on huge base of XML documents. Since we all like functional languages at our faculty, most part of it will be implemented using them. So I hope soon there will be a lot of posts about data mining and functional languages!</p>
]]></content:encoded>
			<wfw:commentRss>http://maksim.sorokin.dk/it/2010/03/23/new-project-on-data-mining/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

