Skip to content
Feb 17 11

Guards in Haskell and Erlang

by admin

I have found it  slightly annoying at times that Erlang guards is limited to BIF’s (Built In Function). Haskell let’s you use (almost) any function. Let me first explain why this is. Functional languages try to get rid of side effects as much as possible since they make analyzing code much more difficult because every statement possibly could yield different results every time it is run.

Guards are control structures found in both Erlang and Haskell. They are used like the condition part of an if, such as:

if( condition1 ){
doSomething1();
} else if ( condition2 ){
doSomething2();
}

In this case, doSomething1() will be executed if condition1 is evaluated to true, and doSomething2() will be evaluated if condition1 is false and condition2 is true.

Now, if condition1 contains side effects, it could have some pretty peculiar effects when trying to analyze what the code does. Look at the code below.

x = 1;
if( condition1 ){
return x;
} else if ( condition2 ){
return x+1;
}

In this example, the code will return 1 if condition1 is true, and 2 if condition1 is false and condition2 is true. Right? It certainly feels logical to look at this way, but what happens if x is a global variable (or even privately declared class variable), and condition1 would be a function containing:

bool condition1(){
x = 2;
return true;
}

Now, what would happen? The second clause would now return x+1 = 2+1 = 3. Analyzing the first example becomes very difficult when we have side effects.

Guards in Haskell and Erlang would suffer the same way if side effects were allowed. Now Haskell does not have this problem since no function can contain side effects, unless explicitly marked as such. Erlang on the other hand do allow side effects without the compiler having any way of knowing this. Because this is such a big problem when analyzing, Erlang only allows BIF’s in the guards, that are guaranteed to not contain side effects. For the end user this is a of course a limitation.

Feb 12 11

Erlchat

by admin

Erlchat – the why and what

I will in this tutorial create a simple chat server in Erlang.  Nothing fancy, just a chat room that provides enough challenge. It will host a few simple features such as whispering to a certain user, or use default which is multicasting the message to every in the chat room. It will also be fairly simple to extend my chat server with multiple chat rooms and other features.

The server architecture will host a supervisor process to monitor three child processes; one to accept incoming connections, one for the interim process of holding the client in a waiting room, until a nickname has been chosen, and then one for the chat room.

So why am I doing this?

This is my first project in Erlang. It is a way for me to learn the language. I have got a tremendous amount of help from other bloggers and I hope that maybe someone can benefit from my experiences. I also enjoy writing about things since it really helps understanding the topic. You never really know if you understand something until you have been able to explain it to someone else. At least that is my experience.

Please don’t be to critical of my writing since I am a very experienced writer. That is in it’s own right another reason for my blogging.

Okay, that was a very short introduction, in the next section I will start of with some gen_tcp functions for us to be able to make a telnet client talk to our server.

Until next time…

Jan 12 11

Life as an Erlang programmer, first thoughts

by admin

I have now spent close to two weeks working for Ericsson, these are my first thoughts on Erlang, the language in which I am immersing myself.

Erlang’s main focus is on trying to do concurrency right. It does this by:

  • allowing every logically concurrent task to be run in it’s own process.
  • physically separating each process so not requiring the programmer to coordinate state; a tremendously difficult task to achieve, even in the simpler cases.

The two bullets above show how Erlang tries to deal with concurrency. Erlang being a functional language and all, it also embraces a programming style that is not only easier to read and maintain, but also lending itself to be more analyzable. Getting rid of side effects are an important point here. However, Erlang does not take the pure form that e.g. Haskell takes, being entirely rid of side effects. I have not yet made up my mind about wether I like this or not, but I can conclude that where Haskell can use pretty much any function in it’s guards (since it knows the guard will not contain a side effect), Erlang can’t. I don’t know yet to what extent this will be an annoyance.

Something Erlang do embrace is the philosophy of being fault tolerant rather than fault-free. Pretty much every Erlang book says: “Let it [the processes] Crash”. It is better to get rid of defensive programming altogether (such as looking for a null value in your favorite imperative programming language) and letting the process crash early so you can find the bug more easily, and just restart the process so that not the entire system goes down, rather than trying to handle every possible fault in your program and ending up with most of your code being hard to read. Something that in turn is much more likely to introduce serious faults in the your system.

So far it seems that Erlang is doing concurrency right. I have already made a few serious server applications with very little code.

A last interesting side note, I still haven’t met a single Erlang programmer that does not like the language. I find that pretty unique.

Jun 18 10

Basic Neural Networks

by admin

This is the first part of my series on Neural Networks. I am a newcomer to the subject and the intention of this introduction is to help others to grasp the basic idea, and to be able to implement a small project using neural networks. The introduction will show code made in C#, but the concept will of course easily translate to other languages.

I am not going to write about the biological analogy of the subject but rather focus on the mathematical modelling of it. Before I do that however, I wish to start off by discussing why we would want to build an artificial neural network.

What is it that we wish to achieve? We all have different motives but the current driving force to me has been whether or not a neural network can be used to earn money passively. Can it be trained to make good decisions in the stock market? Or make money off betting in harness racing. Or perhaps can it play poker by itself, watching others play to “see” their strategies and entering the competition when it believes it can win.

Other interesting projects could be for making a website of predicting house prices in your city, or why not simply try to train it to beat your ego in a good ol’ game of connect four.?

First of all, neural networks is nothing new. Many people have tried to make insane money by computerized aid, some may have succeeded, but many have failed. Trying to find patterns in the stock market is not easy, and any finding made by a neural network is most likely already accounted for. Making money off harness betting might work better, but you will probably not be alone in this area either. Plus, in Sweden the biggest company – ATG – is taking approximately twenty percent. This means that whatever system you are able to develop, you need to be at least twenty percent better than a random guess to even start making a dime in profit. A poker bot is feasible, but requires several layers of intelligence to work by itself. It has to be able to understand the visual images of the cards on the screen to even begin playing since there are no official API’s for interacting with the poker sites. After all, it is not wanted by the casinos, nor the players since your bot will make heavily use of statistics which they can not.

Starting with the basics

Humans are fantastic at finding patterns in a visual picture. Take for example the picture below. It would be easy to draw a line to separate the blue class from the red class.

Diagram 1

Our next step in this tutorial will be to make a neural network make this classification for us.