Variable declaration reset: Difference between revisions
Content added Content deleted
(Add Seed7) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12: | Line 12: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
In Algol 68, things that aren't explicitely initialised are notionally initialised to SKIP - an indeterminate value, so there should be no output from the program. Each iteration of the loop will get a new curr and prev, with prev initialised to SKIP. The following is equivalent to the Phix program... |
In Algol 68, things that aren't explicitely initialised are notionally initialised to SKIP - an indeterminate value, so there should be no output from the program. Each iteration of the loop will get a new curr and prev, with prev initialised to SKIP. The following is equivalent to the Phix program... |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
[]INT s = ( 1, 2, 2, 3, 4, 4, 5 ); |
[]INT s = ( 1, 2, 2, 3, 4, 4, 5 ); |
||
FOR i FROM LWB s TO UPB s DO |
FOR i FROM LWB s TO UPB s DO |
||
Line 21: | Line 21: | ||
prev := curr |
prev := curr |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
...however, one of the non-standard features of Algol 68G is that uninitialised variables cause a runtime error instead of silently being set to SKIP. |
...however, one of the non-standard features of Algol 68G is that uninitialised variables cause a runtime error instead of silently being set to SKIP. |
||
{{out}} with [[ALGOL_68_Genie|Algol 68G]]: |
{{out}} with [[ALGOL_68_Genie|Algol 68G]]: |
||
Line 31: | Line 31: | ||
No output. |
No output. |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f VARIABLE_DECLARATION_RESET.AWK |
# syntax: GAWK -f VARIABLE_DECLARATION_RESET.AWK |
||
BEGIN { |
BEGIN { |
||
Line 44: | Line 44: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 55: | Line 55: | ||
The following compiles using either C89/90 (-std=c90 -ansi -pedantic) or C99 syntax using gcc 9.4.0. |
The following compiles using either C89/90 (-std=c90 -ansi -pedantic) or C99 syntax using gcc 9.4.0. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int main() { |
int main() { |
||
Line 81: | Line 81: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<small>(Note: Obviously the <code>for (int i=0, prev</code> needs the outer i and the inner prev removed, and the same "int" added to the second loop, for it to compile cleanly though it only does so under C99 (or later) as for loop initial declarations are not allowed in C89/90.)</small> |
<small>(Note: Obviously the <code>for (int i=0, prev</code> needs the outer i and the inner prev removed, and the same "int" added to the second loop, for it to compile cleanly though it only does so under C99 (or later) as for loop initial declarations are not allowed in C89/90.)</small> |
||
{{out}} |
{{out}} |
||
Line 90: | Line 90: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <array> |
||
#include <iostream> |
#include <iostream> |
||
Line 116: | Line 116: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 125: | Line 125: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Functional programming doesn't really do variables!!! There is no particular judgement of right or wrong here, just a plain-speaking statement that using variables is awful. |
Functional programming doesn't really do variables!!! There is no particular judgement of right or wrong here, just a plain-speaking statement that using variables is awful. |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Variable declaration reset. Nigel Galloway: June 21st 2022 |
// Variable declaration reset. Nigel Galloway: June 21st 2022 |
||
let s=[1;2;2;3;4;4;5] |
let s=[1;2;2;3;4;4;5] |
||
Line 138: | Line 138: | ||
if previousValue = currentValue then printfn "%d" i |
if previousValue = currentValue then printfn "%d" i |
||
previousValue <- currentValue |
previousValue <- currentValue |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 155: | Line 155: | ||
{{works with|Factor|0.99 2022-04-03}} |
{{works with|Factor|0.99 2022-04-03}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel math prettyprint sequences ; |
||
[let |
[let |
||
Line 165: | Line 165: | ||
curr prev! |
curr prev! |
||
] each |
] each |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[none] |
[none] |
||
Line 173: | Line 173: | ||
{{works with|Factor|0.99 2022-04-03}} |
{{works with|Factor|0.99 2022-04-03}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel math prettyprint sequences ; |
||
[let |
[let |
||
Line 183: | Line 183: | ||
curr prev! |
curr prev! |
||
] each |
] each |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 191: | Line 191: | ||
Now compare to how you would normally solve this in Factor, where issues of variables and scope are irrelevant: |
Now compare to how you would normally solve this in Factor, where issues of variables and scope are irrelevant: |
||
{{works with|Factor|0.99 2022-04-03}} |
{{works with|Factor|0.99 2022-04-03}} |
||
< |
<syntaxhighlight lang="factor">USING: grouping math.vectors prettyprint sequences.extras ; |
||
{ 1 2 2 3 4 4 5 } 2 <clumps> [ all-eq? ] arg-where 1 v+n .</ |
{ 1 2 2 3 4 4 5 } 2 <clumps> [ all-eq? ] arg-where 1 v+n .</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">Dim As Integer s(1 To 7) => {1,2,2,3,4,4,5} |
||
For i As Integer = 1 To Ubound(s) |
For i As Integer = 1 To Ubound(s) |
||
Dim As Integer curr = s(i), prev |
Dim As Integer curr = s(i), prev |
||
Line 202: | Line 202: | ||
prev = curr |
prev = curr |
||
Next i |
Next i |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 215: | Line 215: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Note firstly that unassigned variables are impossible in Go. If a variable is created (using the 'var' keyword) without giving it 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. |
Note firstly that unassigned variables are impossible in Go. If a variable is created (using the 'var' keyword) without giving it 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="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 243: | Line 243: | ||
prev = curr |
prev = curr |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 257: | Line 257: | ||
First off, the idiomatic J approach to finding indices of numbers which match their predecessors would be: |
First off, the idiomatic J approach to finding indices of numbers which match their predecessors would be: |
||
< |
<syntaxhighlight lang="j"> 1+I.(}:=}.) 1 2 2 3 4 4 5 |
||
2 5</ |
2 5</syntaxhighlight> |
||
In other words, compare adjacent numbers (which results in a list of results one element shorter than the argument), find the indices of the matches (which would be the indices of the pairs which match) and add one (to get the indices in the original list of the second value of each of the pairs). |
In other words, compare adjacent numbers (which results in a list of results one element shorter than the argument), find the indices of the matches (which would be the indices of the pairs which match) and add one (to get the indices in the original list of the second value of each of the pairs). |
||
Line 268: | Line 268: | ||
Anyways, here's a rough approximation of what the task is asking for: |
Anyways, here's a rough approximation of what the task is asking for: |
||
< |
<syntaxhighlight lang="j">same2=: {{ |
||
i=. 0 |
i=. 0 |
||
r=. ,EMPTY |
r=. ,EMPTY |
||
Line 282: | Line 282: | ||
end. |
end. |
||
r |
r |
||
}}</ |
}}</syntaxhighlight> |
||
This gives us: |
This gives us: |
||
< |
<syntaxhighlight lang="j"> same2 1,2,2,3,4,4,5 |
||
2 5</ |
2 5</syntaxhighlight> |
||
But, since we were unable to declare 'prev' before it was assigned, we have no way of moving that declaration of 'prev' outside of the loop. We could add a declaration of 'prev' outside of the loop, |
But, since we were unable to declare 'prev' before it was assigned, we have no way of moving that declaration of 'prev' outside of the loop. We could add a declaration of 'prev' outside of the loop, |
||
< |
<syntaxhighlight lang="j">same3=: {{ |
||
i=. 0 |
i=. 0 |
||
r=. ,EMPTY |
r=. ,EMPTY |
||
Line 306: | Line 306: | ||
end. |
end. |
||
r |
r |
||
}}</ |
}}</syntaxhighlight> |
||
But it would not alter the generated result. |
But it would not alter the generated result. |
||
Line 316: | Line 316: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
Note firstly that variables declared in methods must be assigned a value before they can be used in Java and so here we give '(g)prev' an initial value of 0 which won't clash with the values in the array. |
Note firstly that variables declared in methods must be assigned a value before they can be used in Java and so here we give '(g)prev' an initial value of 0 which won't clash with the values in the array. |
||
< |
<syntaxhighlight lang="java">public class VariableDeclarationReset { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
int[] s = {1, 2, 2, 3, 4, 4, 5}; |
int[] s = {1, 2, 2, 3, 4, 4, 5}; |
||
Line 340: | Line 340: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 349: | Line 349: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript"><!DOCTYPE html> |
||
<html lang="en" > |
<html lang="en" > |
||
<head> |
<head> |
||
Line 369: | Line 369: | ||
</script> |
</script> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight> |
||
No output<br> |
No output<br> |
||
Any of 1) manually moving the declaration of prev to before the loop, or 2) using <code>for (let i=0, prev; i<7; i+=1)</code>, and in fact initialising prev there, to any value, works exactly the same, or 3) changing the third "let" to "var" (causes legacy hoisting and) gives: |
Any of 1) manually moving the declaration of prev to before the loop, or 2) using <code>for (let i=0, prev; i<7; i+=1)</code>, and in fact initialising prev there, to any value, works exactly the same, or 3) changing the third "let" to "var" (causes legacy hoisting and) gives: |
||
Line 383: | Line 383: | ||
As it happens, if the first argument of range/2 was changed to 0, then in this particular case the correct results would still be correct because at the first iteration, the test would be $array[0] == $array[-1], the point being that $array[-1] evaluates to the last element of the array. That is, the "bug" in the program would not be revealed by the test case. |
As it happens, if the first argument of range/2 was changed to 0, then in this particular case the correct results would still be correct because at the first iteration, the test would be $array[0] == $array[-1], the point being that $array[-1] evaluates to the last element of the array. That is, the "bug" in the program would not be revealed by the test case. |
||
< |
<syntaxhighlight lang="jq">[1,2,2,3,4,4,5] |
||
| . as $array |
| . as $array |
||
| range(1;length) |
| range(1;length) |
||
| select( $array[.] == $array[.-1]) |
| select( $array[.] == $array[.-1]) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
In Julia, variables are declared by being defined. Because variables also must be defined before they are |
In Julia, variables are declared by being defined. Because variables also must be defined before they are |
||
referred to in compiled code, the code below yields an error that the variable `prev` is not defined: |
referred to in compiled code, the code below yields an error that the variable `prev` is not defined: |
||
< |
<syntaxhighlight lang="julia"> |
||
s = [1, 2, 2, 3, 4, 4, 5] |
s = [1, 2, 2, 3, 4, 4, 5] |
||
Line 400: | Line 400: | ||
prev = curr |
prev = curr |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
If the variable `prev` is defined before the `for` statement, the code then runs. We also may |
If the variable `prev` is defined before the `for` statement, the code then runs. We also may |
||
declare the variable `prev` as global to refer explicitly to the variable declared outside of the for block: |
declare the variable `prev` as global to refer explicitly to the variable declared outside of the for block: |
||
< |
<syntaxhighlight lang="julia"> |
||
s = [1, 2, 2, 3, 4, 4, 5] |
s = [1, 2, 2, 3, 4, 4, 5] |
||
prev = -1 |
prev = -1 |
||
Line 413: | Line 413: | ||
prev = curr |
prev = curr |
||
end |
end |
||
</ |
</syntaxhighlight> {{out}} |
||
<pre> |
<pre> |
||
3 |
3 |
||
Line 420: | Line 420: | ||
Parenthetical note: making a global variable to support a for loop has a bad code smell in Julia. A better |
Parenthetical note: making a global variable to support a for loop has a bad code smell in Julia. A better |
||
way to do such a comparison of adjacent values in an array is to alter the start of the loop variable: |
way to do such a comparison of adjacent values in an array is to alter the start of the loop variable: |
||
< |
<syntaxhighlight lang="julia"> |
||
s = [1, 2, 2, 3, 4, 4, 5] |
s = [1, 2, 2, 3, 4, 4, 5] |
||
Line 426: | Line 426: | ||
s[i] == s[i - 1] && println(i) |
s[i] == s[i - 1] && println(i) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
By default, variables can be created on-the-fly, as with <code>$prev</code> here. Testing against <code>$curr</code> is not an error, even when it's value is undefined. This is perhaps not "best practices", but it does work just fine. |
By default, variables can be created on-the-fly, as with <code>$prev</code> here. Testing against <code>$curr</code> is not an error, even when it's value is undefined. This is perhaps not "best practices", but it does work just fine. |
||
< |
<syntaxhighlight lang="perl">@s = <1 2 2 3 4 4 5>; |
||
for ($i = 0; $i < 7; $i++) { |
for ($i = 0; $i < 7; $i++) { |
||
$curr = $s[$i]; |
$curr = $s[$i]; |
||
if ($i > 1 and $curr == $prev) { print "$i\n" } |
if ($i > 1 and $curr == $prev) { print "$i\n" } |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 441: | Line 441: | ||
But better to do it this way, requiring <code>my</code> declarations imposing lexical scope (an instance of <code>$curr</code> is instantiated on every pass through loop) and employing a <code>state</code> variable (persistent within loop). |
But better to do it this way, requiring <code>my</code> declarations imposing lexical scope (an instance of <code>$curr</code> is instantiated on every pass through loop) and employing a <code>state</code> variable (persistent within loop). |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'state'; |
use feature 'state'; |
||
Line 451: | Line 451: | ||
if ($i > 1 and $curr == $prev) { print "$i\n" } |
if ($i > 1 and $curr == $prev) { print "$i\n" } |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 457: | Line 457: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}</span> |
||
Line 467: | Line 467: | ||
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span> |
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 480: | Line 480: | ||
Although PL/M has block scope, all variables are static, so PREV retains its value between iterations of the loop.<br> |
Although PL/M has block scope, all variables are static, so PREV retains its value between iterations of the loop.<br> |
||
Note the extra DO which is necessary to introduce a new scope as declarations are not allowed in a DO loop. |
Note the extra DO which is necessary to introduce a new scope as declarations are not allowed in a DO loop. |
||
< |
<syntaxhighlight lang="pli">100H: |
||
/* CP/M BDOS SYSTEM CALL */ |
/* CP/M BDOS SYSTEM CALL */ |
||
Line 513: | Line 513: | ||
END; |
END; |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 524: | Line 524: | ||
The following code is legal, but note that a Python code checker such as pyflakes will flag such code with an error. |
The following code is legal, but note that a Python code checker such as pyflakes will flag such code with an error. |
||
< |
<syntaxhighlight lang="python"> |
||
s = [1, 2, 2, 3, 4, 4, 5] |
s = [1, 2, 2, 3, 4, 4, 5] |
||
Line 532: | Line 532: | ||
print(i) |
print(i) |
||
prev = curr |
prev = curr |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
2 |
2 |
||
Line 541: | Line 541: | ||
By default, Raku variables need a prefix sigil indicating the storage / interface, and a scope declarator to indicate the variables' accessibility. The vast majority of the time, variables are declared with a "my" scope declarator that constrains them to the present block and any enclosed sub blocks. When a 'my' variable is declared inside a loop (block), a new independent instance of the variable is instantiated every time through. |
By default, Raku variables need a prefix sigil indicating the storage / interface, and a scope declarator to indicate the variables' accessibility. The vast majority of the time, variables are declared with a "my" scope declarator that constrains them to the present block and any enclosed sub blocks. When a 'my' variable is declared inside a loop (block), a new independent instance of the variable is instantiated every time through. |
||
<lang |
<syntaxhighlight lang="raku" line>my @s = 1, 2, 2, 3, 4, 4, 5; |
||
loop (my $i = 0; $i < 7; $i += 1) { |
loop (my $i = 0; $i < 7; $i += 1) { |
||
my $curr = @s[$i]; |
my $curr = @s[$i]; |
||
Line 549: | Line 549: | ||
} |
} |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
{{out|Yields}} |
{{out|Yields}} |
||
<pre>Use of uninitialized value of type Any in numeric context |
<pre>Use of uninitialized value of type Any in numeric context |
||
Line 564: | Line 564: | ||
Lots of warnings but nothing else. If we suppress the warnings: |
Lots of warnings but nothing else. If we suppress the warnings: |
||
<lang |
<syntaxhighlight lang="raku" line>my @s = 1, 2, 2, 3, 4, 4, 5; |
||
quietly loop (my $i = 0; $i < 7; $i += 1) { |
quietly loop (my $i = 0; $i < 7; $i += 1) { |
||
my $curr = @s[$i]; |
my $curr = @s[$i]; |
||
Line 572: | Line 572: | ||
} |
} |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
No output. |
No output. |
||
Line 579: | Line 579: | ||
We can however, declare the variable with an "our" scope, which effectively makes it a package global. Use of 'our' scoping is discouraged except in a few very specific situations. It "works" (for some value of works), but pollutes the namespace. The 'our' variable will trample any other instance of a variable with that name anywhere in the program in any other scope. |
We can however, declare the variable with an "our" scope, which effectively makes it a package global. Use of 'our' scoping is discouraged except in a few very specific situations. It "works" (for some value of works), but pollutes the namespace. The 'our' variable will trample any other instance of a variable with that name anywhere in the program in any other scope. |
||
<lang |
<syntaxhighlight lang="raku" line>my @s = 1, 2, 2, 3, 4, 4, 5; |
||
loop (my $i = 0; $i < 7; $i += 1) { |
loop (my $i = 0; $i < 7; $i += 1) { |
||
my $curr = @s[$i]; |
my $curr = @s[$i]; |
||
Line 587: | Line 587: | ||
} |
} |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
{{out|Yields}} |
{{out|Yields}} |
||
Line 595: | Line 595: | ||
A better solution is to declare a state variable. A 'state' variable is essentially scoped similar to a 'my' variable (visible only inside the block), but is persistent across calls. |
A better solution is to declare a state variable. A 'state' variable is essentially scoped similar to a 'my' variable (visible only inside the block), but is persistent across calls. |
||
<lang |
<syntaxhighlight lang="raku" line>my @s = 1, 2, 2, 3, 4, 4, 5; |
||
loop (my $i = 0; $i < 7; $i += 1) { |
loop (my $i = 0; $i < 7; $i += 1) { |
||
my $curr = @s[$i]; |
my $curr = @s[$i]; |
||
Line 603: | Line 603: | ||
} |
} |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
{{out|Yields}} |
{{out|Yields}} |
||
Line 612: | Line 612: | ||
No scope declarators at all. Every variable is a global. Bad idea. Do not do this casually. |
No scope declarators at all. Every variable is a global. Bad idea. Do not do this casually. |
||
<lang |
<syntaxhighlight lang="raku" line>no strict; |
||
@s = 1, 2, 2, 3, 4, 4, 5; |
@s = 1, 2, 2, 3, 4, 4, 5; |
||
loop ($i = 0; $i < 7; $i += 1) { |
loop ($i = 0; $i < 7; $i += 1) { |
||
Line 620: | Line 620: | ||
} |
} |
||
$prev = $curr; |
$prev = $curr; |
||
}</ |
}</syntaxhighlight> |
||
{{out|Yields}} |
{{out|Yields}} |
||
Line 629: | Line 629: | ||
* Blocks start at index 1 in Red. |
* Blocks start at index 1 in Red. |
||
* <code>all</code> short-circuits, so <code>prev</code> will be defined by the time <code>curr = prev</code> is checked. |
* <code>all</code> short-circuits, so <code>prev</code> will be defined by the time <code>curr = prev</code> is checked. |
||
< |
<syntaxhighlight lang="rebol">Red[] |
||
s: [1 2 2 3 4 4 5] |
s: [1 2 2 3 4 4 5] |
||
repeat i length? s [ |
repeat i length? s [ |
||
Line 637: | Line 637: | ||
] |
] |
||
prev: curr |
prev: curr |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 646: | Line 646: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Variables must be declared in the locals section (or as globals) before execution begins, so this whole excercise is moot. There is only one way to write it and it's the way that works. |
Variables must be declared in the locals section (or as globals) before execution begins, so this whole excercise is moot. There is only one way to write it and it's the way that works. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 662: | Line 662: | ||
prev := curr; |
prev := curr; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 670: | Line 670: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang="vbnet">Option Strict On |
||
Option Explicit On |
Option Explicit On |
||
Line 689: | Line 689: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 698: | Line 698: | ||
=={{header|Vlang}}== |
=={{header|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. |
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="vlang">fn main() { |
||
s := [1, 2, 2, 3, 4, 4, 5] |
s := [1, 2, 2, 3, 4, 4, 5] |
||
Line 722: | Line 722: | ||
prev = curr |
prev = curr |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 732: | Line 732: | ||
=={{header|Wren}}== |
=={{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. |
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="ecmascript">var s = [1, 2, 2, 3, 4, 4, 5] |
||
// There is no output as 'prev' is created anew each time |
// There is no output as 'prev' is created anew each time |
||
Line 750: | Line 750: | ||
if (i > 0 && curr == prev) System.print(i) |
if (i > 0 && curr == prev) System.print(i) |
||
prev = curr |
prev = curr |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |