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.
Comments
5 Responses to “Comparing Erlang and Prolog”
Leave a Reply
Fun reading your blog. Did you come up with “Nuclear powered Nutcracker” as a response to the Swiss-Army ChainSaw thing?
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.
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.
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.
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.