Generic swap: Difference between revisions

Dialects of BASIC moved to the BASIC section.
No edit summary
(Dialects of BASIC moved to the BASIC section.)
Line 432:
a 1 ng
</pre>
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="applesoft basic">A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;</syntaxhighlight>
<pre> A=47 B=43</pre>
 
=={{header|Axe}}==
Line 440 ⟶ 437:
<syntaxhighlight lang="axe">Exch(°A,°B,2)</syntaxhighlight>
 
=={{header|Batch FileBASIC}}==
==={{header|Applesoft BASIC}}===
 
<syntaxhighlight lang="applesoft basic">A=43:B=47:H=A:A=B:B=H:?" A="A" B="B;</syntaxhighlight>
Swap using pass-by-name
<pre> A=47 B=43</pre>
 
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
set a=1
set b=woof
echo %a%
echo %b%
call :swap a b
echo %a%
echo %b%
goto :eof
 
:swap
set temp1=!%1!
set temp2=!%2!
set %1=%temp2%
set %2=%temp1%
goto :eof</syntaxhighlight>
 
 
 
=={{header|BaCon}}==
 
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">
x = 1
Line 474 ⟶ 451:
</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">global a, b
a = "one"
Line 488 ⟶ 465:
end subroutine</syntaxhighlight>
 
==={{header|BBC BASIC}}===
====Built-in function====
<syntaxhighlight lang="bbcbasic"> a = 1.23 : b = 4.56
SWAP a,b
Line 497 ⟶ 474:
SWAP a$,b$
PRINT a$,b$</syntaxhighlight>
====Custom function====
<syntaxhighlight lang="bbcbasic"> a = 1.23 : b = 4.56
PROCswap(^a,^b, 5)
Line 518 ⟶ 495:
world! Hello
</pre>
 
==={{header|FreeBASIC}}===
FreeBASIC already has a built-in generic Swap procedure but a macro can be used to build another one:
<syntaxhighlight lang="freebasic">' FB 1.05.0
#Macro Declare_Swap(T)
Sub Swap_##T(ByRef t1 As T, ByRef t2 As T)
Dim temp As T = t2
t2 = t1
t1 = temp
End Sub
#EndMacro
 
Dim As Integer i, j
i = 1 : j = 2
 
Declare_Swap(Integer) ' expands the macro
Swap_Integer(i, j)
Print i, j
 
Dim As String s, t
s = "Hello" : t = "World"
 
Declare_Swap(String)
Swap_String(s, t)
Print s, t
 
Print
Print "Press any key to exit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
2 1
World Hello
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Generic Swap", (0,0,480,270)
 
text ,,,,, 60
 
long i, j
double x, y
CFStringRef a, b
 
i = 1059 : j = 62
print i, j
swap i, j
print i, j
 
print
 
x = 1.23 : y = 4.56
print x, y
swap x, y
print x, y
 
print
 
a = @"Hello" : b = @"World!"
print a, b
swap a, b
print a, b
 
HandleEvents
</syntaxhighlight>
 
{{output}}
<pre>
1059 62
62 1059
 
1.23 4.56
4.56 1.23
 
Hello World!
World! Hello
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=b746e857f6f280fb92c204795f6053be Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim vA As Variant = " World"
Dim vB As Variant = 1
 
Swap vA, vB
 
Print vA; vB
 
End</syntaxhighlight>
Output:
<pre>
1 World
</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 DEF SWAP(REF A,REF B)
110 LET T=A:LET A=B:LET B=T
120 END DEF
130 LET A=1:LET B=2
140 PRINT A,B
150 CALL SWAP(A,B)
160 PRINT A,B</syntaxhighlight>
 
==={{header|OxygenBasic}}===
 
<syntaxhighlight lang="text">
macro Swap(a,b, c)
typeof(a) c
c=a
a=b
b=c
end macro
 
 
'demo with compound types:
'=========================
type point { float x,y}
point p={1,2}
point q={3,4}
swap p,q
print "p: " p.x "," p.y 'p: 3,4
print "q: " q.x "," q.y 'q: 1,2
</syntaxhighlight>
 
==={{header|PureBasic}}===
Built in function:
<syntaxhighlight lang="purebasic">Swap a, b</syntaxhighlight>
 
==={{header|QBasic}}===
QBasic already has a generic swap procedure built in, but a new subroutine can be defined:
<syntaxhighlight lang="qbasic">SUB nswap (a, b)
temp = a: a = b: b = temp
END SUB
 
a = 1
b = 2
 
PRINT a, b
CALL nswap(a, b)
PRINT a, b</syntaxhighlight>
 
==={{header|Run BASIC}}===
Run BASIC does not have support for swapping built in:
<syntaxhighlight lang="runbasic">a = 1
b = 2
'----- swap ----
tmp = a
a = b
b = tmp
end</syntaxhighlight>
 
==={{header|ThinBASIC}}===
Generic function, swap the content of two variables.
<syntaxhighlight lang="thinbasic">Swap Var1, Var2</syntaxhighlight>
 
==={{header|TI-89 BASIC}}===
TI-89 BASIC is dynamically typed, so the genericity is implicit. It has no pass by reference, so we must pass the variable names as strings. It is dynamically scoped, so we must choose hopefully distinct names for the variables.
 
<syntaxhighlight lang="ti89b">Define swap(swapvar1, swapvar2) = Prgm
Local swaptmp
#swapvar1 → swaptmp
#swapvar2 → #swapvar1
swaptmp → #swapvar2
EndPrgm
 
1 → x
2 → y
swap("x", "y")
x
2
y
1</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="tiny basic"> LET a = 11
LET b = 22
PRINT a, " ", b
GOSUB 100
PRINT a, " ", b
END
100 REM swap(a, b)
LET t = a
LET a = b
LET b = t
RETURN</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">SUB swap(a, b)
LET temp = a
LET a = b
LET b = temp
END SUB
 
LET a = 1
LET b = 2
 
PRINT a, b
CALL swap(a, b)
PRINT a, b
END</syntaxhighlight>
 
==={{header|uBasic/4tH}}===
Since uBasic/4tH has a stack (just like [[Forth]]) and it is an integer BASIC only, this is quite trivial. However, making a function or procedure with the same functionality is impossible, because there is no way to pass variables by reference.
<syntaxhighlight lang="text">a = 5 : b = 7
Print a,b
Push a,b : a = Pop() : b = Pop()
Print a,b</syntaxhighlight>
 
==={{header|VBScript}}===
This works for everything: strings, dates, booleans ... The fact is, with everything being a Variant, it's always generic.
 
<syntaxhighlight lang="vb">sub swap( byref x, byref y )
dim temp
temp = x
x = y
y = temp
end sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vb">dim a
a = "woof"
dim b
b = now()
swap a,b
wscript.echo a
wscript.echo b</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="vb">5/02/2010 2:35:36 PM
woof</syntaxhighlight>
 
==={{header|Visual Basic}}===
Visual Basic can use the [[#VBScript|VBScript]] example above, with the caveat that it won't work if any <code>DEFtype</code> (except <code>DefVar</code>) has been used. (The default data type is <code>Variant</code>, which can be used as a stand-in for any variable type.)
 
Also, the sub will fail if one arg is a string containing non-numeric data and the other arg is numeric.
 
==={{header|Visual Basic .NET}}===
Semantically identical to [[#C#|C#]]
<syntaxhighlight lang="vbnet">Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim temp = a
a = b
b = temp
End Sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vbnet">Dim a = 1, b = 2
Swap(a, b)</syntaxhighlight>
 
==={{header|Yabasic}}===
Yabasic already has a generic swap procedure built in.
<syntaxhighlight lang="yabasic">a = 1
b = 2
 
print a, b
//----- swap ----
temp = a : a = b : b = temp
print a, b</syntaxhighlight>
 
=={{header|Batch File}}==
 
Swap using pass-by-name
 
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
set a=1
set b=woof
echo %a%
echo %b%
call :swap a b
echo %a%
echo %b%
goto :eof
 
:swap
set temp1=!%1!
set temp2=!%2!
set %1=%temp2%
set %2=%temp1%
goto :eof</syntaxhighlight>
 
=={{header|Beads}}==
Line 1,253 ⟶ 1,511:
Test me
me Test</pre>
 
=={{header|FreeBASIC}}==
FreeBASIC already has a built-in generic Swap procedure but a macro can be used to build another one:
<syntaxhighlight lang="freebasic">' FB 1.05.0
#Macro Declare_Swap(T)
Sub Swap_##T(ByRef t1 As T, ByRef t2 As T)
Dim temp As T = t2
t2 = t1
t1 = temp
End Sub
#EndMacro
 
Dim As Integer i, j
i = 1 : j = 2
 
Declare_Swap(Integer) ' expands the macro
Swap_Integer(i, j)
Print i, j
 
Dim As String s, t
s = "Hello" : t = "World"
 
Declare_Swap(String)
Swap_String(s, t)
Print s, t
 
Print
Print "Press any key to exit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
2 1
World Hello
</pre>
 
=={{header|Frink}}==
Line 1,295 ⟶ 1,518:
[b,a] = [a,b]
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1, @"Generic Swap", (0,0,480,270)
 
text ,,,,, 60
 
long i, j
double x, y
CFStringRef a, b
 
i = 1059 : j = 62
print i, j
swap i, j
print i, j
 
print
 
x = 1.23 : y = 4.56
print x, y
swap x, y
print x, y
 
print
 
a = @"Hello" : b = @"World!"
print a, b
swap a, b
print a, b
 
HandleEvents
</syntaxhighlight>
 
{{output}}
<pre>
1059 62
62 1059
 
1.23 4.56
4.56 1.23
 
Hello World!
World! Hello
</pre>
 
=={{header|Fōrmulæ}}==
Line 1,346 ⟶ 1,526:
 
In '''[https://formulae.org/?example=Generic_swap this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=b746e857f6f280fb92c204795f6053be Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim vA As Variant = " World"
Dim vB As Variant = 1
 
Swap vA, vB
 
Print vA; vB
 
End</syntaxhighlight>
Output:
<pre>
1 World
</pre>
 
=={{header|Gecho}}==
Line 1,590 ⟶ 1,754:
b = temporary(c)
end</syntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 DEF SWAP(REF A,REF B)
110 LET T=A:LET A=B:LET B=T
120 END DEF
130 LET A=1:LET B=2
140 PRINT A,B
150 CALL SWAP(A,B)
160 PRINT A,B</syntaxhighlight>
 
=={{header|J}}==
Line 2,348 ⟶ 2,503:
 
<syntaxhighlight lang="oforth">swap</syntaxhighlight>
 
=={{header|OxygenBasic}}==
 
<syntaxhighlight lang="text">
macro Swap(a,b, c)
typeof(a) c
c=a
a=b
b=c
end macro
 
 
'demo with compound types:
'=========================
type point { float x,y}
point p={1,2}
point q={3,4}
swap p,q
print "p: " p.x "," p.y 'p: 3,4
print "q: " q.x "," q.y 'q: 1,2
</syntaxhighlight>
 
=={{header|Oz}}==
Line 2,772 ⟶ 2,906:
Y = 1.
</syntaxhighlight>
 
=={{header|PureBasic}}==
Built in function:
<syntaxhighlight lang="purebasic">Swap a, b</syntaxhighlight>
 
=={{header|Python}}==
Line 2,788 ⟶ 2,918:
return b, a</syntaxhighlight>
Note that tuples are immutable in Python. This function doesn't mutate anything, but simply returns a new pair with the order of the elements switched.
 
=={{header|QBasic}}==
QBasic already has a generic swap procedure built in, but a new subroutine can be defined:
<syntaxhighlight lang="qbasic">SUB nswap (a, b)
temp = a: a = b: b = temp
END SUB
 
a = 1
b = 2
 
PRINT a, b
CALL nswap(a, b)
PRINT a, b</syntaxhighlight>
 
=={{header|Quackery}}==
Line 3,021 ⟶ 3,138:
puts x # prints string
puts y # prints 42</syntaxhighlight>
 
=={{header|Run BASIC}}==
 
Run BASIC does not have support for swapping built in:
<syntaxhighlight lang="runbasic">a = 1
b = 2
'----- swap ----
tmp = a
a = b
b = tmp
end</syntaxhighlight>
 
=={{header|Rust}}==
Line 3,307 ⟶ 3,413:
<pre>before a=1 b=2
after a=2 b=1</pre>
 
=={{header|ThinBASIC}}==
Generic function, swap the content of two variables.
<syntaxhighlight lang="thinbasic">Swap Var1, Var2</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
TI-89 BASIC is dynamically typed, so the genericity is implicit. It has no pass by reference, so we must pass the variable names as strings. It is dynamically scoped, so we must choose hopefully distinct names for the variables.
 
<syntaxhighlight lang="ti89b">Define swap(swapvar1, swapvar2) = Prgm
Local swaptmp
#swapvar1 → swaptmp
#swapvar2 → #swapvar1
swaptmp → #swapvar2
EndPrgm
 
1 → x
2 → y
swap("x", "y")
x
2
y
1</syntaxhighlight>
 
=={{Header|Tiny BASIC}}==
<syntaxhighlight lang="tiny basic"> LET a = 11
LET b = 22
PRINT a, " ", b
GOSUB 100
PRINT a, " ", b
END
100 REM swap(a, b)
LET t = a
LET a = b
LET b = t
RETURN</syntaxhighlight>
 
=={{header|Trith}}==
As with other stack-based languages (e.g. [[Factor]] and [[Joy]]), the solution to this task is a trivial matter of swapping the top two operands on the stack:
<syntaxhighlight lang="trith">swap</syntaxhighlight>
 
=={{header|True BASIC}}==
<syntaxhighlight lang="qbasic">SUB swap(a, b)
LET temp = a
LET a = b
LET b = temp
END SUB
 
LET a = 1
LET b = 2
 
PRINT a, b
CALL swap(a, b)
PRINT a, b
END</syntaxhighlight>
 
=={{header|TXR}}==
Line 3,405 ⟶ 3,459:
 
<code>with-update-expander</code> is a macro which writes code for accessing and updating a place, and makes that code available as local macros. The result is wrapped around the body of code passed to the macro; the body can access these functions, using a backquote to insert the symbols which refer to them. For instance the macro call <code>(,l-getter)</code> expands to code which accesses the prior value of the <code>left</code> place, and <code>(,r-setter ,tmp)</code> stores the value of the temporary variable into the <code>right</code> place.
 
=={{header|uBasic/4tH}}==
Since uBasic/4tH has a stack (just like [[Forth]]) and it is an integer BASIC only, this is quite trivial. However, making a function or procedure with the same functionality is impossible, because there is no way to pass variables by reference.
<syntaxhighlight lang="text">a = 5 : b = 7
Print a,b
Push a,b : a = Pop() : b = Pop()
Print a,b</syntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 3,463 ⟶ 3,510:
='hi' 'hello'
 
=={{header|VBScript}}==
This works for everything: strings, dates, booleans ... The fact is, with everything being a Variant, it's always generic.
 
<syntaxhighlight lang="vb">sub swap( byref x, byref y )
dim temp
temp = x
x = y
y = temp
end sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vb">dim a
a = "woof"
dim b
b = now()
swap a,b
wscript.echo a
wscript.echo b</syntaxhighlight>
 
{{out}}
<syntaxhighlight lang="vb">5/02/2010 2:35:36 PM
woof</syntaxhighlight>
 
=={{header|Verbexx}}==
Line 3,507 ⟶ 3,532:
 
@SAY "a=" a " b=" b;</syntaxhighlight>
 
=={{header|Visual Basic}}==
 
Visual Basic can use the [[#VBScript|VBScript]] example above, with the caveat that it won't work if any <code>DEFtype</code> (except <code>DefVar</code>) has been used. (The default data type is <code>Variant</code>, which can be used as a stand-in for any variable type.)
 
Also, the sub will fail if one arg is a string containing non-numeric data and the other arg is numeric.
 
=={{header|Visual Basic .NET}}==
Semantically identical to [[#C#|C#]]
<syntaxhighlight lang="vbnet">Sub Swap(Of T)(ByRef a As T, ByRef b As T)
Dim temp = a
a = b
b = temp
End Sub</syntaxhighlight>
 
Usage:
<syntaxhighlight lang="vbnet">Dim a = 1, b = 2
Swap(a, b)</syntaxhighlight>
 
Line 3,676 ⟶ 3,683:
4.00000 3.00000
</pre>
 
=={{header|Yabasic}}==
Yabasic already has a generic swap procedure built in.
<syntaxhighlight lang="yabasic">a = 1
b = 2
 
print a, b
//----- swap ----
temp = a : a = b : b = temp
print a, b</syntaxhighlight>
 
=={{header|Yorick}}==
511

edits