Generic swap: Difference between revisions
→{{header|langur}}
(Added Chipmunk Basic, GW-BASIC, MSX Basic and XBasic) |
Langurmonkey (talk | contribs) |
||
(10 intermediate revisions by 8 users not shown) | |||
Line 617:
150 CALL SWAP(A,B)
160 PRINT A,B</syntaxhighlight>
==={{header|Minimal BASIC}}===
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|IS-BASIC}}
{{works with|MSX_Basic}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">10 LET A = 1
20 LET B = 2
30 PRINT " A=";A;" B=";B
40 LET T = A
50 LET A = B
60 LET B = T
70 PRINT " A=";A;" B=";B
80 END</syntaxhighlight>
==={{header|MSX Basic}}===
Line 657 ⟶ 678:
CALL nswap(a, b)
PRINT a, b</syntaxhighlight>
==={{header|Quite BASIC}}===
The [[#Minimal _BASIC|Minimal BASIC]] solution works without any changes.
==={{header|Run BASIC}}===
Line 839 ⟶ 863:
B = [1, 2, 3, 4, 5]
</pre>
=={{header|Binary Lambda Calculus}}==
From https://github.com/tromp/AIT/blob/master/rosetta/swap.lam we get the 25-bit swap function
<pre>0001100000000101101101110</pre>
=={{header|BQN}}==
Line 1,565 ⟶ 1,593:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Generic_swap}}
'''Solution'''
Fōrmulæ supports assignments of several symbols (provided as a list) from an expression that reduces to a list of the same cardinality (the expression is first reduced before the actual assignment).
This can be used to do a generic swap as follows:
[[File:Fōrmulæ - Generic swap 01.png]]
'''Test case'''
[[File:Fōrmulæ - Generic swap 02.png]]
[[File:Fōrmulæ - Generic swap 03.png]]
=={{header|Gecho}}==
Line 1,829 ⟶ 1,867:
V2
cat</syntaxhighlight>
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn swap<T>(anon a: &mut T, anon b: &mut T) {
let temporary = *a
*a = *b
*b = temporary
}
fn main() {
mut a = "Hello"
mut b = "World"
println("{} {}", a, b)
swap(&mut a, &mut b)
println("{} {}", a, b)
mut c = 1
mut d = 2
println("{} {}", c, d)
swap(&mut c, &mut d)
println("{} {}", c, d)
}
</syntaxhighlight>
=={{header|Java}}==
Line 2,007 ⟶ 2,070:
=={{header|langur}}==
▲{{works with|langur|0.10}}
<syntaxhighlight lang="langur">var .abc = [1, 2, 3]
var .def = [5, 6, 7]
Line 2,017 ⟶ 2,079:
writeln .abc
writeln .def</syntaxhighlight>
{{out}}
Line 2,426 ⟶ 2,484:
swap(locals, "x", "y")
print "AFTER: x=" + x + ", y=" + y</syntaxhighlight>
{{out}}
<pre>BEFORE: x=1, y=2
AFTER: x=2, y=1</pre>
Line 3,622 ⟶ 3,680:
Both approaches are illustrated below.
<syntaxhighlight lang="
var a = 6
|