Erlang Versus Python

Samuel Tardieu sam@REDACTED
Tue Mar 7 11:35:47 CET 2000


| You get the idea. You can also programmatically create function objects
| via compile() and friends. So, where're the tricks? ;)

I notice that you don't mention closures :)

Eshell V4.9.1  (abort with ^G)
1> F = fun (X) -> fun (Y) -> X * Y end end.
#Fun<erl_eval.19.120858100>
2> G = F (5).
#Fun<erl_eval.19.120858100>
3> G (3).
15
4> G (4).
20

In Python, you don't capture any external variable.

Python 1.5.2 (#2, Feb 18 2000, 05:02:29)  [GCC 2.95.2 19991024 (release)] on freebsd4
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def F (X):
...     def G (Y): return X*Y
...     return G
...
>>> G = F (5)
>>> G (3)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in G
NameError: X

You have to "cheat" by using default arguments:

>>> def F (X):
...     def G (Y, X=X): return X * Y
...     return G
...
>>> G = F (5)
>>> G (3)
15
>>> G (4)
20

The "X=X" construct reads "when not supplied, the X parameter will be equal
to (resolved at compile time) X". In fact, G is a 2-args function with a
default second parameter, not a 1-arg one as Erlang or ML return.




More information about the erlang-questions mailing list