Parametric polymorphism: Difference between revisions

Added Prolog
(Added FreeBASIC)
(Added Prolog)
 
Line 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
2,131

edits