<?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>Ikke&#039;s blog &#187; functional programming</title>
	<atom:link href="http://eikke.com/tag/functional-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://eikke.com</link>
	<description>&#039;cause this is what I do</description>
	<lastBuildDate>Sun, 13 Feb 2011 14:58:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>On Functional Programming Languages</title>
		<link>http://eikke.com/on-functional-programming-languages/</link>
		<comments>http://eikke.com/on-functional-programming-languages/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 21:50:38 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[fp]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=173</guid>
		<description><![CDATA[As a programming-language adept I&#8217;ve been studying the ideas, concepts and theory of functional programming (FP) and FP-related languages for about 2 years now, still learning new things everyday. Recently a &#8216;FP User Group&#8217; was started by some people at Ghent University, called GhentFPG, and the first meeting took place last thursday, with great interest [...]]]></description>
			<content:encoded><![CDATA[<p>As a programming-language adept I&#8217;ve been studying the ideas, concepts and theory of <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a> (FP) and FP-related languages for about 2 years now, still learning new things everyday.</p>
<p>Recently a &#8216;FP User Group&#8217; was started by some people at <a href="http://www.ugent.be">Ghent University</a>, called GhentFPG, and the first meeting took place last thursday, with great interest from students, university employees as well as people working in the industry. You can find some more info in the <a href="http://groups.google.com/group/ghent-fpg">GhentFPG Google Group</a> or in the <a href="http://www.haskell.org/haskellwiki/Ghent_Functional_Programming_Group">wiki</a> (where you can also find the slides of the presentations given during the first meeting).</p>
<p>Some days ago someone new to FP posted a <a href="http://groups.google.com/group/ghent-fpg/browse_thread/thread/81cee5d8c9a77266">message on the mailing list</a>, asking which language he should study, among other things.</p>
<p>Since I think my reply might be of general interest (also outside GhentFPG), I decided to post a copy on this blog as well (note I did add some extra markup). Comments welcome!</p>
<blockquote><p>
Based on my experience (which is biased, obviously):</p>
<ul>
<li>
<p>Functional Programming is not only a language-related thing. FP<br />
languages do enforce you to apply functional paradigms, but you can<br />
easily follow these paradigms in lots of other (more mainstream?)<br />
languages as well: it is easier to learn people a paradigm using a<br />
language they already know, rather than telling them FP is really cool<br />
and useful and interesting, but requires them to learn a new<br />
language/toolchain/&#8230; first.</p>
<p>Not talking about <a href="http://www.java.net">Java</a> or <a href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a> or something similar here, rather <a href="http://www.python.org">Python</a><br />
and <a href="http://ruby-lang.org/">Ruby</a>.</p>
<li>
<p>If you&#8217;re into Java/<a href="http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx">C#</a>/&#8230;, <a href="http://www.scala-lang.org/"><em>Scala</em></a> is a really good introduction to FP:<br />
it allows you to write OOP code just like you do already, but also<br />
provides you lots of FP-related features, and pushes you gently into the<br />
FP approach. The book &#8220;Programming in Scala&#8221; by Odersky et al. (the main<br />
author of Scala) is IMO a really good intro to both Scala as well as the<br />
FP concepts it provides, not only showing them but also explaining<br />
gently why they&#8217;re useful, and why they&#8217;re &#8216;better&#8217; than the approaches<br />
you&#8217;re taking already.</p>
<p>The Scala type system is rather interesting as well.</p>
<p>It&#8217;s the gentle path, so you want <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Learning Scala before reading<br />
&#8216;<a href="http://www.realworldhaskell.org/blog/">Real World Haskell</a>&#8216; certainly helped me a lot to understand the latter.</p>
<li>
<p><a href="http://haskell.org/"><em>Haskell</em></a> is an incredibly interesting language because of the concepts<br />
it adopted and types it provides, but it does require an immediate mind<br />
switch when coming from a non-OOP world (I once spent about 2 hours to<br />
explain a Java-guy how classes and instances in Haskell relate to<br />
classes and instances in Java, it wasn&#8217;t obvious). &#8220;Real World Haskell&#8221;<br />
is certainly worth a read (and if you read &#8220;Programming in Scala&#8221; as<br />
well, you&#8217;ll notice lots of similarities).</p>
<p>I for one can read Haskell code pretty easily and learned lots of<br />
CS/math things thanks to learning it, but I&#8217;m (still) unable to write<br />
non-trivial code (I need some good project to get my hands dirty I<br />
guess).</p>
<li>
<p><a href="http://www.erlang.org"><em>Erlang</em></a> is really interesting from a (very specific) feature<br />
perspective: high-availability, distributed computing, the actor system<br />
and the OTP library on top of it,&#8230;</p>
<p>It&#8217;s a rather &#8216;old&#8217; language, but I kind of like it. Some people do<br />
complain about the syntax, but once you figured out &#8216;,&#8217;, &#8216;;&#8217; and &#8216;.&#8217; are<br />
used almost the same as they are in &#8216;human&#8217; written language, everything<br />
becomes obvious  <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Do note though Erlang is not a normal general-purpose language. You can<br />
code +- everything you want using it, but it&#8217;s really targeted to<br />
distributed/high-available/network applications. You most likely won&#8217;t<br />
use it to solve mathematical problems or write a game. It&#8217;s really good<br />
at what it&#8217;s built for though.</p>
<p>One final note: please don&#8217;t ever make the mistake I made. If you know<br />
Erlang, and take a look at Scala (which also has an actor library in the<br />
standard distribution, as well as the more advanced <a href="http://akkasource.org">Akka</a>-library), don&#8217;t<br />
judge Scala as being a competitor for Erlang, they&#8217;re both completely<br />
different languages targeting different applications. &#8216;Scala&#8217; is not<br />
about &#8216;scalability&#8217; as Erlang is (it&#8217;s a &#8220;Scalable Language&#8221;).</p>
<li>
<p><a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/"><em>F#</em></a> (and most likely <a href="http://caml.inria.fr/ocaml/"><em>OCaml</em></a> as well, although I never used it though) is<br />
certainly worth a look as well. I only read 3/4th of a book on it, but<br />
it looks really promising and interesting.</p>
<li>
<p>There&#8217;s obviously all sorts of <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)"><em>Lisp</em></a> dialects. I have no opinion on<br />
them, never looked into any Lisp closely enough. I only wrote some<br />
<a href="http://clojure.org"><em>Clojure</em></a> (a Lisp-dialect for the JVM) code one day, but need to learn<br />
more about the Lisp-way of programming. Clojure seems to be interesting<br />
because of the deep integration of <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">Software Transactional Memory</a> (STM)<br />
in the language (yet another approach to concurrency <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ).</p>
</ul>
<p>As for the IDE question: <a href="http://www.vim.org">Vim</a> and a decent terminal are all you need,<br />
luckily none of the above languages require you to learn how to use a<br />
toolchain which enforces you (or some magic IDE) to write 500 lines of<br />
XML-based build &#8216;programs&#8217; or other insanities.</p>
<p>My advice: pick some language, learn it, but make sure you don&#8217;t only<br />
learn the language, but especially the concepts (type system,<br />
higher-order stuff, list manipulation,&#8230;). Then pick some other<br />
language and learn it as well (which will be easier since you got the<br />
concepts already) and so on.</p>
<p>And read tons of papers available on the internet in between <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Even if<br />
you don&#8217;t understand a paper completely, you&#8217;ll pick up some things<br />
already, and re-reading it 2 weeks later helps a lot <img src='http://eikke.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
<p>Just my .02,</p>
<p>Nicolas</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/on-functional-programming-languages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Scala tail recursion and decompiler adventures</title>
		<link>http://eikke.com/scala-tail-recursion-decompiler/</link>
		<comments>http://eikke.com/scala-tail-recursion-decompiler/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 22:31:03 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=129</guid>
		<description><![CDATA[I've been into Scala lately. More about it will follow later, but there's something I found out which I really like.

Last couple of days I've been writing some very basic Scala snippets, containing constructs which would be non-trivial or 'unusual' to write in Java, compile it to a class file, and then use a Java decompiler to figure out how the Scala compiler maps those constructs on the JVM.

There's one thing which took my immediate attention: looks like (basic) tail-recursive functions are optimized into while-loops!]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been into <a href="http://www.scala-lang.org">Scala</a> lately. More about it will follow later, but there&#8217;s something I found out which I really like.</p>
<p>Last couple of days I wrote some very basic Scala snippets, containing constructs which would be non-trivial or &#8216;unusual&#8217; to write in <a href="http://java.sun.com">Java</a>, compile it to a class file, and then use a <a href="http://java.decompiler.free.fr">Java decompiler</a> to figure out how the Scala compiler maps those constructs to JVM bytecodes.</p>
<p>There&#8217;s one thing which took my attention: looks like (basic) tail-recursive functions are optimized into while-loops! This only happens if the last call of a function is a call to itself (the most basic form of tail recursion), but it&#8217;s an interesting feature anyway&#8230; No more need to put socket accept handling in an infinite while loop <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
<span id="more-129"></span><br />
A little demo. First, here&#8217;s a Scala object which implements a very basic &#8216;reduce&#8217; function:</p>
<pre>
object Reducer {
  def reduce[T, V](fun: (V, T) => V, values: List[T], initial: V): V = {
    if(values isEmpty)
      return initial
    val next = fun(initial, values head)
    return reduce(fun, values tail, next)
  }

  def main(args: Array[String]): Unit = {
    val values = List(1, 2, 3, 4)
    val sum = reduce[Int, Int]((x, y) => x + y, values, 0)
    println("Result: " + sum)
  }
}
</pre>
<p>We can compile and run this, and it&#8217;ll output the expected result &#8217;10&#8242;:</p>
<pre>MacBook:reduce nicolas $ scalac Reducer.scala
MacBook:reduce nicolas $ scala Reducer
Result: 10</pre>
<p>Now we can open the generated class files in JD. There are a couple of them (it&#8217;s interesting to take a look at all of them and figure out what they represent exactly), but in this case we need &#8216;Reducer$.class&#8217;, which contains the implementations of our public functions, including &#8216;reduce&#8217;.</p>
<p>Here&#8217;s the Java version of the &#8216;reduce&#8217; function:</p>
<pre>public &lt;T, V&gt; V reduce(Function2&lt;V, T, V&gt; fun, List&lt;T&gt; values, V initial)
{
  while (true)
  {
    if (values.isEmpty())
      return initial;
    Object next = fun.apply(initial, values.head());
    initial = next;
    values = values.tail();
  }
}
</pre>
<p>&#8216;Function2&#8242; is a built-in Scala type which represents a function taking 2 parameters. As you can see, this code does exactly the same as our Scala version and is most likely the way we&#8217;d write the code manually as well (the only thing I don&#8217;t get is why &#8216;next&#8217; is an Object and not a &#8216;V&#8217;, I might figure that out later), but without forcing us to write the imperative code, whilst still producing bytecodes which will most likely show the best performance on the JVM (which currently has no tail recursion optimization support (although <a href="http://openjdk.java.net/projects/mlvm/subprojects.html#TailCalls">that might change one day</a>)).</p>
<p>I like it <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>[update]<br />
For reference, here&#8217;s a slightly more Scala-ish implementation of <em>reduce</em>, showing the same time performance characteristics during some basic profiling. I was not able to get JD nor jad to generate any usable decompiled code though:</p>
<pre>
def reduce[T, V](fun: (V, T) => V, values: List[T], initial: V): V = {
    values match {
        case List() => initial;
        case head :: tail => reduce(fun, tail, fun(initial, head))
    }
}
</pre>
<p>It uses Scala&#8217;s &#8220;List&#8221; pattern matching functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/scala-tail-recursion-decompiler/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Python if/else in lambda</title>
		<link>http://eikke.com/python-ifelse-in-lambda/</link>
		<comments>http://eikke.com/python-ifelse-in-lambda/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 21:24:43 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eikke.com/python-ifelse-in-lambda/</guid>
		<description><![CDATA[Scott, in your &#8220;Functional Python&#8221; introduction you write: The one limitation that most disappoints me is that Python lacks is a functional way of writing if/else. Sometimes you just want to do something like this: lambda x : if_else(x&#62;100, “big number”, “little number”) (This would return the string “big number” if x was greater than [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://scott.andstuff.org/" title="Scott Moonen">Scott</a>, in your &#8220;<a href="http://scott.andstuff.org/FunctionalPython" title="Scott Moonen: Functional Python">Functional Python</a>&#8221; introduction you write:</p>
<blockquote><p>The one limitation that most disappoints me is that Python lacks is a functional way of writing <tt>if</tt>/<tt>else</tt>.  Sometimes you just want to do something like this:</p>
<blockquote>
<pre>lambda x : if_else(x&gt;100, “big number”, “little number”)</pre>
</blockquote>
<p>(This would return the string “big number” if <em>x</em> was greater than 100, and “little number” otherwise.)  Sometimes I get around this by defining my own <tt>if_else</tt> that I can use in lambda-functions:</p>
<blockquote>
<pre>def if_else(condition, a, b) :
   if condition : return a
   else         : return b</pre>
</blockquote>
</blockquote>
<p>Actually, you don&#8217;t need this helper if_else function at all:</p>
<pre>In [1]: f = lambda x: x &gt; 100 and 'big' or 'small'</pre>
<pre>In [2]: for i in (1, 10, 99, 100, 101, 110):
...:     print i, 'is', f(i)
...:
1 is small
10 is small
99 is small
100 is small
101 is big
110 is big</pre>
<p><a href="http://blogs.gnome.org/jamesh/2008/02/12/python-metaclasses/" title="James Henstridge: Python metaclasses">James</a>, obviously you&#8217;re right&#8230; Stupid me didn&#8217;t think about that. Your version won&#8217;t work when a discriminator isn&#8217;t known at import time. But even then a function taking <tt>*args</tt> and <tt>**kwargs</tt> with a class-like name, returning a correct class instance, would cut the job.</p>
<p>Regarding the module/plugin stuff, I&#8217;d rather use setuptools/pkg_resources <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/python-ifelse-in-lambda/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

