[erlang-questions] Does Erlang Treat Everything Function?
Richard O'Keefe
ok@REDACTED
Tue May 18 01:35:42 CEST 2010
On May 18, 2010, at 4:51 AM, 黃耀賢 (Yau-Hsien Huang) wrote:
> Hi, Erlang-lovers! I'm writing Erlang tutorials in my local
> language, Chinese. I felt in thinking about a word
> "Everything is functions."
> Could I say that Erlang realizes the word too?
> That is, to say even if a term is number, it's also a function:
> for example, 0 is a function 0/0. Is it right?
About a hundred years ago, people were trying to put mathematics
on a rigorous foundation. After several attempts, consistent
set theories were developed. In set theory, we *define*
0 = {}
1 = {0}
2 = {0,1}
...
n = {0,1,2,...,n-1}
with the result that
i < j
i ∈ j
i ⊊ j (that's proper-subset)
all mean exactly the same thing for ordinal numbers. But outside
set theory, nobody would think of 1 ∈ 2 as anything but a mistake.
Church's lambda-calculus was part of the foundations programme.
In the pure lambda calculus we can *define*
0 = \f.\x.x
1 = \f.\x.f x
2 = \f.\x.f (f x)
...
n = \f.\x.f ( .... (f x) ...) f applied n times
with the result that
i ∘ j
i × j
do the same. But outside pure lambda calculus, nobody would think
of 3 ∘ 4 as anything but a mistake.
There is a simple experiment you can perform to see whether in
fact 0 is a function named 0 with arity 0 in Erlang.
% erl
1> 0().
** exception error: bad function 0
2> X = 0, X().
** exception error: bad function 0
Erlang also draws a distinction between atoms and functions
of no arguments:
3> q.
q
4> q().
ok
5>
% (Unix prompt; q() shut Erlang down)
For practical reasons, functional programming languages like Lisp,
Erlang, Haskell, Clean, and ML DON'T take the view that everything
is a function. Most grammatically possible programs are nonsense.
The statically typed languages try to reject as much nonsense as
possible at compile time, at the price of rejecting some things
that are not nonsense. In those languages, numbers *might* well
be functions really, but they ensure that you can't *tell* whether
they are or not. The dynamically typed languages have to wait
until run time to check for probable nonsense. Things that it is
useful to distinguish (like numbers and atoms) are, as it were,
painted in different colours. The number 0 and the atom '0'
might well *be* functions underneath, and they might well even
be the same function, but 0+'0' finds one thing with number-
coloured paint and another with atom-coloured paint and exclaims
in horror.
More information about the erlang-questions
mailing list