Tuesday, October 31, 2006

Serializing Erlang Tuples For Network Transmission

I have been playing around a bit with setting up an SSL client/server connection between nodes using Erlang. As you have guessed, the SSL module in Erlang expects you to send and receive binary data. You may also know, that the typical way of sending data between nodes in Erlang is by using the '!' operator. For example:

Pid ! {data, SomeList}

Now since the SSL uses sockets to send data, we need to convert from the tuple to a binary form. Luckily, Erlang provides a facility to do this!

On the encoding side all you need to do is use the Erlang term_to_binary Built In Function(BIF):

Data = term_to_binary({data, SomeList})

Then we send it away.

ssl:send(CSock, Data)

Now we receive the data:

{ok, DataRecv} = ssl:recv(SSock, 0)

Then we do the decoding. ssl:send automaticlly converts the binary stream to a list when sending, so we need to convert it back, then go from binary_to_term to reverse our initial encoding.


That's all there is to it!

Monday, October 30, 2006

ECal First Beta Release!

I have made my first release of ECal! It is a personal calendar and event organizer written in Erlang. Right now it is just meant for the command line, but later on I will probably make a nice little GUI for it with wxErlang. The program really is nice and handy for those of us who live and breathe on the command line and want a simple way to stay organized and remember meetings.

Weird sites of Chicago

Madeline and I were shooting pictures behind the Oriental Theater, where a famous fire in 1903 killed over 600 people back when it was known as the Iroquois Theater. They used the alley as a temporary morgue to pile all the bodies.

Anyways, as we were shooting, out of nowhere a "lady bum" walks into the alley, pulls down her pants, and does her business.

Click here to see the picture.

Friday, October 27, 2006

Visualizing problems in Erlang

Ever since I started learning Erlang almost 10 months ago, I have always just had this deep down unexplainable gut feeling that Erlang is neat. Erlang just feels so right. The flow from idea to implementation in Erlang is so smooth, sometimes I second guess myself, "can it really be this easy?!"

Last night I couldn't sleep, so naturally I stayed in bed thinking about Erlang, and I think I might have figured out why Erlang is so nice: visualization.

Let's just take an example. Let's say I tell you to write Quick Sort in C.

The first thing you would probably do is look up the algorithm which is:

function quicksort(q)
var list less, pivotList, greater
if length(q) = 1
return q
select a pivot value pivot from q
for each x in q except the pivot element
if x < pivot then add x to less
if x = pivot then add x to greater
add pivot to pivotList
return concatenate(quicksort(less), pivotList, quicksort(greater))

To code the algorithm, you need to start thinking about the implementation details, such as how you will manage the pivot list, what type of values will your quick sort support, etc. It is very difficult to go from the algorithm directly to C, there are too many C things to worry about first.

void quickSort(int numbers[], int array_size)
q_sort(numbers, 0, array_size - 1);
void q_sort(int numbers[], int left, int right)
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
while ((numbers[right] >= pivot) && (left < right))
if (left != right)
numbers[left] = numbers[right];
while ((numbers[left] <= pivot) && (left < right))
if (left != right)
numbers[right] = numbers[left];
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);

Now let's say I tell you to think about writing a Merge Sort in Erlang. You instantly realize this problem is dead easy using a list a few recursion calls and Erlang's powerful list operators. There are no memory issues to think about, no type checking to worry about, no extra temporary values to handle, no having to "translate" your programs calls to fit the algorithm. With Erlang you can actually imagine in your head, having a long list of things, doing a few list operations, then using recursion and being done.

sort([Pivot|T]) ->
sort([ X || X <- T, X < Pivot]) ++
[Pivot] ++
sort([ X || X <- T, X >= Pivot]);
sort([]) -> [].

While I admit it takes a functional mindset to get used to seeing things this way, once you do have it, it is an almost instantaneous process of going from algorithm to code. Being able to visualize both the algorithm and the Erlang code together at the same time without much difficulty and implementing them immediately makes programming a lot more enjoyable and fulfilling. Gone are the days of tedious details and mindless translations.


Monday, October 23, 2006

Goodbye Gentoo

I never thought I would say this, but so long Gentoo. I always thought I would be able to stick with Gentoo for most of my life, as it was an easy system to maintain, with a wealth of packages.

Unfortunately Gentoo has been going in a downward spiral for a while now, and finally the final straw has broken the camel's back.

These crazy kids just hard mask anything for fun it seems these days. XMMS, whether "buggy" or not, still works just fine for me, alot better than any of the other current music players out there. I see no reason to hard mask it. Who cares if it doesn't have a maintainer? It still works on x86 computers just fine. I know this gentoo person claims XMMS to be broken, but it works just fine for me. Maybe these days "broken" means they don't release a new version every two weeks.

Anyways, all this rambling is for nothing now, I am officially switching to Ututo.

Saturday, October 21, 2006

How to compile wxErlang on Linux

wxErlang is an interface for Erlang to the popular wxWidgets GUI library. Combining Erlang and wxWidgets creates an unbeatable combination for GUI writing. First of all we inherit the power of having a true concurrent functional programming with Erlang and add to that the fact that we can write a program once but have it use the native GUI for Linux, MacOSX, Windows, etc. This means if you execute the program in Linux, you will get a nice Gnome look, if you execute in MacOSX, you will net the nice Mac look, all with one single program.

wxErlang is still in an early version, but alot of documentation is provided how to use it, the only trouble one might face is actually compiling the thing.

Before you compile, make sure to have the following programs installed on your computer:
1. Erlang
2. wxGTK
3. Doxygen

Now download wxErlang and let's start.

1. tar zxvf wxe-*.tar.gz
2. cd wxe
3. Edit config.mk and change the following values:
b. ERLI_VSN=`echo /usr/lib/erlang/lib/erl_interface-* | awk 'BEGIN{ FS="erl_interface-" } { print $2 }'`
c. WXCONFIG=`which wx-config`
4. cd gen
5. make xml
6. cd ..
7. make

Now if that worked, you should be able to try the example program:
1. cd etop
2. make
3. erl -pa ../ebin -run erltop start

If that worked, congratulations! You are now well on your way to learning more and writing some real applications.

Wednesday, October 11, 2006

Why you should hate everything

So, why should you hate everything?

Because you will always be right.. eventually!

We live in a world that has two major foundations(among others). The first being it is always easier to disprove something than to prove something. Second, it is better to be the person who was wrong at first, then eventually proven correct, then the person who was correct initially, then proven wrong later.

So, if you want to always be "right", you must hate everything.

Some examples.

1. Java. When Java first came out, it was all hyped up to be the next great thing, everyone was excited, etc. I myself, found it be very cumbersome, extremely boring, and of little use to me, therefore I hated it. I paid the price at first, I was ridiculed and scolded about all the benefits of Java and so on, but now finally, the tables have turned, and people see what Java really is: an excuse to kill yourself.

2. Iraq War. When the war first started, Bush had the highest approval ratings and public support was almost a complete majority. Friends and family of mine even supported the cause. I again, was against it. I just couldn't synthesize how a struggling impoverished nation would be able to do much harm to anyone else besides itself. Well, again the tides have turn and the support for war is disappearing daily and people finally see what the war really is: an excuse to kill yourself.

3. Science. Every day new discoveries are made that prove old discoveries to be untrue or not useful, from the orbit of planets to atomic bombs, we have been wrong several times in the past to get to where we are today. People now realize what scientific exploration and research really is: an excuse to kill yourself.

I could go on and on with many other examples, but the point sticks, almost everything you know will be hated one way or another, it is just natural progression. There is a 99.9% chance that if you hate something now in the face of the majority adverse opinion, you will eventually be the one who is proven correct, and all of them incorrect, how great is that!

Some things you should immediately start hating.

1. String theory
2. Linux
3. People

Tuesday, October 03, 2006

Oct 3rd: Day Against DRM!

Today is the official "Day Against DRM"!

If you are in Chicago today, we could sure use your help! For information, please goto: http://www.chicagolug.org/Drm

Some things that we could really use help with are:

1. People to wear the Biohazard suits
2. People to hand out materials and educate
3. People to take pictures, blog, create editorial materials, etc.

It is important that we not only get the word out, but that we document this event. If you take any pictures, please upload them to flickr, with the following tags: defectivebydesign, drm, apple, chicago, ipod, itunes, nodrm, antidrm. Also, feel free to add them to the DefectiveByDesign group.

If you can't make it, you can always print out the following materials:
Anti-DRM Sign
Anti-DRM Literature
and hand them out to everyone you know!

Most of all, have a great day, and just try to educate people. Once people know what DRM is, they most surely won't like it.