Generic swap: Difference between revisions

(Added Chipmunk Basic, GW-BASIC, MSX Basic and XBasic)
 
(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|langur|0.10QBasic}}
{{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}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
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).
In '''[https://formulae.org/?example=Generic_swap this]''' page you can see the program(s) related to this task and their results.
 
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}}==
Langur does not have an option for using references (so far). The following is not directly applicable to the task, but valuesValues can be swapped using multi-variable assignment, including indexed values (for mutable variables).
 
{{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>
 
Prior to 0.10, you would use parentheses as follows.
 
<syntaxhighlight lang="langur">(.abc[3], .def[3]) = (.def[3], .abc[3])</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="ecmascriptwren">var swap = Fn.new { |l| l.swap(0, 1) }
 
var a = 6
885

edits