Erlang Programming Exercise: 3-2
Exercise 3-2 is also 2 questions:
Write a function that returns a list of the format [1,2,..,N-1,N].
Write a function that returns a list of the format [N, N-1,..,2,1]
I've created 4 functions, create/1, create/2, reverse_create/1, and reverse_create/2. The /1 functions set up the arguments for the /2 worker functions to loop on.
-module(threetwo). -export([create/1, reverse_create/1]). create(1, [1|_T]=List) -> List; create(Num, List) -> Next = Num-1, create(Next, [Next|List]). create(Num) when Num > 0 -> create(Num, [Num]). reverse_create_list(Num, [Num|_T]=List) -> List; reverse_create_list(Num, [H|_T]=List) -> reverse_create_list(Num, [H+1|List]). reverse_create(Num) when Num > 0 -> reverse_create_list(Num, [1]).
An example run:
4> threetwo:create(10). [1,2,3,4,5,6,7,8,9,10] 5> threetwo:reverse_create(10). [10,9,8,7,6,5,4,3,2,1]
The key in is to pass your target number along and to build the list from right to left. In each iteration we peak at the head of the list, modify that value and then prepend the result to the head of the full list.
[H|_T]=List
H is the first item in the list.
List is the full List.
Cheers,
Ben
Erlang Programming Exercise: 3-1
I'm posting the exercises starting with chapter 3. The previous chapters have you work in the shell and don't require much file work. Exercise 3-1 is:
Write a function sum/1 which, given a positive integer N, will return the sum of all the integers between 1 and N.
And the second part:
Write a function sum/2 which, given two integers N and M, where N =< M, will return the sum of the interval between N and M. If N > M, you want your process to terminate abnormally.
Here is my solution: threeone.erl
-module(threeone). -export([sum/1, sum/2]). sum(1) -> 1; sum(Number) -> Number + sum(Number - 1). sum(Lower, Lower) -> Lower; sum(Lower, Upper) when Lower =< Upper -> Upper + sum(Lower, Upper-1).
If you give sum/1 a number less than 1, you'll infinite loop.
The above solution doesn't use Erlang's tail call optimizations. If you give them a very large set of numbers to add up, they keep eating up memory. I wanted to see what would happen when it ran out of memory or stack space, so I told it to sum up a billion. After my system started swapping and started to crawl I killed the process. Maybe next time.
Here is the reworked version that takes advantage of tail call optimization and can sum up numbers from 1 to a billion without making my system swap. The beam.smp process stays around 10mb of memory.
-module(threeone_tco). -export([sum/1, sum/2]). sum_range(Sum, Lower, Lower) -> Sum + Lower; sum_range(Sum, Lower, Upper) -> sum_range(Sum + Upper, Lower, Upper-1). sum(Number) when Number > 0 -> sum_range(0, 1, Number). sum(Lower, Upper) when Lower =< Upper -> sum_range(0, Lower, Upper).
The big change comes from passing the Sum as an argument to the next function call. To use TCO (Tail Call Optimization) you want to leave no work to be done in your function, and the last thing your function does, should be calling itself. Previously, the function would still have to add two numbers after it's recursion finished.
Cheers,
-Halzy
Learning Erlang
It all started way back when I was doing concurrent programming in Java for the MMORPG when a buddy of mine pointed out CouchDB. It's a neat project, but was written in some strange language called Erlang. I started to look more into Erlang and found that the language was designed to addressed some of the more complicated aspects of concurrent programming. This appealed to me because those were my current pain points with the game. When do we lock around what data, which threads are changing what, do I need to use volatile for this variable too. It's a lot to think about, it's hard to get right, and Erlang solves those problems for you if you're willing to think differently.
I started by reading Programming Erlang by Joe Armstrong and now I'm reading through Erlang Programming by Francesco Cesarini and Simon Thompson. I'm enjoying the second book more than the first one, but this could be a side effect of being more comfortable with Erlang because I read the first book. I'm so taken with this bizarre language that I've decided to work through all of the exercises in Erlang Programming. I'll be posting my solutions and hopefully, by the time I get through all of them I'll feel comfortable enough with Erlang to build something with it.
Cheers,
- Halzy
Subversion loves Araxis Merge (or, How I finally configured subversion to use Araxis Merge in a not so stupid way.)
Araxis suggests modifying subversions diff-cmd and diff3-cmd settings to get it to use Araxis Merge instead of the defaults. Here is a snippet from their docs. (You can skip it, it's just here to make my post look bigger.):
Open your ‘SVN configuration area’ configuration file in a text editor. The default location for this file is ~/.subversion.
Uncomment the line that specifies the
diff-cmdand set its value to the path of the Merge araxissvndiff executable that you have installed on your machine:diff-cmd = /Users/<userid>/bin/araxissvndiffNote: in the above path, replace /Users/<userid>/bin/araxissvndiff with the full path to the Merge Merge araxissvndiff command-line utility that you have installed on your machine.
Also uncomment the line that specifies the
diff-cmd3and set its value to the path of the Merge araxissvndiff3 executable that you have installed on your machine:diff3-cmd = /Users/<userid>/bin/araxissvndiff3Note: in the above path, replace /Users/<userid>/bin/araxissvndiff3 with the full path to the Merge Merge araxissvndiff3 command-line utility that you have installed on your machine.
By doing what they say, you tell svn to use Araxis Merge every time it needs to merge a file. The problem with this is that svn will open Araxis Merge to make every merge. When you have a couple hundred files, you want svn to merge everything it can without opening Araxis Merge, and to only open it when there is a conflict. There is a way to do this, but it is not obvious.
Shock & Awe @ Google

2009.10.7 Google Homepage
Today Googles homepage has a nice look to it, so much different that I was shocked when I arrived. The navigation and footer links faded in when you move your mouse. Very nice Google, I applaud you.
I’ll be late…
Dearest Love of My Life,
I know how much it can negatively affect life at home when I'm unable to arrive at a reasonable hour. Please understand my regrets that I will be late this evening..
Quick math game.
Yesterday I made a quick math game to help with some math studies. You have to click on the game, then type in the answer and hit enter (or click GO!).

