danlucraft /blog

Comparing Erlang and Prolog

August 2007 • Daniel Lucraft

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:

% 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.
blog comments powered by Disqus