Parametric polymorphism: Difference between revisions

added Ursala
(Derived a Clean implementation based on the Haskell one)
(added Ursala)
Line 233:
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}}==
Types are first class entities and functions to construct or operate on them may be defined
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>
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.) A mapping combinator over this type can be defined as shown.
<lang Ursala>
binary_tree_map "f" = ~&a^& ^A/"f"@an ~&amPfamPWB
</lang>
(Type signatures are not associated with function declarations.) 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': ())
 
#cast string_tree
 
example = (binary_tree_map "s". "s"--"s") x
</lang>
output:
<pre>
'foofoo': ('barbar': (),'bazbaz': ())
</pre>
 
{{Omit From|ALGOL 68}} <!-- it isn't immediately obvious that ALGOL 68 is object oriented -->
{{omit from|C}} <!-- no type variables in stdc -->
Anonymous user