Comparing Erlang and Prolog
In the back of my mind, I have for a little while known two things:
- Prolog is no language for a man to use.
- 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:
- Modules - one per file.
- Module/export directives very similar.
- Commas go between statements, full stops end code blocks.
- Comments use the same symbol.
- Multiple function clauses with argument matching.
- Lists work in same manner: [head|tail].
- Variables begin with capitals.
- Atoms begin with lowercase, (atoms are the same as Ruby’s symbols).
- Singleton variables generate compiler warnings, unless they are prefixed with an underscore (_F above)
- Both scope modules with a colon.
- To quit the shell you call ‘halt’.
Differences:
- Erlang functions have return values, rather than the Prolog concept of success and failure, and passing return values out through arguments.
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.