[erlang-questions] Function Declaration Syntax

Torben Hoffmann <>
Sat May 14 13:34:11 CEST 2011


On Fri, May 13, 2011 at 23:44, Jesper Louis Andersen <
> wrote:

> On Fri, May 13, 2011 at 22:23, Joe Armstrong <> wrote:
> > No good reason. Erlang evolved from Prolog and that how it was done in
> > Prolog
> > no deep thought was involved ..
>
> Interestingly, that is also the style of Haskell:
>
> len :: [a] -> Integer
> len [] = 0
> len (x : xs) = 1 + (len xs)
>
> And also of Standard ML:
>
> fun len [] = 0
>  | len (x :: xs) = 1 + (len xs)
>
> ..
>
> My usual rewrite in many cases is to use a case. It almost notates as
> the original idea of a simplification of the syntax:
>
> len(L) ->
>  case L of
>    [] -> 0;
>    [_|R] -> 1 + len(R)
>  end.
>

This is - in my book - a step backwards.
You should haev the spec for the function and then pattern matching in the
function clauses since it gives you:

   1. Overview of what the function does.
   2. 1st level overview of how the function branches out.

This makes debugging so much faster than having to get inside a function
only to meet a case on the parameters as the first thing.

Cheers,
Torben




>
>
> --
> J.
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 
http://www.linkedin.com/in/torbenhoffmann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110514/d02953e8/attachment.html>


More information about the erlang-questions mailing list