Anonymous user
Parametric polymorphism: Difference between revisions
m
Fixed lang tags.
(Added Scala) |
m (Fixed lang tags.) |
||
Line 7:
=={{header|Ada}}==
<lang ada>generic
▲ type Element_Type is private;
▲ package Container is
▲ type Tree is tagged private;
▲ procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
type Node;
▲ private
Value : Element_type;
Left
▲ end record;
The_Tree.Value := New_Value;
▲ procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type) is
▲ begin
end
if
▲ if The_tree.Right /= null then
end Container;</lang>
▲ The_Tree.Right.all.Replace_All(New_Value);
▲ end Replace_All;
▲ end Container;
=={{header|C++}}==
<lang cpp>
For simplicity, we replace all values in the tree with a new value:
<lang cpp>
=={{header|Clean}}==
Line 118 ⟶ 114:
}</lang>
This is a using example.
<lang d>module polytest ;
import std.stdio ;
import parapoly ;
Line 219 ⟶ 214:
=={{header|Java}}==
Following the C++ example:
<lang java>
=={{header|OCaml}}==
<lang ocaml>
=={{header|Scala}}==
Line 322 ⟶ 317:
=={{header|Standard ML}}==
<lang sml>
=={{header|Ursala}}==
Line 332 ⟶ 327:
routinely. A parameterized binary tree type can be defined using a syntax for anonymous
recursion in type expressions as in this example,
<lang Ursala>binary_tree_of "node-type" = "node-type"%hhhhWZAZ</lang>▼
▲binary_tree_of "node-type" = "node-type"%hhhhWZAZ
or by way of a recurrence solved using a fixed point combinator imported from a library
as shown below.
<lang Ursala>#import tag
#fix general_type_fixer 1
binary_tree_of "node-type" = ("node-type",(binary_tree_of "node-type")%Z)%drWZwlwAZ</lang>
(The %Z type operator constructs a "maybe" type, i.e., the free union of its operand type
with the null value. Others shown above are standard stack manipulation primitives, e.g. d (dup) and w (swap), used to build the type expression tree.) At the other extreme, one may construct an equivalent parameterized type in
point-free form.
<lang Ursala>binary_tree_of = %-hhhhWZAZ</lang>
A mapping combinator over this type can be defined with pattern matching like this
<lang Ursala>binary_tree_map "f" = ~&a^& ^A/"f"@an ~&amPfamPWB</lang>▼
▲binary_tree_map "f" = ~&a^& ^A/"f"@an ~&amPfamPWB
or in point free form like this.
<lang Ursala>binary_tree_map = ~&a^&+ ^A\~&amPfamPWB+ @an</lang>
Here is a test program
defining a type of binary trees of strings, and a function that concatenates each node
with itself.
<lang Ursala>string_tree = binary_tree_of %s
x = 'foo': ('bar': (),'baz': ())
Line 368 ⟶ 352:
#cast string_tree
example = (binary_tree_map "s". "s"--"s") x</lang>
Type signatures are not associated with function declarations, but
have uses in the other contexts such as assertions and compiler directives
|