How to resolve the algorithm Parametric polymorphism step by step in the Seed7 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Parametric polymorphism step by step in the Seed7 programming language
Table of Contents
Problem Statement
Parametric Polymorphism is a way to define types or functions that are generic over other types. The genericity can be expressed by using type variables for the parameter type, and by a mechanism to explicitly or implicitly replace the type variables with concrete types when necessary.
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Parametric polymorphism step by step in the Seed7 programming language
Source code in the seed7 programming language
$ include "seed7_05.s7i";
const func type: container (in type: elemType) is func
result
var type: container is void;
begin
container := array elemType;
global
const func container: map (in container: aContainer,
inout elemType: aVariable, ref func elemType: aFunc) is func
result
var container: mapResult is container.value;
begin
for aVariable range aContainer do
mapResult &:= aFunc;
end for;
end func;
end global;
end func;
const type: intContainer is container(integer);
var intContainer: container1 is [] (1, 2, 4, 6, 10, 12, 16, 18, 22);
var intContainer: container2 is 0 times 0;
const proc: main is func
local
var integer: num is 0;
begin
container2 := map(container1, num, num + 1);
for num range container2 do
write(num <& " ");
end for;
writeln;
end func;
You may also check:How to resolve the algorithm Soloway's recurring rainfall step by step in the RPL programming language
You may also check:How to resolve the algorithm Klarner-Rado sequence step by step in the C programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the LibreOffice Basic programming language
You may also check:How to resolve the algorithm Map range step by step in the PARI/GP programming language