Ulf Wiger ulf.wiger@REDACTED
Fri Oct 24 09:52:36 CEST 2003

On Fri, 24 Oct 2003 02:03:46 +0200, Joachim Durchholz 
<joachim.durchholz@REDACTED> wrote:

> Joe Armstrong wrote:
>> When you read code like this my brain automatically wonders --
>> wonder why there is no base case for the recursion - I wonder if the 
>> programmer
>> forgot it (actually a type system will go though the same kind of
>> reasoning as well, so it's not a bad idea to be explicit).
>>  If you write it like this:
>> 	last([_|T]) -> last(T);
>> 	last([H])   -> H;
>> 	last([])    -> exit(emptyListsDoNotHaveLastElementYourProgramIsNuts).
>>  The you will at least know that the programmer had thought about it.
> Besides, Haskell does it that way, too, so it can't be entirely nuts to 
> have an idiom like this :-)

Personally, I prefer the (working) version:

last([]) -> H;
last([_|T]) -> last(T).

And if it's not obvious from the overall programming style in the
module that I'm describing the correct cases only and relying on the
standard error, I would prefer to insert a comment:

%% This function will crash if called with anything but a non-empty list
last([]) -> H;
last([_|T]) -> last(T).

In my experience, from working in a huge software system, the standard
crash reports are usually good enough, and custom exit reasons usually
only serve to confuse the audience. There is a lot to be said about
consistency in large projects...  (:

Ulf Wiger

More information about the erlang-questions mailing list