Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!mcnc!rti-sel!dg_rtp!throopw From: throopw@dg_rtp.UUCP (Wayne Throop) Newsgroups: net.lang.c Subject: Re: swap by name Message-ID: <433@dg_rtp.UUCP> Date: Mon, 7-Jul-86 16:37:52 EDT Article-I.D.: dg_rtp.433 Posted: Mon Jul 7 16:37:52 1986 Date-Received: Tue, 8-Jul-86 07:32:48 EDT References: <1959@brl-smoke.ARPA> Lines: 72 Summary: What "bind-by-reference" means to me (in gory detail) > gwyn@BRL.ARPA (VLD/VMB) > I'm not sure how one would implement a bind-by-reference > operator in a language with call-by-name function interface, > though. Relatively simple. It implies that the bind-by-reference operator must not be a function, but this is no stranger than allowing a bind-by-reference operation in C, where function arguments are bind-by-value. > Are the names to be forced through the bind or not? > I think my original statement is correct no matter what else > you add to the language, unless you break call-by-name. I seem to have been unclear. The bind-by-reference operator would not affect the way values are passed to functions. It would be an operator that could be applied to (perhaps) any expression. In a language with lisp-like syntax but with call-by-name, we might implement swap something like so: (define-function swap (a b) (declare t) (bind-by-reference ((a-alias a) (b-alias b)) (assign t a-alias) (assign a-alias b-alias) (assign b-alias t) ) ) "define-function", "declare" and "bind-by-reference" must be special forms, but "assign" and "swap" can be functions. The bind-by-reference form coins new names, which, when evaluated, always yield the same instance. Thus, swap only mentions "a" and "b" once. I have glossed over detail (what type is "t", for example?), but I hope everybody can see how this would work. Casting it in C, it might work something like this: void swap( a, b ) int *(*a)(), *(*b)(); { int t, *a_alias = (*a)(), *b_alias = (*b)(); t = *a_alias; *a_alias = *b_alias; *b_alias = t; } Of course, to be exactly proper, t, a_alias, and b_alias ought to be of type (int *(*)()) also. The t function would always return an address in swap's local frame, a_alias would always return what a returned at bind-time, and so on. Can be done, but makes the function a *lot* uglier. I'll leave that as an excersize for the masochistic. > In any case, I was just trying to illuminate one aspect of > the problem; you have added some more light to that. Who > needs a universal swap macro anyway? Well, the "weak form" of your original point (the meaning of "swap" in the presense of by-name binding is a real problem) is still quite valid. I just was trying to point out that implicit by-name binding doesn't mean that the language can't provide explicit by-reference binding. Just as in C, the fact that the language binds by-value implicitly, doesn't mean that one can't bind by-reference or by-name explicitly. And I don't know who needs a universal swap macro. I guess I view these "devise a swap such that..." challenges as finger excersizes. -- It is possible by ingenuity and at the expense of clarity ... [to do almost anything in any language]. However, the fact that it is possible to push a pea up a mountain with your nose does not mean that this is a sensible way of getting it there. --- Christopher Strachey -- Wayne Throop !mcnc!rti-sel!dg_rtp!throopw