Parametric polymorphism: Difference between revisions

Added Prolog
m (syntax highlighting fixup automation)
(Added Prolog)
 
(4 intermediate revisions by 4 users not shown)
Line 199:
=={{header|C++}}==
 
<syntaxhighlight lang="cpp">template<classtypename T>
class tree {
{
T value;
tree *left;
Line 212 ⟶ 211:
 
<syntaxhighlight lang="cpp">template<class T>
void tree<T>::replace_all (T new_value) {
{
value = new_value;
if (left != NULLnullptr)
left->replace_all (new_value);
if (right != NULLnullptr)
right->replace_all (new_value);
}</syntaxhighlight>
 
=={{header|C3}}==
<syntaxhighlight lang="c3">module tree(<Type>);
 
struct Tree
Line 231 ⟶ 229:
}
 
fn void Tree.replaceAllreplace_all(Tree* a_tree&self, Type new_value)
{
a_treeself.value = new_value;
if (a_treeself.left) a_treeself.left.replaceAllreplace_all(new_value);
if (a_treeself.right) a_treeself.right.replaceAllreplace_all(new_value);
}
</syntaxhighlight>
 
Usage:
To use a parametric type, the type must be defined first:
<syntaxhighlight lang="c3">define IntTree =import tree<int>::Tree;
 
<syntaxhighlight lang="c3">define IntTree = tree<int>::Tree;
 
fn void test()
{
IntTreeTree(<int>) inttree;
inttree.replaceAllreplace_all(3);
}</syntaxhighlight>
 
Line 496 ⟶ 493:
# value: <tree></syntaxhighlight>
 
=={{header|F_Sharp|F#FreeBASIC}}==
{{trans|Visual Basic .NET}}
FreeBASIC does not support object-oriented programming, so we will use a more procedural approach.
<syntaxhighlight lang="vbnet">Type BinaryTree
valor As Integer
izda As BinaryTree Ptr
dcha As BinaryTree Ptr
End Type
 
Sub PrintTree(t As BinaryTree Ptr, depth As Integer)
If t = 0 Then Exit Sub
Print String(depth, Chr(9)); t->valor
PrintTree(t->izda, depth + 1)
PrintTree(t->dcha, depth + 1)
End Sub
 
Dim As BinaryTree b = Type(6)
Dim As BinaryTree bLeft = Type(5)
Dim As BinaryTree bRight = Type(7)
b.izda = @bLeft
b.dcha = @bRight
 
PrintTree(@b, 0)</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
namespace RosettaCode
Line 1,142 ⟶ 1,162:
-> NIL</pre>
 
=={{header|RacketProlog}}==
{{works with|SWI Prolog}}
SWI-Prolog does not support object-oriented programming, but we can simulate it.
<syntaxhighlight lang="prolog">% Tree Definition
tree(leaf(_)).
tree(branch(Left, Right)) :- tree(Left), tree(Right).
 
% Definition of the addone function
Typed Racket has parametric polymorphism:
addone(X, Y) :- Y is X + 1.
 
% Definition of treewalk
treewalk(leaf(Value), Func, leaf(NewValue)) :- call(Func, Value, NewValue).
treewalk(branch(Left, Right), Func, branch(NewLeft, NewRight)) :-
treewalk(Left, Func, NewLeft),
treewalk(Right, Func, NewRight).
 
% Execution
run :-
X = branch(leaf(2), branch(leaf(3),leaf(4))),
treewalk(X, addone, Y),
write(Y).</syntaxhighlight>
 
=={{header|Racket}}==
Typed Racket has parametric polymorphism:
<syntaxhighlight lang="racket">
#lang typed/racket
Line 1,578 ⟶ 1,618:
 
Fortunately, we can simulate PP by passing an extra parameter to a collection class's contructor to specify the type of values to be used for that particular instantiation. We can then use this to guard against the wrong type of values being passed to the class's other methods.
<syntaxhighlight lang="ecmascriptwren">class BinaryTree {
construct new(T, value) {
if (!(T is Class)) Fiber.abort ("T must be a class.")
2,122

edits