swap by name
Wayne Throop
throopw at dg_rtp.UUCP
Fri Jul 4 06:55:27 AEST 1986
> gwyn at brl-smoke.UUCP (Doug Gwyn (VLD/VMB) <gwyn>)
> It may be amusing and/or instructive to contemplate the fact that
> there is no way to write a function that exchanges the contents of
> two variables in a language where parameters are passed "by name".
Amusing, instructive.... and wrong.
I am assuming that you mean "Given a language which passes arguments to
functions with call-by-name semantics, it is impossible write an
'exchange argument pair' function, no matter what other facilities the
language provides". This is clearly incorrect. In fact, the C language
is a counterexample (assuming that the C language includes the
preprocessor).
Consider: macros act much like functions with by-name arguments. The
trick is to utter the swap while mentioning the arguments only once.
Thus, one needs a bind-by-reference operation, which in C is nicely
provided by the '&' operator (with some restrictions, granted).
My conclusion: a swap function is easy to implement even with
pass-by-name functions, *provided* there is a bind-by-reference operator
in the language.
--
It is a lovely language, but it takes a very long time to say anything
in it, because we do not say anything in it, unless it is worth taking
a long time to say, and to listen to.
--- J. R. R. Tolkien
--
Wayne Throop <the-known-world>!mcnc!rti-sel!dg_rtp!throopw
More information about the Comp.lang.c
mailing list