Parametric polymorphism: Difference between revisions

Content added Content deleted
(Added Scala)
m (Fixed lang tags.)
Line 7:
 
=={{header|Ada}}==
<lang ada>generic
type Element_Type is private;
generic
package Container is
type Element_Type is private;
type Tree is tagged private;
package Container is
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
type Tree is tagged private;
private
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
type Node;
private
type Node_Access is access Node;
type Node_Access isTree accesstagged Node;record
Value : Element_type;
type Tree tagged record
Left Value : Element_typeNode_Access := null;
Left Right : Node_Access := null;
end record;
Right : Node_Access := null;
end Container;</lang>
end record;
end<lang ada>package body Container; is
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type) is
</lang>
begin
<lang ada>
The_Tree.Value := New_Value;
package body Container is
If if The_treeThe_Tree.RightLeft /= null then
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type) is
The_Tree.RightLeft.all.Replace_All(New_Value);
begin
end The_Tree.Value := New_Valueif;
if If The_TreeThe_tree.LeftRight /= null then
The_Tree.LeftRight.all.Replace_All(New_Value);
end if;
end Replace_All;
if The_tree.Right /= null then
end Container;</lang>
The_Tree.Right.all.Replace_All(New_Value);
end if;
end Replace_All;
end Container;
</lang>
 
=={{header|C++}}==
 
<lang cpp> template<class T>
class tree
{
T value;
tree *left;
tree *right;
public:
void replace_all (T new_value);
};</lang>
 
For simplicity, we replace all values in the tree with a new value:
 
<lang cpp> template<class T>
void tree<T>::replace_all (T new_value)
{
value = new_value;
left->replace_all (new_value);
right->replace_all (new_value);
}</lang>
 
=={{header|Clean}}==
Line 118 ⟶ 114:
}</lang>
This is a using example.
<lang d>module polytest ;
module polytest ;
import std.stdio ;
import parapoly ;
Line 219 ⟶ 214:
=={{header|Java}}==
Following the C++ example:
<lang java> public class Tree<T>{
private T value;
private Tree<T> left;
private Tree<T> right;
 
public void replaceAll(T value){
this.value = value;
if(left != null)
left.replaceAll(value);
if(right != null)
right.replaceAll(value);
}
}</lang>
 
=={{header|OCaml}}==
 
<lang ocaml> type 'a tree = Empty | Node of 'a * 'a tree * 'a tree
 
(** val map_tree : ('a -> 'b) -> 'a tree -> 'b tree *)
let rec map_tree f = function
| Empty -> Empty
| Node (x,l,r) -> Node (f x, map_tree f l, map_tree f r)</lang>
 
=={{header|Scala}}==
Line 322 ⟶ 317:
=={{header|Standard ML}}==
 
<lang sml> datatype 'a tree = Empty | Node of 'a * 'a tree * 'a tree
 
(** val map_tree = fn : ('a -> 'b) -> 'a tree -> 'b tree *)
fun map_tree f Empty = Empty
| map_tree f (Node (x,l,r)) = Node (f x, map_tree f l, map_tree f r)</lang>
 
=={{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>
<lang Ursala>
binary_tree_of "node-type" = "node-type"%hhhhWZAZ
</lang>
or by way of a recurrence solved using a fixed point combinator imported from a library
as shown below.
<lang Ursala>#import tag
#import tag
 
#fix general_type_fixer 1
 
binary_tree_of "node-type" = ("node-type",(binary_tree_of "node-type")%Z)%drWZwlwAZ</lang>
</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>
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>
<lang Ursala>
binary_tree_map "f" = ~&a^& ^A/"f"@an ~&amPfamPWB
</lang>
or in point free form like this.
<lang Ursala>binary_tree_map = ~&a^&+ ^A\~&amPfamPWB+ @an</lang>
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
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>
</lang>
Type signatures are not associated with function declarations, but
have uses in the other contexts such as assertions and compiler directives