I've check wikipedia, and googled but I still can't wrap my mind around how pass-by-name works in ALGOL 60.
相关问题
- Pythonic Way To Check for A Parameter Type
- passing search parameter through jquery
- ISCC - /D compiler-parameter seems to have no effe
- putc needs stdout, vs puts
- VBA passing date parameters to SQL Server stored p
相关文章
- If CancellationToken is a struct and is passed by
- Accessing variables in a function within a functio
- Pass-by-value (StringBuilder vs String) [duplicate
- History of access control modifiers such as public
- passing arguments in google apps script
- What came first: git subtree merge strategy or git
- Can't pass a script block as a parameter to po
- Detemine the previous page (i.e. referrer) in jQTo
ALGOL was designed for mathematical algorithms. I like the summation function as an example of call by name.
Sorry my ALGOL is a bit rusty the syntax is probably not right.
You could than use sum like
In the above the inner sum(y,3,8,x+y) would generate an unnamed function to pass to the outer sum call. The variables x and y are not passed by value but by name. In the case of variables call by name is equivalent to call by address reference in C. It gets a bit confusing when recursion is involved.
Borrows made ALGOL machines. They had 48 bit word memory with 3 flag bits. The flag bits implemented the cal by name of ALGOL. it was a stack machine so when function was loaded onto the stack the call by name fag would cause it to be called. The compiler would generate unnamed functions when expressions were used as arguments. A variable would be a simple indirect reference. An error would occur writing to a function.
I'm assuming you mean call-by-name in ALGOL 60.
Call-by-name is similar to call-by-reference in that you can change the value of the passed in parameter. It differs from call-by-reference in that the parameter is not evaluated before the procedure is called but is instead evaluated lazily. That is, it is evaluated when and only when the parameter is actually used.
For example, suppose we have a procedure
f(x, y)
and we pass iti
andi/2
wherei
is initially equal to10
. Iff
setsx
to42
and then evaluatesy
it will see the value21
(whereas with call by reference or call by value it would still see5
). This is because the expressioni/2
isn't evaluated untily
is evaluated.In many ways this appears to behave like a literal text-substitution of the parameters (with renaming to avoid name conflicts). In practice, however, this is implemented using "thunks" (basically closures) for the passed in expressions.
The Wikipedia article on Jensen's Device shows some interesting examples of using call by name. Here is one of them:
You can pass "name" in the symbolic form of a variable which allows it to be both updated and accessed simultaneously. As an example lets say you want to triple a variable x which is of type int :
I know I'm joining late to the club and this is not necessarily an answer, but I did want to add one thing that could help clarify a little. I've always thought of the Algol pass-by-name as a similar process to when the C++ preprocessor directives (macros, specifically) replaces the name of some function/variable with the the actual piece of code during compile time. The pass-by-name essentially replaces the name of the formal parameter with the actual parameter, and executes. I've never written in Algol, but I hear that pass-by-name will have the same result as C++'s pass-by-reference.
Actually, call-by-name, is not just a historical curiosity. You can do call-by-name in Windows batch files (and myriad of other scripting languages). Knowing how it works, and how to use it effectively in programming can open up neat solutions to problems. I know it is only passing strings through for later expansion, but it can be manipulated to have similar effects as call-by-name.
For those in the future:
Concepts in Programming Languages by John C. Mitchell was also helpful.