[erlang-questions] Erlang again!

Richard O'Keefe ok@REDACTED
Fri Jul 23 01:51:20 CEST 2010


On Jul 23, 2010, at 3:52 AM, Corrado Santoro wrote:
> Try this:
> 
> sub ([],[]) -> [];
> sub (_, []) -> [];
> sub ([], _) -> [];
> sub ([H1 | T1], [H2 | T2]) -> [ H2 - H1 | sub (T1, T2)].

Don't.  Why write three clauses when one will do?

sub([X|Xs], [Y|Ys]) -> [X-Y | sub(Xs, Ys)];
sub(_,      _     ) -> [].

Then think about whether you really want sub(boojum, []) to answer
[], or whether you really want sub([2,3], [1|missing]) to return
[1], and make the second clause

sub([], []) -> [].
> 
> not tail-recursive... however :-)

Tail recursion is important, but body recursion is not the
Dark Side of the Source.

"FIRST make it right,
 THEN (try to) make it fast(er)."



More information about the erlang-questions mailing list