[erlang-questions] DRY principle and the syntax inconsistency in fun vs. vanilla functions

Michael Turner michael.eugene.turner@REDACTED
Wed May 18 15:29:39 CEST 2011


It's just been pointed out to me (in a possibly *unintentionally* private
e-mail) that this topic was already taken up on a recent thread:

  http://erlang.org/pipermail/erlang-questions/2011-May/058323.html

<http://erlang.org/pipermail/erlang-questions/2011-May/058323.html>I don't
really understand one of the objections: that newbies would find it strange.
When I started learning Erlang, even with some experience in lisp and
prolog, I found *lots* of things strange.

And yes, I understand that function clauses were borrowed from prolog. But
an influence is not a stricture, especially when that influence is only
partial, as it is here -- and maybe prolog wasn't doing it right either?

The better way to evaluate newbie-friendliness of the proposed syntactic
change is to do it *scientifically*: teach some newbies both ways of writing
function clauses, then see how well they do in timed code-comprehension
tests where the only difference is the proposed change in syntax. I predict
they'd actually have an easier time with what I propose, or at least do just
as well.

-michael

On Wed, May 18, 2011 at 6:16 PM, Michael Turner <
michael.eugene.turner@REDACTED> wrote:

> I can say
>
>    fun (1)->2;
>         (2)->1
>    end
>
> but, oddly, I can't define a named function in the analogous way, e.g.:
>
>    factorial
>      (1) -> 1;
>      (N) -> N*factorial(N-1).
>
> gives me a syntax error. I find the latter more readable than
>
>    factorial(1) -> 1;
>    factorial(2) -> N*fact(N-1).
>
> It's also less to type and to read, which is consistent with the DRY
> principle ("Don't Repeat Yourself").  And it lends itself to reading a
> function definition as a set of cases. I think for Erlang newbies, it should
> therefore would be preferred: it helps underscore the pattern-matching style
> of Erlang function invocation.
>
> It also looks a *little* bit more like the mathematical convention for
> defining these sorts of functions, where you have "f(x) = ", centered
> vertically to the left of a big left "{" that (left-)encloses the list of
> expression/parameter-condition pairs in a two-column format, e.g.,
>
>  http://cnx.org/content/m29517/latest/Picture%2047.png
>
> So there's a (weak) argument from the Principle of Least Surprise here as
> well.
>
> It seems to me that, if anything, this requires only a *simplification* of
> the Erlang parser. That leaves only one obvious objection: would any
> existing code break if Erlang syntax were altered to allow this?
>
> -michael turner
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110518/360a6767/attachment.htm>


More information about the erlang-questions mailing list