Comparing Erlang and Prolog

In the back of my mind, I have for a little while known two things:

  1. Prolog is no language for a man to use.
  2. I should probably check out this Erlang thing.

So today I have a look at this getting-started-with-erlang tutorial, and lo! Perhaps my Prolog skills are not so useless after all.


% Erlang
-module(tut4).
-export([list_length/1]).

% simple function to return list length
list_length([]) ->
    0;
list_length([_F|R]) ->
    1 + list_length(R).


% Prolog
:- module(tut4).
:- export list_length/2.

% simple predicate to return list length
list_length([], 0).
list_length([_F|R], L):-
    L is 1 + list_length(R).

Similarities so far:

Differences:

Fix for Ruby Gnome GtkMozEmbed

After upgrading to Feisty, any attempt to use the Ruby-Gnome2 Gtkmozembed bindings fails with:


$ ruby ruby-gecko.rb
ruby-gecko.rb:203: [BUG] Segmentation fault
ruby 1.8.5 (2006-08-25) [i486-linux]

Aborted (core dumped)

After an hour of searching online, I discover that (a) the problem isn’t limited to Ruby, and (b) the fix is quite simple:


$ export LD_LIBRARY_PATH=/usr/lib/firefox

It’s things like this that make you crazy.

Null Objects

Tom Locke and I have similar proclivities. He says:


class Object
  def _?()
    self
  end
end

class NilClass
  def _?()
    SafeNil.new
  end
end

class SafeNil
  def method_missing(*args, &b)
    nil.send(*args, &b) rescue nil
  end
end

I say:


class Object
  def null
    n = nil
    def n.method_missing(*args)
      null
    end
    n
  end
end

The difference:


string_or_nil._?.length        # Tom's
(string_or_nil||null).length   # mine

I like being able to go more than one method deep,


(string_or_nil||null).length * 4

But that might not be to your taste.

Short is Good

Here’s a little Ruby snippet that I enjoy:


module Kernel
  alias fn lambda
end

Example usage:


lambda {a_call}.should raise_error
fn {a_call}.should raise_error

Courtesy of Arc.

  • About

    Daniel Lucraft, London, UK. Interested in Ruby, Prolog and software.