[erlang-questions] Function Declaration Syntax

Torben Hoffmann torben.lehoff@REDACTED
Sat May 14 13:34:11 CEST 2011


On Fri, May 13, 2011 at 23:44, Jesper Louis Andersen <
jesper.louis.andersen@REDACTED> wrote:

> On Fri, May 13, 2011 at 22:23, Joe Armstrong <erlang@REDACTED> 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
> erlang-questions@REDACTED
> 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.htm>


More information about the erlang-questions mailing list