If I'm not mistaken, the concept of a "lazy imperative programming language" makes perfect sense.
For example, I envision that the following code should cause the program to print "7"
a <- 1
b <- a+5
a <- 2
print([b])
while the following code should cause the program to print "6"
a <- 1
b <- [a+5]
a <- 2
print(b)
and the following code should cause the program to print the string "a+5"
a <- 1
b <- a+5
a <- 2
print(b)
The idea is that [..]
flattens an expression by performing an evaluation, using the current values of each variable.
Question. Do lazy imperative programming languages exist, and if not, why not? Is there any particular reason why they cannot ever exist?
(n**2 for n in range(10))
produces no squares until you try to iterate over it -- or pass it to a function likesum
which implicitly iterates over it. This was the case even in Python2 but was made more extensive in Python3. The core evaluation strategy is still eager, though. – Hobardvar a = 1; var b = () => a + 5; a = 2; console.log(b());
which prints "7" becauseb
wraps the expression in a lambda that is defined whena
is 1 but not executed untila
is set to 2. The[]
syntax in your example code could just be interpreted as the syntax to execute a function-wrapped expression, so your second example would instead look likevar a = 1; var b = (() => a + 5)(); a = 2; console.log(b);
which would print "6". – Sisterly