2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,4 +1,6 @@
The task is to write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types.
;Task:
Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types.
If your solution language is statically typed please describe the way your language provides genericity.
If variables are typed in the given language, it is permissible that the two variables be constrained to having a mutually compatible type, such that each is permitted to hold the value previously stored in the other without a type violation.
@ -13,3 +15,4 @@ Functional languages, whether static or dynamic, do not necessarily allow a dest
Some static languages have difficulties with generic programming due to a lack of support for ([[Parametric Polymorphism]]).
Do your best!
<br><br>

View file

@ -0,0 +1,20 @@
SWAP CSECT , control section start
BAKR 14,0 stack caller's registers
LR 12,15 entry point address to reg.12
USING SWAP,12 use as base
MVC A,=C'5678____' init field A
MVC B,=C'____1234' init field B
LA 2,L address of length field in reg.2
WTO TEXT=(2) Write To Operator, results in:
* +5678________1234
XC A,B XOR A,B
XC B,A XOR B,A
XC A,B XOR A,B. A holds B, B holds A
WTO TEXT=(2) Write To Operator, results in:
* +____12345678____
PR , return to caller
LTORG , literals displacement
L DC H'16' halfword containg decimal 16
A DS CL8 field A, 8 bytes
B DS CL8 field B, 8 bytes
END SWAP program end

View file

@ -1,9 +1,9 @@
generic
type Swap_Type is private; -- Generic parameter
procedure Generic_Swap(Left : in out Swap_Type; Right : in out Swap_Type);
procedure Generic_Swap (Left, Right : in out Swap_Type);
procedure Generic_Swap(Left : in out Swap_Type; Right : in out Swap_Type) is
Temp : Swap_Type := Left;
procedure Generic_Swap (Left, Right : in out Swap_Type) is
Temp : constant Swap_Type := Left;
begin
Left := Right;
Right := Temp;

View file

@ -1,7 +1,7 @@
with Generic_Swap;
...
type T is ...
package T_Swap is new Generic_Swap(Swap_Type => T);
A,B:T;
procedure T_Swap is new Generic_Swap (Swap_Type => T);
A, B : T;
...
T_Swap(A,B);
T_Swap (A, B);

View file

@ -3,9 +3,3 @@
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,4 @@
%Swap:Procedure(a,b);
declare (a,b) character; /*These are proper strings of arbitrary length, pre-processor only.*/
return ('Begin; declare t like '|| a ||'; t='|| a ||';'|| a ||'='|| b ||';'|| b ||'=t; End;');
%End Swap;

View file

@ -0,0 +1,6 @@
Begin;
declare t like this;
t = this;
this = that;
that = t;
End;

View file

@ -0,0 +1,5 @@
(defmacro swp (left right)
(with-gensyms (tmp)
^(let ((,tmp ,left))
(set ,left ,right
,right ,tmp))))

View file

@ -0,0 +1,7 @@
(defmacro swp (left right)
(with-gensyms (tmp lpl rpl)
^(placelet ((,lpl ,left)
(,rpl ,right))
(let ((,tmp ,lpl))
(set ,lpl ,rpl
,rpl ,tmp)))))

View file

@ -0,0 +1,7 @@
(defmacro swp (left right :env env)
(with-gensyms (tmp)
(with-update-expander (l-getter l-setter) left env
(with-update-expander (r-getter r-setter) right env
^(let ((,tmp (,l-getter)))
(,l-setter (,r-getter))
(,r-setter ,tmp))))))