Common Lisp Parallel vs. Sequential Declaration/Assignment
1 2 3 4 5 6 7 8 9 10 |
[1]> (let ((a 1)) (let ((a 2) (b a)) b)) 1 [2]> (let ((a 1)) (let* ((a 2) (b a)) b)) 2 |
Common Lisp Redeclaration
1 2 3 4 5 6 7 8 9 10 |
[1]> (let ((a 1)) (let ((a 2) (a (+ a 2))) a)) 3 [2]> (let ((a 1)) (let* ((a 2) (a (+ a 2))) a)) 4 |
Python global/local scope
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Adapted from Scott, Michael Lee. Programming language pragmatics, 3e. Morgan Kaufmann, 2016. i = 1 j = 3 def outer(): def middle(k): def inner(): global i i = 4 inner() return i, j, k i = 2 return middle(j) print outer() print (i, j) # output: # (2, 3, 3) # (4, 3) |
Nested Block Scope in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#import <stdio.h> void blockscope(){ int i = 2; int j = 3; printf("outer: i: %d, j: %d\n", i, j); { int i = 4; printf("middle: i: %d, j: %d\n", i, j); { j = 5; printf("inner: i: %d, j: %d\n", i, j); } } printf("outer: i: %d, j: %d\n", i, j); } int main(){ blockscope(); } // Output: // outer: i: 2, j: 3 // middle: i: 4, j: 3 // inner: i: 4, j: 5 // outer: i: 2, j: 5 |