langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,2 @@
def coords = (1, -1);
def invcoords = Swap(coords);

View file

@ -0,0 +1 @@
a <-> b;

View file

@ -0,0 +1,4 @@
Swap[T, U] (a : T, b : U) : U * T
{
(b, a)
}

View file

@ -0,0 +1,2 @@
|reverse 1 2
=2 1

View file

@ -0,0 +1 @@
let swap (x, y) = (y, x)

View file

@ -0,0 +1,4 @@
let swapref x y =
let temp = !x in
x := !y;
y := temp

View file

@ -0,0 +1,6 @@
proc {SwapCells A B}
Tmp = @A
in
A := @B
B := Tmp
end

View file

@ -0,0 +1,3 @@
proc {SwapCells A B}
B := A := @B
end

View file

@ -0,0 +1,3 @@
fun {SwapPair A#B}
B#A
end

View file

@ -0,0 +1,3 @@
my(tmp=a);
a=b;
b=tmp;

View file

@ -0,0 +1 @@
[a,b]=[b,a]

View file

@ -0,0 +1,11 @@
%swap: procedure (a, b);
declare (a, b) character;
return ( 't=' || a || ';' || a || '=' || b || ';' || b '=t;' );
%end swap;
%activate swap;
The statement:-
swap (p, q);
is replaced, at compile time, by the three statements:
t = p; p = q; q = t;

View file

@ -0,0 +1,16 @@
declare swap generic (
swapf when (float, float),
swapc when (char, char));
swapf: proc (a, b);
declare (a, b, t) float;
t = a; a = b; b = t;
end swapf;
swapc: proc (a, b);
declare (a, b) character(*);
declare t character (length(b));
t = a; a = b; b = t;
end swapc;
declare (r, s) character (5);
call swap (r, s);

View file

@ -0,0 +1,36 @@
%swap: proc(x,y);
dcl (x, y) char;
x = trim(x); /* Just for neatness sake */
y = trim(y);
ans('begin; ') skip;
ans(' dcl c char (1); ') skip;
ans(' dcl sx char (1) based(px); ') skip;
ans(' dcl sy char (1) based(py); ') skip;
ans(' dcl i fixed bin (31); ') skip;
ans(' dcl px ptr init (addr(' || x || ')); ') skip;
ans(' dcl py ptr init (addr(' || y || ')); ') skip;
ans(' do i = 1 to min(stg(' || x || '), stg(' || y || '));') skip;
ans(' c = sx; ') skip;
ans(' sx = sy; ') skip;
ans(' sy = c; ') skip;
ans(' px = px + 1; ') skip;
ans(' py = py + 1; ') skip;
ans(' end; ') skip;
ans('end; ') skip;
%end swap;
%act swap;
dcl c1 char (10) init ('1234567890');
dcl c2 char (10) init ('ABCDEFGHIJ');
dcl f1 fixed bin (31) init (12345);
dcl f2 fixed bin (31) init (98765);
put data(c1, c2, f1, f2);
swap(c1, c2);
swap(f1, f2);
put data(c1, c2, f1, f2);
f1 = -656877352; /* '5a5a5a5a'x, aka 'QQQQ' */
swapper(c1, f1);
put data(c1,f1);

View file

@ -0,0 +1,15 @@
begin;
dcl c char (1);
dcl sx char (1) based(px);
dcl sy char (1) based(py);
dcl i fixed bin (31);
dcl px ptr init (addr(C1));
dcl py ptr init (addr(C2));
do i = 1 to min(stg(C1), stg(C2));
c = sx;
sx = sy;
sy = c;
px = px + 1;
py = py + 1;
end;
end;

View file

@ -0,0 +1,28 @@
program generictest;
{$mode objfpc}
type
generic TSwap<T> = procedure (var a, b: T);
procedure Proc1(var a, b: integer);
var
temp: integer;
begin
temp := a;
a := b;
b := temp;
end;
var
S, T: integer;
SwapInt: specialize TSwap<integer>;
begin
S := 4;
T := 3;
SwapInt := @Proc1;
writeln(S, T:2);
SwapInt(S, T);
writeln(S, T:2);
end.

View file

@ -0,0 +1 @@
($x, $y) .= reverse;

View file

@ -0,0 +1 @@
(a, b) -> (b, a);

View file

@ -0,0 +1 @@
exch

View file

@ -0,0 +1 @@
$b, $a = $a, $b

View file

@ -0,0 +1,3 @@
function swap ([ref] $a, [ref] $b) {
$a.Value, $b.Value = $b.Value, $a.Value
}

View file

@ -0,0 +1 @@
swap ([ref] $a) ([ref] $b)

View file

@ -0,0 +1 @@
Swap a, b

View file

@ -0,0 +1,20 @@
REBOL [
Title: "Generic Swap"
Author: oofoe
Date: 2009-12-06
URL: http://rosettacode.org/wiki/Generic_swap
Reference: [http://reboltutorial.com/blog/rebol-words/]
]
swap: func [
"Swap contents of variables."
a [word!] b [word!] /local x
][
x: get a
set a get b
set b x
]
answer: 42 ship: "Heart of Gold"
swap 'answer 'ship ; Note quoted variables.
print rejoin ["The answer is " answer ", the ship is " ship "."]

View file

@ -0,0 +1,22 @@
swap = function(x,y)
{
if (!exist($$.[x]))
{ return 0; }
if (!exist($$.[y]))
{ return 0; }
local (t);
t = $$.[x];
$$.[x] = $$.[y];
$$.[y] = t;
return 1;
};
>> a=1
1
>> b = "fish"
fish
>> swap( "a" , "b" );
>> a
fish
>> b
1

View file

@ -0,0 +1 @@
swap

View file

@ -0,0 +1,7 @@
a = 1
b = 2
'----- swap ----
tmp = a
a = b
b = tmp
end

View file

@ -0,0 +1,9 @@
* SWAP(.V1, .V2) - Exchange the contents of two variables.
* The variables must be prefixed with the name operator
* when the function is called.
DEFINE('SWAP(X,Y)TEMP') :(SWAP_END)
SWAP TEMP = $X
$X = $Y
$Y = TEMP :(RETURN)
SWAP_END

View file

@ -0,0 +1,13 @@
const proc: generate_swap (in type: aType) is func
begin
const proc: swap (inout aType: left, inout aType: right) is func
local
var aType: temp is aType.value;
begin
temp := left;
left := right;
right := temp;
end func;
end func;

View file

@ -0,0 +1,2 @@
generate_swap(integer);
generate_swap(string);

View file

@ -0,0 +1 @@
swap(a, b);

View file

@ -0,0 +1,10 @@
x@(Syntax LoadVariable traits) swapWith: y@(Syntax LoadVariable traits) &environment: env
"A macro that expands into simple code swapping the values of two variables
in the current scope."
[
env ifNil: [error: 'Cannot swap variables outside of a method'].
tmpVar ::= env addVariable.
{tmpVar store: x variable load.
x variable store: y variable load.
y variable store: tmpVar load} parenthesize
].

View file

@ -0,0 +1 @@
a `swapWith: b

View file

@ -0,0 +1 @@
fun swap (x, y) = (y, x)

View file

@ -0,0 +1,2 @@
fun swapref (x, y) =
let temp = !x in x := !y; y := temp end

View file

@ -0,0 +1,14 @@
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

View file

@ -0,0 +1 @@
swap

View file

@ -0,0 +1,7 @@
pmgs("x","y") = ("y","x") # the pattern matching way
ugs = ~&rlX # the idiosyncratic Ursala way
#cast %sWL
test = <pmgs ('a','b'),ugs ('x','y')>

View file

@ -0,0 +1,5 @@
[swap [a b : b a] view].
1 2 swap
= 2 1
'hello' 'hi' swap

View file

@ -0,0 +1,6 @@
sub swap( byref x, byref y )
dim temp
temp = x
x = y
y = temp
end sub

View file

@ -0,0 +1,7 @@
dim a
a = "woof"
dim b
b = now()
swap a,b
wscript.echo a
wscript.echo b

View file

@ -0,0 +1,2 @@
5/02/2010 2:35:36 PM
woof

View file

@ -0,0 +1,14 @@
include c:\cxpl\codes;
proc Exch(A, B, S);
char A, B, S;
int I, T;
for I:= 0 to S-1 do
[T:= A(I); A(I):= B(I); B(I):= T];
real X, Y;
[X:= 3.0; Y:= 4.0;
Exch(addr X, addr Y, 8);
RlOut(0, X); RlOut(0, Y); CrLf(0);
]