-
-
Save myw/178165ff5002915e0ad3 to your computer and use it in GitHub Desktop.
| #include <stdio.h> | |
| int func(int, int, int); | |
| int | |
| main() | |
| { | |
| printf("%d\n", func(3, 4, 5)); | |
| return 0; | |
| } | |
| int | |
| func(int a, int b, int c) | |
| { | |
| int d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| int d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } |
| #include <stdio.h> | |
| int func(int, int, int); | |
| int | |
| main() | |
| { | |
| printf("%d\n", func(3, 4, 5)); | |
| return 0; | |
| } | |
| int | |
| func(int a, int b, int c) | |
| { | |
| int d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } |
| function func(a, b, c) { | |
| var d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| var d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } | |
| function main() { | |
| console.log(func(3, 4, 5)); | |
| } | |
| if (require.main === module) { | |
| main(); | |
| } |
| function func(a, b, c) { | |
| var d, h; | |
| d = 7; | |
| h = 3; | |
| while (h < 5) { | |
| d = 1; | |
| h++; | |
| d++; | |
| } | |
| return d; | |
| } | |
| function main() { | |
| console.log(func(3, 4, 5)); | |
| } | |
| if (require.main === module) { | |
| main(); | |
| } |
| def func(a, b, c): | |
| d = 7 | |
| h = 3 | |
| while h < 5: | |
| d = 1 | |
| h += 1 | |
| d += 1 | |
| return d | |
| if __name__ == '__main__': | |
| print(func(3, 4, 5)) |
Another way to look at it is through the idea of referential transparency. When does this snippet of code do it you put it in the middle of your program?
a = 1
b = 2
if a < b:
temp = a
a = b
b = temp
A lot of people would expect it to have just the effect of assigning a=2 and b=1 but it has the additional, perhaps unexpected, effect of assigning temp, should it already exist, to 1. In Scheme, this code looks like this:
(let ([a 1
[b 2])
(when (< a b)
(let ([temp a])
(set! a b)
(set! b temp))))
If there is a temp in scope outside of this block of code it has the exact same effect. temp is just briefly shadowed because the developer who added this code probably didn't know about it, but she doesn't have to because the lexical scoping makes it clear what this code is doing regardless of any, potentially hairy, context it might be in.
This is all solved by introducing a let or my or var keyword. Personally I'd require it. I hear this is happening in Python 3. I'm not sure if it's going to change it's scoping though. I'll have to read up on that. Hopefully the introduction of this keyword doesn't work like var in Javascript.
There are no scoping changes planned for Py3 as far as I can tell. ECMAScript 6, however, is planning on adding a let keyword, which would work normally, unlike that insane var nonsense: all curly braces would create local scope for variables declared with let.
yes, exactly