<?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>Untamed Wilds &#187; erlang</title>
	<atom:link href="http://thomaswebb.net/category/computers/coding/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://thomaswebb.net</link>
	<description>Human ecology, human action and human nature</description>
	<lastBuildDate>Tue, 06 Mar 2012 18:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Exercise 3-6: Sorting Lists &amp; 3-7 Using Library Modules</title>
		<link>http://thomaswebb.net/2009/11/04/exercise-3-6-sorting-lists/</link>
		<comments>http://thomaswebb.net/2009/11/04/exercise-3-6-sorting-lists/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 03:19:54 +0000</pubDate>
		<dc:creator>Thomas J. Webb</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[exercises]]></category>

		<guid isPermaLink="false">http://thomaswebb.net/?p=653</guid>
		<description><![CDATA[It looks like there&#8217;s one other guy on the internet who&#8217;s posting his answers for the exercises in this book. So, I&#8217;ll be linking to his so you (and I) can compare our answers. He knows what he&#8217;s doing. In &#8230; <a href="http://thomaswebb.net/2009/11/04/exercise-3-6-sorting-lists/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It looks like there&#8217;s one other guy on the internet who&#8217;s posting his answers for the exercises in this book. So, I&#8217;ll be linking to his so you (and I) can compare our answers. He knows what he&#8217;s doing. In his posts, he talks about, e.g., what he&#8217;d do in Haskell. I&#8217;m not a functional programming guy (lisp gives me the heebie-geebies, my earlier post on evolutionary programming where I used it notwithstanding). So, below are my answers (click to see more if you&#8217;re looking at the blog and not this post by itself) and <a href="http://d.hatena.ne.jp/vostok92/20090817/1250509904" target="_blank">here</a> is the Japanese guy&#8217;s answers.</p>
<p><span id="more-653"></span>[erlang]<br />
-module(sort).<br />
-export([quicksort/1,mergesort/1]).</p>
<p>less([Head|Tail], Pivot) when Head &gt;= Pivot -&gt;<br />
	less(Tail, Pivot);<br />
less([Head|Tail], Pivot) when Head &lt; Pivot -&gt;<br />
	[Head|less(Tail, Pivot)];<br />
less([],_) -&gt;<br />
	[].</p>
<p>more([Head|Tail], Pivot) when Head &gt;= Pivot -&gt;<br />
        [Head|more(Tail, Pivot)];<br />
more([Head|Tail], Pivot) when Head &lt; Pivot -&gt;<br />
        more(Tail, Pivot);<br />
more([],_) -&gt;<br />
        [].</p>
<p>quicksort([Head|Tail]) -&gt;<br />
	quicksort(less(Tail,Head)) ++ [Head] ++ quicksort(more(Tail,Head));<br />
quicksort([]) -&gt;<br />
	[].</p>
<p>% For reference, here&#8217;s the quicksort from wikipedia&#8217;s page on erlang&#8230; much better-looking than my crap (but in my defense, I didn&#8217;t know how to do this kind of list construction&#8230; cool!)<br />
% quicksort([]) -&gt; [];<br />
% quicksort([Pivot|Rest]) -&gt;<br />
% quicksort([Front || Front &lt;- Rest, Front &lt; Pivot])<br />
% ++ [Pivot] ++<br />
% quicksort([Back || Back &lt;- Rest, Back &gt;= Pivot]).</p>
<p>merge([Head1|Tail1],[Head2|Tail2]) when Head2 &gt; Head1 -&gt;<br />
	[Head1] ++ merge(Tail1,[Head2|Tail2]);<br />
merge([Head1|Tail1],[Head2|Tail2]) when Head2 =&lt; Head1 -&gt;<br />
	[Head2] ++ merge([Head1|Tail1],Tail2);<br />
merge([],[]) -&gt;<br />
	[];<br />
merge(End,[]) -&gt;<br />
	End;<br />
merge([],End) -&gt;<br />
	End.</p>
<p>mergesort([]) -&gt;<br />
	[];<br />
mergesort([Left]) -&gt;<br />
	[Left];<br />
mergesort(List) -&gt;<br />
	{Left, Right} = lists:split(length(List) div 2, List),<br />
	merge(mergesort(Left), mergesort(Right)).</p>
<p>[/erlang]</p>
<p>[erlang]<br />
-module(db_lists).<br />
-export([new/0, destroy/1, write/3, delete/2, read/2, match/2]).</p>
<p>new() -&gt;<br />
	[].</p>
<p>destroy(Db) -&gt;<br />
	ok.</p>
<p>write(Key, Element, Db) -&gt;<br />
	Db ++ [{Key, Element}].</p>
<p>delete(Key, List) -&gt;<br />
	lists:keydelete(Key, 1, List).</p>
<p>read(Key, List) -&gt;<br />
	case (lists:keyfind(Key, 1, List)) of<br />
		{MatchKey, Element} -&gt;<br />
			{ok, Element};<br />
		_ -&gt;<br />
			{error, instance}<br />
	end.</p>
<p>match(Element, [{DbHeadKey, DbHeadElement}|DbTail]) -&gt;<br />
	case DbHeadElement of<br />
		Element -&gt; [DbHeadKey] ++ match(Element, DbTail);<br />
		_ -&gt; match(Element, DbTail)<br />
	end;<br />
match(Element, []) -&gt;<br />
	[].</p>
<p>[/erlang]</p>
]]></content:encoded>
			<wfw:commentRss>http://thomaswebb.net/2009/11/04/exercise-3-6-sorting-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 3-5: Manipulating Lists</title>
		<link>http://thomaswebb.net/2009/11/03/exercise-3-5-manipulating-lists/</link>
		<comments>http://thomaswebb.net/2009/11/03/exercise-3-5-manipulating-lists/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 18:19:52 +0000</pubDate>
		<dc:creator>Thomas J. Webb</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[exercise]]></category>

		<guid isPermaLink="false">http://thomaswebb.net/?p=647</guid>
		<description><![CDATA[Next installment in my &#8220;Thomas does your homework&#8221; series&#8230; [erlang] -module(list). -export([filter/2, reverse/1, concatenate/1, flatten/1]). filter([Head&#124;Tail],Num) when Head =&#60; Num -&#62; [Head&#124;filter(Tail,Num)]; filter([Head&#124;Tail],Num) when Head &#62; Num -&#62; filter(Tail,Num); filter([],_) -&#62; []. reverse([Head&#124;Tail]) -&#62; reverse(Tail) ++ [Head]; reverse([]) -&#62; []. &#8230; <a href="http://thomaswebb.net/2009/11/03/exercise-3-5-manipulating-lists/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Next installment in my &#8220;Thomas does your homework&#8221; series&#8230;</p>
<p><span id="more-647"></span></p>
<p>[erlang]<br />
-module(list).<br />
-export([filter/2, reverse/1, concatenate/1, flatten/1]).</p>
<p>filter([Head|Tail],Num) when Head =&lt; Num -&gt;<br />
	[Head|filter(Tail,Num)];<br />
filter([Head|Tail],Num) when Head &gt; Num -&gt;<br />
	filter(Tail,Num);<br />
filter([],_) -&gt;<br />
	[].</p>
<p>reverse([Head|Tail]) -&gt;<br />
	reverse(Tail) ++ [Head];<br />
reverse([]) -&gt;<br />
	[].</p>
<p>concatenate([Head|Tail]) -&gt;<br />
	Head ++ concatenate(Tail);<br />
concatenate([]) -&gt;<br />
	[].</p>
<p>% Probably possible to make this a LOT more efficient, ne<br />
flatten([Head|Tail]) -&gt;<br />
	flatten(Head) ++ flatten(Tail);<br />
flatten([]) -&gt;<br />
	[];<br />
flatten(NotArry) -&gt;<br />
	[NotArry].<br />
[/erlang]</p>
]]></content:encoded>
			<wfw:commentRss>http://thomaswebb.net/2009/11/03/exercise-3-5-manipulating-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exercise 3-4 Database Handling Using Lists</title>
		<link>http://thomaswebb.net/2009/10/12/exercise-3-4-database-handling-using-lists/</link>
		<comments>http://thomaswebb.net/2009/10/12/exercise-3-4-database-handling-using-lists/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 06:07:25 +0000</pubDate>
		<dc:creator>Thomas J. Webb</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[education]]></category>
		<category><![CDATA[exercise]]></category>

		<guid isPermaLink="false">http://thomaswebb.net/?p=606</guid>
		<description><![CDATA[Here&#8217;s my clumsy attempt at the database handling exercise. Man, I must be a nerd, but I actually like these exercises.. I&#8217;m also enjoying my biology class. Is there something wrong with me? [code lang="erl"]-module(db). -export([new/0, destroy/1, write/3, delete/2, read/2, &#8230; <a href="http://thomaswebb.net/2009/10/12/exercise-3-4-database-handling-using-lists/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s my clumsy attempt at the database handling exercise. Man, I must be a nerd, but I actually like these exercises.. I&#8217;m also enjoying my biology class. Is there something wrong with me?<br />
<span id="more-606"></span></p>
<p>[code lang="erl"]-module(db).<br />
-export([new/0, destroy/1, write/3, delete/2, read/2, match/2]).</p>
<p>new() -&gt;<br />
	[].</p>
<p>destroy(Db) -&gt;<br />
	ok.</p>
<p>write(Key, Element, Db) -&gt;<br />
	Db ++ [{Key, Element}].</p>
<p>delete(Key, [{DbHeadKey, DbHeadElement}|DbTail]) -&gt;<br />
	case DbHeadKey of<br />
		Key -&gt; delete(Key, DbTail);<br />
		_ -&gt; [{DbHeadKey, DbHeadElement}] ++ delete(Key, DbTail)<br />
	end;<br />
delete(Key, []) -&gt;<br />
	[].</p>
<p>read(Key, [{DbHeadKey, DbHeadElement}|DbTail]) -&gt;<br />
	case DbHeadKey of<br />
		Key -&gt; {ok, DbHeadElement};<br />
		_ -&gt; read(Key, DbTail)<br />
	end;<br />
read(Key, []) -&gt;<br />
	{error, instance}.</p>
<p>match(Element, [{DbHeadKey, DbHeadElement}|DbTail]) -&gt;<br />
	case DbHeadElement of<br />
		Element -&gt; [DbHeadKey] ++ match(Element, DbTail);<br />
		_ -&gt; match(Element, DbTail)<br />
	end;<br />
match(Element, []) -&gt;<br />
	[].</p>
<p>[/code]</p>
]]></content:encoded>
			<wfw:commentRss>http://thomaswebb.net/2009/10/12/exercise-3-4-database-handling-using-lists/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Erlang Exercises &#8211; Chapter 3</title>
		<link>http://thomaswebb.net/2009/10/10/erlang-exercises-chapter-3/</link>
		<comments>http://thomaswebb.net/2009/10/10/erlang-exercises-chapter-3/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 17:34:02 +0000</pubDate>
		<dc:creator>Thomas J. Webb</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[evaluating expressions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[practice code]]></category>

		<guid isPermaLink="false">http://thomaswebb.net/?p=594</guid>
		<description><![CDATA[I learn by doing much more than by reading or listening, so as I go through O&#8217;Reilly&#8217;s Erlang Programming, I take the exercises at the end seriously. Generally, when I learn a new programming language, while I read, I&#8217;ll make &#8230; <a href="http://thomaswebb.net/2009/10/10/erlang-exercises-chapter-3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I learn by doing much more than by reading or listening, so as I go through O&#8217;Reilly&#8217;s <em>Erlang Programming</em>, I take the exercises at the end seriously. Generally, when I learn a new programming language, while I read, I&#8217;ll make a program of my own choosing, perhaps something that will be useful to me or that I could sell. However, with Erlang, I don&#8217;t even know where to begin using it yet. I want to get really, really good at it before I even attempt to make anything with it (I think remembering the mistakes I made with my first Ruby on Rails project caused this shift in attitude.. and let&#8217;s not forget the triumphs of reading <em>Mastering Regular Expressions</em> in its entirety).</p>
<p>I will be posting my attempts at the exercises, in case others might find it useful or seasoned Erlang programmers might tell me what I may be doing wrong. I&#8217;ll be spacing out my answers to chapter 3 over the week to keep things interesting. Here are the first three exercises from Chapter 3:<br />
<span id="more-594"></span><br />
[erlang]-module(chapter3).<br />
-export([sum/1, sum/2, even/1, create/1, reverse_create/1]).<br />
-import(erlang).</p>
<p>sum(Num) when Num &gt; 1 -&gt;<br />
	Num + sum(Num &#8211; 1);<br />
sum(Num) when Num == 1 -&gt;<br />
	1.</p>
<p>sum(N,M) when N &lt; M -&gt;<br />
	M + sum(N,M-1);<br />
sum(N,M) when N == M -&gt;<br />
	N;<br />
sum(N,M) when N &gt; M -&gt;<br />
	erlang:throw({&#8216;EXIT&#8217;,{bad_argument}}).</p>
<p>reverse_create(N) when N &gt; 1 -&gt;<br />
	[N|reverse_create(N-1)];<br />
reverse_create(N) when N == 1 -&gt;<br />
	[1].</p>
<p>create(N) when N &gt; 1 -&gt;<br />
	create(N-1) ++ [N];<br />
create(N) when N == 1 -&gt;<br />
	[1].</p>
<p>even(N) when (N &gt; 0) and (N rem 2 == 0) -&gt;<br />
	io:format(&quot;Number:~p~n&quot;,[N]), even(N-1);<br />
even(N) when (N &gt; 0) and (N rem 2 == 1) -&gt;<br />
	even(N-1);<br />
even(N) when N == 0 -&gt;<br />
	done.<br />
[/erlang]</p>
]]></content:encoded>
			<wfw:commentRss>http://thomaswebb.net/2009/10/10/erlang-exercises-chapter-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

