A programmer’s favourite language

I’ve discovered something recently. Most programmers have a favourite programming language. No, that’s not it, there’s more to my discovery. Whenever those programmers have an idea they open a text editor and start coding and/or they start playing with a REPL, if they are lucky to like a programming language that provides one.

You watch them code and they do it fast, with little errors. They don’t program fast because it’s a good language, they program fast because they know it, and they know it because they use it often, and they do that because they like it. That was the first part of my discovery. Continue reading “A programmer’s favourite language”

Programming Python, Part I

Linux Journal has just published an article I wrote title Programming Python, Part I. It is obviously an honour to have an article on such an important publication.

Thank you Ramiro for letting me know it was already published and write about it. Continue reading “Programming Python, Part I”

SVGs in .Net using Cairo and Gtk+ (and C#)

If I was using C it would have been simply a couple of calls to librsvg, but on C# things got a bit more ugly because Rsvg, the wrapper around librsvg is not finished. And other bindings are also missing. Just getting a Cairo context out of a Gtk.DrawingArea was not as simple as I would have liked it to be (I describe how to do it in a previous post, but I’ll do it again here).

Continue reading “SVGs in .Net using Cairo and Gtk+ (and C#)”

Trying to find exceptions in Haskell

In my quest to re-learn Haskell I eventually thought: “OK, let’s see how an exception looks like”. Starting my favorite interactive Haskell implementation:

   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.4.1, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base-1.0 ... linking ... done.
Prelude>

OK. Let’s generate an exception now, a division by zero for example (something basic): Continue reading “Trying to find exceptions in Haskell”

Django… awesome

I have an idea for a web application that might enjoy moderate success. And from time to time I try to develop it and it would be already done if it wasn’t that web developing is so painful (reading PLAI and trying to make my own Lisp to conquer the word is far more fun and entretaining… oh damn, I shouldn’t told you my plan… oh well).

My favourite framework so far is (and continues to be, more on that latter): UnCommon Web (UCW). But after reading an article titled Framework Performance (or Django vs. Rails vs. Symfony: Django is fastest on digg) I said: “Hey, let’s give this Django thing a try”.

Continue reading “Django… awesome”

Coders, please, make libraries!

I am sick and tired of pieces of code arbitrary put together when they do very different things, so I shout:
Coders, please, make libraries!
Continue reading “Coders, please, make libraries!”

Erlang, the language for network programming Issue 2: binary pattern matching

Much is being said about the excellent capabilities of Erlang to write distributed fault-tolerant programs, but little has been said about how easy and fun it is to write servers (those programs at the other end of the line) with it. And by easy I don’t just mean that you can put up a web server in two lines of code and hope it’ll work, I mean it’ll be easy to built robust servers.

One example of this is ejabberd, a free Jabber server.

I’ll start this second part, the one with real networking programming, with a bet. Think about the IPv4 protocol, its header is like this:

0                   1                   2                   30
  1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version|  IHL  |Type of Service|          Total Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Identification        |Flags|      Fragment Offset    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Time to Live |    Protocol   |         Header Checksum       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Source Address                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Destination Address                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

you can check RFC791, page 11 for more information. At a glance, the first 4 bits are the version, the next 4 bits the IHL (Internet Header Length), then we have a whole byte, 8 bits, of Type of Service. The next two bytes are the total length and I am already tired of it, you get the picture right?

Pick whatever language you want (except Erlang, that’s mine now, but it can be yours latter) and think about how many lines of code would take you to parse that beast, the IP header. Think about how much time it takes you to write those lines, and test them.

Done? come on! really think about it, otherwise the game is boring. Close your eyes, picture the lines of code. If you can’t, go and write some pseudo-code similar to your favorite language to do the parsing. Done? OK.

Here’s my bet: I bet that I can do it, in Erlang, in far less lines than you! I bet you that I can code it so fast that I’d be finished of writing the code to parse the whole header before you finish the code to parse the first line. And while you are testing I’ll go to the beach because I’ll just trust my code to run without problems.
Continue reading “Erlang, the language for network programming Issue 2: binary pattern matching”

Erlang, the language for network programming Issue 1: pattern matching

Much is being said about the excellent capabilities of Erlang to write distributed fault-tolerant programs, but little has been said about how easy and fun it is to write servers (those programs at the other end of the line) with it. And by easy I don’t just mean that you can put up a web server in two lines of code and hope it’ll work, I mean it’ll be easy to built robust servers.

One example of this is ejabberd, a free Jabber server.

One of the Erlang features that let us write servers is its binary pattern matching. But to understand binary pattern matching first you have to understand pattern matching.

Let’s start with a classic of functional programming: factorial. This is factorial in Erlang

fac(N) ->
   if N == 0 -> 1;
      true -> N * fac(N - 1)
   end.

Continue reading “Erlang, the language for network programming Issue 1: pattern matching”