Variable declaration reset: Difference between revisions
Content deleted Content added
m →{{header|JavaScript}}: me & zero-based indexes, huh! |
Add Factor |
||
Line 32: | Line 32: | ||
with [[Rutgers_ALGOL_68|Rutgers Algol 68]]:<br> |
with [[Rutgers_ALGOL_68|Rutgers Algol 68]]:<br> |
||
No output. |
No output. |
||
=={{header|Factor}}== |
|||
Normally you would not use lexical scoping for something like this in Factor. But since it is possible, here we go. Note that: |
|||
* Factor doesn't allow you to declare a lexical variable without initializing it. |
|||
* Lexical variables are immutable by default. To make a lexical variable mutable, you need to add a <code>!</code> to its declaration. |
|||
{{works with|Factor|0.99 2022-04-03}} |
|||
<lang factor>USING: kernel math prettyprint sequences ; |
|||
[let |
|||
{ 1 2 2 3 4 4 5 } :> s |
|||
s length <iota> [| i | |
|||
i s nth -1 :> ( curr prev! ) |
|||
i 1 > curr prev = and |
|||
[ i . ] when |
|||
curr prev! |
|||
] each |
|||
]</lang> |
|||
{{out}} |
|||
[none] |
|||
To get output, we need to declare <code>prev</code> outside the <code>each</code> quotation. |
|||
{{works with|Factor|0.99 2022-04-03}} |
|||
<lang factor>USING: kernel math prettyprint sequences ; |
|||
[let |
|||
{ 1 2 2 3 4 4 5 } -1 :> ( s prev! ) |
|||
s length <iota> [| i | |
|||
i s nth :> curr |
|||
i 1 > curr prev = and |
|||
[ i . ] when |
|||
curr prev! |
|||
] each |
|||
]</lang> |
|||
{{out}} |
|||
<pre> |
|||
2 |
|||
5 |
|||
</pre> |
|||
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}} |
|||
<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 .</lang> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |