<?xml version="1.0" encoding="iso-8859-1"?>
        <?xml-stylesheet type="text/css" href="http://www.miek.nl/blog/"?>
<rss version="2.0"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:admin="http://webns.net/mvcb/"
 xmlns:atom="http://www.w3.org/2005/Atom"
>
<channel>
<title>Filed under: erlang | Miek</title>
<atom:link href="http://www.miek.nl/blog/archives/erlang/index-rss.xml" rel="self" type="application/rss+xml" />
<link>http://www.miek.nl/blog</link>
<description>Thoughts on (technical) stuff</description>
<dc:language>en-us</dc:language>
<dc:creator>Miek Gieben</dc:creator>
<dc:date>2012-02-04T04:15:11+01:00</dc:date>
<admin:generatorAgent rdf:resource="http://nanoblogger.sourceforge.net" />

<item>
<link>http://www.miek.nl/blog/archives/2009/04/18/coding_erlang/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/04/18/coding_erlang/index.html</guid>
<title>Coding Erlang</title>
<dc:date>2009-04-18T20:08:34+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> erlang</dc:subject>
<description><![CDATA[<p>For the past year or so I've been trying to learn a new language called
<a href="http://www.erlang.org">Erlang</a>. </p>

<p>I've found this <a href="http://www.castro.aus.net/~maurice/serc/erlbk/">nice
document</a>, which can be
used when learning the language. So here I am on Saturday evening doing
some Erlang exercises :-)</p>

<p>Anyhow, I was doing exercise 3.4:</p>

<ol>
<li>Erlang provides a function <em>lists:append</em> which joins two lists
together, implement you own function <em>append</em> that performs the append
operation. (<strong>Do NOT peek</strong>; the answer is given in figure 3.11).</li>
</ol>

<p>And the answer given is:</p>

<div class="nbcode">
<pre>
<span class="lnr">1 </span><span class="Type">int</span> main(<span class="Type">int</span> c) {
<span class="lnr">2 </span>    printf(<span class="Constant">&quot;This is nice&quot;</span>);
<span class="lnr">3 </span>    exit(<span class="Constant">1</span>);
<span class="lnr">4 </span>}
</pre>
</div>

<p>However, when I test this I get:</p>

<pre><code>% erl
1&gt; c(append).
{ok,append}

2&gt; append:append([4,5,6], [1,2,3]).
[4,[5,[6,[1,2,3]]]]
</code></pre>

<p>Which made me suspicious. Checking with the official <em>BIF</em>:</p>

<pre><code>3&gt; lists:append([4,5,6], [1,2,3]). 
[4,5,6,1,2,3]
</code></pre>

<p>Hmm. With a little help from "Programming Erlang" I found the <code>++</code>
operator for adding lists together. So my final solution became:</p>

<div class="nbcode">
<pre>
<span class="lnr">1 </span><span class="Type">int</span> main(<span class="Type">int</span> c) {
<span class="lnr">2 </span>    printf(<span class="Constant">&quot;This is nice&quot;</span>);
<span class="lnr">3 </span>    exit(<span class="Constant">1</span>);
<span class="lnr">4 </span>}
</pre>
</div>

<p>Checking:</p>

<pre><code>4&gt; c(append).                      
{ok,append}
5&gt; append:append([4,5,6], [1,2,3]).
[4,5,6,1,2,3]
</code></pre>

<p>The only ugly thing IMHO is the creation of the one element list with
<code>[H]</code> at the last line.</p>]]></description>

</item>
</channel>
</rss>

