Function Declaration Syntax

classic Classic list List threaded Threaded
15 messages Options
moi
Reply | Threaded
Open this post in threaded view
|

Function Declaration Syntax

moi
Hi all,

I come to a question on function declaration syntax: why does function
name appear in each clause?

Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
    Body1;
...;
Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
    BodyK.

It does not provide any information, except that one might make silly mistakes.

If omitting it, a new problem is how to indent clauses properly and
beautifully.

Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
    Body1;
    (PatternK1,...,PatternKN) [when GuardSeqK] ->
    BodyK.

--
Best Regards,
Justus
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Attila Rajmund Nohl
2011/5/13, Justus <[hidden email]>:

> Hi all,
>
> I come to a question on function declaration syntax: why does function
> name appear in each clause?
>
> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>     Body1;
> ...;
> Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
>     BodyK.
>
> It does not provide any information, except that one might make silly
> mistakes.
>
> If omitting it, a new problem is how to indent clauses properly and
> beautifully.

And it would also make tools like ctags less useful...
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

dmercer
In reply to this post by moi
On Friday, May 13, 2011, Justus wrote:

> I come to a question on function declaration syntax: why does function
> name appear in each clause?
>
> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>     Body1;
> ...;
> Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
>     BodyK.
>
> It does not provide any information, except that one might make silly
> mistakes.

Syntactic salt?  http://en.wikipedia.org/wiki/Syntactic_salt#Syntactic_salt

Cheers,

DBM

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Alexander Krasnukhin
In reply to this post by moi
Think about it not as whole function but as set of clauses. It make sense this way.

On Fri, May 13, 2011 at 11:43 AM, Justus <[hidden email]> wrote:
Hi all,

I come to a question on function declaration syntax: why does function
name appear in each clause?

Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
   Body1;
...;
Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
   BodyK.

It does not provide any information, except that one might make silly mistakes.

If omitting it, a new problem is how to indent clauses properly and
beautifully.

Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
   Body1;
   (PatternK1,...,PatternKN) [when GuardSeqK] ->
   BodyK.

--
Best Regards,
Justus
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



--
Regards,
Alexander


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Attila Rajmund Nohl
The (usual) problem is that this is not consistent. You have to write
the function name for different function clauses, but must not write
the 'fun' for different clauses in anonymous functions. I would prefer
the 'fun' anu anonymous functions too.

2011/5/13, Alexander Krasnukhin <[hidden email]>:

> Think about it not as whole function but as set of clauses. It make sense
> this way.
>
> On Fri, May 13, 2011 at 11:43 AM, Justus <[hidden email]> wrote:
>
>> Hi all,
>>
>> I come to a question on function declaration syntax: why does function
>> name appear in each clause?
>>
>> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>>    Body1;
>> ...;
>> Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
>>    BodyK.
>>
>> It does not provide any information, except that one might make silly
>> mistakes.
>>
>> If omitting it, a new problem is how to indent clauses properly and
>> beautifully.
>>
>> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>>    Body1;
>>    (PatternK1,...,PatternKN) [when GuardSeqK] ->
>>    BodyK.
>>
>> --
>> Best Regards,
>> Justus
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> --
> Regards,
> Alexander
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Joe Armstrong-2
In reply to this post by moi
No good reason. Erlang evolved from Prolog and that how it was done in Prolog
no deep thought was involved ..

/Joe


On Fri, May 13, 2011 at 11:43 AM, Justus <[hidden email]> wrote:
Hi all,

I come to a question on function declaration syntax: why does function
name appear in each clause?

Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
   Body1;
...;
Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
   BodyK.

It does not provide any information, except that one might make silly mistakes.

If omitting it, a new problem is how to indent clauses properly and
beautifully.

Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
   Body1;
   (PatternK1,...,PatternKN) [when GuardSeqK] ->
   BodyK.

--
Best Regards,
Justus
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Jesper Louis Andersen-2
On Fri, May 13, 2011 at 22:23, Joe Armstrong <[hidden email]> 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.


--
J.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Steve Davis
In reply to this post by moi
I like it the way it is. I never questioned it.

Now that it's been asked, I would guess that your suggestion if
implemented would attract many complaints from new users about weird
syntax. It's also harder to scan to see the full intention of the
pattern matching "entry points" to the function.

So I'd say -- if it works...(which it does...) there's no need to
"fix" it. Particularly since the change may be less effective than the
existing status here.

My 2c

/s
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Torben Hoffmann
In reply to this post by Jesper Louis Andersen-2


On Fri, May 13, 2011 at 23:44, Jesper Louis Andersen <[hidden email]> wrote:
On Fri, May 13, 2011 at 22:23, Joe Armstrong <[hidden email]> 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



--
http://www.linkedin.com/in/torbenhoffmann

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Richard A. O'Keefe-2
In reply to this post by moi

On 13/05/2011, at 9:43 PM, Justus wrote:

> Hi all,
>
> I come to a question on function declaration syntax: why does function
> name appear in each clause?
>
> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>    Body1;
> ...;
> Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
>    BodyK.

Why not?
That's how you do do it in mathematics when defining a recurrence.
That's how it's done in Haskell.
That's how it's done in SML.
That's how it's done in every functional language I know that _has_
multiple clauses (so, not in Lisp).
>
> It does not provide any information, except that one might make silly mistakes.

Which do you suppose human beings rely on, when reading the code,
to tell whether clauses belong together?

 - the function name at the beginning of the line where they can see it
 - a distinction between "." and ";" at differently placed ends of lines?

> If omitting it, a new problem is how to indent clauses properly and
> beautifully.
>
> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>    Body1;
>    (PatternK1,...,PatternKN) [when GuardSeqK] ->
>    BodyK.

Don't.  Use 'case' instead.

For what it's worth, I write

        fun (....) when ... ->
            ...
          ; (....) when ... ->
            ...
        end.

If you don't have a name to make the alignment obvious, you desperately
need the semicolon out the front.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Richard A. O'Keefe-2
In reply to this post by Attila Rajmund Nohl

On 14/05/2011, at 3:01 AM, Attila Rajmund Nohl wrote:

> The (usual) problem is that this is not consistent. You have to write
> the function name for different function clauses, but must not write
> the 'fun' for different clauses in anonymous functions. I would prefer
> the 'fun' anu anonymous functions too.

But that's because "fun" isn't the anonymous function's name.
The name of the anonymous function *is* repeated in every clause.
That name is the sequence of characters strictly between these
brackets: [].

fun  (........) -> .......
    ^ name
  ;  (........) -> .......
    ^ exactly the same name again
end.

You may not *like* the name being invisible, especially because that
makes it impossible to write recursive funs, but you can't really
call it inconsistent (:-) (:-).

An interesting alternative would be to allow

    fun F(N) when is_integer(N), N > 1 -> F(N-1) + F(N-2)
      ; F(1) -> 1
      ; F(0) -> 1
    end

and if the variable here were optional (either appearing in all
clauses or disappearing in all clauses) it would be clear that the
traditional form has a consistently repeated but invisible name.

The thing that makes funs look ugly is people putting the semicolons
in the wrong place...

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Daniel Dormont
I'd like to add that neither Haskell nor Standard ML, both of which I used long before I discovered Erlang, allow anonymous functions to have multiple clauses. Discovering that feature in Erlang was a nice treat.

On the other hand, they do both allow nested named (and hence recursive) functions :)

dan

On Sun, May 15, 2011 at 11:58 PM, Richard O'Keefe <[hidden email]> wrote:

On 14/05/2011, at 3:01 AM, Attila Rajmund Nohl wrote:

> The (usual) problem is that this is not consistent. You have to write
> the function name for different function clauses, but must not write
> the 'fun' for different clauses in anonymous functions. I would prefer
> the 'fun' anu anonymous functions too.

But that's because "fun" isn't the anonymous function's name.
The name of the anonymous function *is* repeated in every clause.
That name is the sequence of characters strictly between these
brackets: [].

fun  (........) -> .......
   ^ name
 ;  (........) -> .......
   ^ exactly the same name again
end.

You may not *like* the name being invisible, especially because that
makes it impossible to write recursive funs, but you can't really
call it inconsistent (:-) (:-).

An interesting alternative would be to allow

   fun F(N) when is_integer(N), N > 1 -> F(N-1) + F(N-2)
     ; F(1) -> 1
     ; F(0) -> 1
   end

and if the variable here were optional (either appearing in all
clauses or disappearing in all clauses) it would be clear that the
traditional form has a consistently repeated but invisible name.

The thing that makes funs look ugly is people putting the semicolons
in the wrong place...

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
moi
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

moi
In reply to this post by Richard A. O'Keefe-2
On Mon, May 16, 2011 at 11:06 AM, Richard O'Keefe <[hidden email]> wrote:
>
> Why not?
> That's how you do do it in mathematics when defining a recurrence.
> That's how it's done in Haskell.
> That's how it's done in SML.
> That's how it's done in every functional language I know that _has_
> multiple clauses (so, not in Lisp).

That's not good reasoning. Mathematica (it's as a FP language as Lisp)
also lists function names in each parameter pattern, but its semantics
is quite different from Erlang:

say[n_] := "n";
say[0] := "zero";

When calling say[0], the 2nd definition is used because it's more
*specific* than the 1st one. -- Different from Erlang.

The reason of the existence of function names here is mainly to help
us/ctags, etc not to get lost in code, although Joe said no deep
though is involved. That's OK, I like it.

>>
>> It does not provide any information, except that one might make silly mistakes.
>
> Which do you suppose human beings rely on, when reading the code,
> to tell whether clauses belong together?
>
>  - the function name at the beginning of the line where they can see it
>  - a distinction between "." and ";" at differently placed ends of lines?
>
>> If omitting it, a new problem is how to indent clauses properly and
>> beautifully.
>>
>> Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
>>    Body1;
>>    (PatternK1,...,PatternKN) [when GuardSeqK] ->
>>    BodyK.
>
> Don't.  Use 'case' instead.
>
> For what it's worth, I write
>
>        fun (....) when ... ->
>            ...
>          ; (....) when ... ->
>            ...
>        end.
>
> If you don't have a name to make the alignment obvious, you desperately
> need the semicolon out the front.
>
>

Regarding eep-0028, I think putting ";" at the beginning of a line is
totally a bad idea, a big error in typesetting. There is already a
symbol for alignment, "(". How about this:

fun
(...) when ... ->
   ...;
(...) when ... ->
   ...
end.

PS, after learn something about J, one might start to dislike anything
that seems to be redundant.

--
Best Regards,
Justus
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

James Hague
In reply to this post by Torben Hoffmann
On Sat, May 14, 2011 at 6:34 AM, Torben Hoffmann
<[hidden email]> wrote:
> 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:
>
> Overview of what the function does.
> 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.

My personal style:

I keep everything in the function header if there are only a couple of
parameters.

If there are, say, four parameters to a function and only the first
one is used for pattern matching, then I use "case." That makes it
clear at a glance which parameters matter for matching and which are
fixed.

James
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Function Declaration Syntax

Richard A. O'Keefe-2
In reply to this post by Richard A. O'Keefe-2

On 16/05/2011, at 9:57 PM, Tony Finch wrote:

> Richard O'Keefe <[hidden email]> wrote:
>>
>> An interesting alternative would be to allow
>>
>>    fun F(N) when is_integer(N), N > 1 -> F(N-1) + F(N-2)
>>      ; F(1) -> 1
>>      ; F(0) -> 1
>>    end
>>
>> and if the variable here were optional (either appearing in all
>> clauses or disappearing in all clauses) it would be clear that the
>> traditional form has a consistently repeated but invisible name.
>
> A tangential observation ...
> http://bosker.wordpress.com/2011/04/29/the-worst-algorithm-in-the-world/

The example was intended to be RECOGNISABLE, not EFFICIENT.
I've been intimately familiar with the logarithmic method for the
last 30 years or more, and the closed-form solution (and how to
derive it and similar solutions to related problems) for at least
another 10 years before that.

I could have just said "think of the difference between LET and
named-LET in Scheme."

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions