I am trying to write a loop in elisp which prints the values sequencially.
I have tried the following code to print the sequence from 1.. which does not work. Please point the error in the code.
(let ((inc_variable 0))
(message "%S" inc_variable)
(while t (let ((inc_variable (+ inc_variable 1)))
(message "%S" inc_variable))))
There are two bindings for
inc_variablein this code. The outer binding has the value 0 and never changes. Then, each time round the loop, you create a new binding forinc_variablethat gets set to one plus the value of the outer binding (which is always 0). So the inner binding gets the value 1 each time.Remember that
letalways creates a new binding: if you want to update the value of an existing binding, usesetq: