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:

Comments

5 Responses to “Comparing Erlang and Prolog”

  1. Henry on December 5th, 2007 5:30 am

    Fun reading your blog. Did you come up with “Nuclear powered Nutcracker” as a response to the Swiss-Army ChainSaw thing?

  2. Daniel on December 5th, 2007 5:55 am

    No. But that’s cool, thanks for the ref.

    I heard the phrase on Mauricio Fernandez’ Eigenclass blog (hope I spelled that right). He described some huge library as a NPN and it just tickled me. Don’t know where he got it from.

  3. Alain O'Dea on June 29th, 2008 6:52 pm

    This is a very late reply I know, but you have drawn out a key issue for anyone - like me - who had bad experiences with Prolog in university or elsewhere previous to encountering Erlang. I thought I saw Prolog when I first looked at Erlang and it took me months of convincing by my friend Michael before I considered learning it for real after that. My friend Andy remarked the same thing when I showed him some Erlang code and it also turned him off. At the time I wasn’t able to effectively explain what Erlang does differently that makes it easier than Prolog. Now I know. Thank you Daniel.

  4. Daniel on July 10th, 2008 9:12 am

    Interesting. I haven’t got any bad experiences with Prolog, unless you call my entire PhD a bad experience! So for me the similarity was a plus point not a bad one.

  5. Mark on July 22nd, 2008 10:17 pm

    Yes, but with Prolog you can say:
    list_length(List, 0).
    and Prolog will give you solutions for List. Namely [].

    So your Prolog code does twice as much as the Erlang code.

    It may not be so impressive there, but consider the Prolog implementation of append which appends 2 lists together:

    append([],Ys,Ys).
    append([X|Xs],Ys,[X|Zs]) :-append(Xs,Ys,Zs).

    Not only does that code append any two lists to form a third. In Prolog if supplied only with third value, they will enumerate all possible combinations of the first two lists that could be appended to form the third.

    Try that in Erlang.

Leave a Reply




  • About

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