Parametric Polymorphism
type variables


Task

Write a small example for a type declaration that is parametric over another type, together with a short bit of code (and its type signature) that uses it.



A good example is a container type, let's say a binary tree, together with some function that traverses the tree, say, a map-function that operates on every element of the tree.

This language feature only applies to statically-typed languages.




procedure main()
    bTree := [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]]
    mapTree(bTree, write)
    bTree := [1, ["two", ["four", [7]], [5]], [3, ["six", ["eight"], [9]]]]
    mapTree(bTree, write)
end
 
procedure mapTree(tree, f)
    every f(\tree[1]) | mapTree(!tree[2:0], f)
end