Variable declaration reset: Difference between revisions

Realize in F#
(Realize in F#)
Line 123:
</pre>
 
=={{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.
<lang fsharp>
// Variable declaration reset. Nigel Galloway: June 21st 2022
let s=[1;2;2;3;4;4;5]
// First let me write this in real F#, which rather avoids the whole issue
printfn "Real F#"
s|>List.pairwise|>List.iteri(fun i (n,g)->if n=g then printfn "%d" (i+1))
// Now let me take the opportunity to write some awful F# by translating the C++
printfn "C++ like awful F#"
let mutable previousValue = -1
for i in 0..s.Length-1 do
let currentValue=s.[i]
if previousValue = currentValue then printfn "%d" i
previousValue <- currentValue
</lang>
{{out}}
<pre>
Real F#
2
5
C++ like awful F#
2
5
</pre>
=={{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:
2,171

edits