Variable declaration reset: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 3 users not shown)
Line 427:
end
</syntaxhighlight>
 
=={{header|K}}==
There is no such thing as a <i>straightforward longhand loop</i> in K. See also: https://nsl.com/
 
A natural expression for finding the indices of repeated elements (in [[ngn/k]]) might be:<syntaxhighlight lang=K>&=/'2':1 2 2 3 4 4 5
1 4</syntaxhighlight>
 
But of course, there's no variables here.
 
Longhand loops can be emulated, using lambdas (in which case local variables expire when the lambda exits), but this is not what many programmers would think of as a straightforward longhand loop.
 
=={{header|Nim}}==
In Nim, a loop create a new block. Variables declared in the loop body are created and initialized at each round: they do not retain the value from one round to the next.
 
Moreover, a variable needs to be declared before use, except variables in “for” loop which are implicitly declared in the loop block scope. If the variable has not been declared, the program fails to compile.
 
Is is not mandatory to initialize a variable. If there is no explicit initialization, the variable gets a default value which depends on its type (this is a binary zero).
 
Thus, the following program doesn’t compile, as, at compile time, “prev” is used before its declaration:
 
<syntaxhighlight lang="Nim">let s = [1, 2, 2, 3, 4, 4, 5]
for i in 0..s.high:
let curr = s[i]
if i > 0 and curr == prev:
echo i
var prev = curr
</syntaxhighlight>
 
The following program compiles but doesn’t output the right result as “prev” is reset at beginning of each round:
 
<syntaxhighlight lang="Nim">let s = [1, 2, 2, 3, 4, 4, 5]
for i in 0..s.high:
let curr = s[i]
var prev: int
if i > 0 and curr == prev:
echo i
prev = curr
</syntaxhighlight>
 
To get the right result, we need to declare “prev” outside the loop.
<syntaxhighlight lang="Nim">let s = [1, 2, 2, 3, 4, 4, 5]
var prev: int
for i in 0..s.high:
let curr = s[i]
if i > 0 and curr == prev:
echo i
prev = curr
</syntaxhighlight>
 
{{out}}
<pre>2
5
</pre>
 
=={{header|Perl}}==
Line 696 ⟶ 749:
</pre>
 
=={{header|V (Vlang)}}==
Note firstly that unassigned variables are impossible in Vlang. If a variable is created it must have an explicit value, then it is assigned the default value for its type which in the case of numbers is zero. Fortunately, this doesn't clash with values in the slice in the following program.
<syntaxhighlight lang="v (vlang)">fn main() {
s := [1, 2, 2, 3, 4, 4, 5]
Line 732 ⟶ 785:
=={{header|Wren}}==
Note firstly that unassigned variables are impossible in Wren. If a variable is created without giving it an explicit value, then it is assigned the special value 'null' which is the only instance of the Null class and therefore distinct from all other values in the language.
<syntaxhighlight lang="ecmascriptwren">var s = [1, 2, 2, 3, 4, 4, 5]
 
// There is no output as 'prev' is created anew each time
9,476

edits