Parametric polymorphism: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added labels to each of the node's output, added/changed comments and whitespace.)
Line 952: Line 952:
=={{header|REXX}}==
=={{header|REXX}}==
This REXX programming example is modeled after the   '''D'''   example.
This REXX programming example is modeled after the   '''D'''   example.
<lang rexx>/*REXX program demonstrates (with displays) a method of parametric polymorphism in REXX.*/
<lang rexx>/*REXX program demonstrates (with displays) a method of parametric polymorphism. */
call newRoot 1.00, 3 /*new root, and also indicate 3 stems.*/
call newRoot 1.00, 3 /*new root, and also indicate 3 stems.*/
/* [↓] no need to label the stems. */
/* [↓] no need to label the stems. */
Line 966: Line 966:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
addStem: nodes=nodes+1; do j=1 for stems; root.nodes.j=arg(1); end; return
addStem: nodes=nodes + 1; do j=1 for stems; root.nodes.j=arg(1); end; return
newRoot: parse arg @,stems; nodes=-1; call addStem copies('═',9); call addStem @; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
modRoot: do j=1 for nodes /*traipse through all the defined nodes*/
modRoot: arg #; do j=1 for nodes /*traipse through all the defined nodes*/
do k=1 for stems
do k=1 for stems
if datatype(root.j.k, 'N') then root.j.k=root.j.k + arg(1) /*bias.*/
if datatype(root.j.k,'N') then root.j.k=root.j.k + # /*add bias.*/
end /*k*/ /* [↑] only add if numeric stem value.*/
end /*k*/ /* [↑] only add if numeric stem value.*/
end /*j*/
end /*j*/
return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
newRoot: stems=arg(2); nodes= -1 /*set NODES to a kind of "null". */
sayNodes: w=9; do j=0 to nodes; _= /*ensure each of the nodes gets shown. */
do k=1 for stems; _=_ center(root.j.k, w) /*concatenate a node*/
call addStem copies('═', 9); call addStem arg(1)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
sayNodes: say; do j=0 to nodes; _= /*ensure each of the nodes gets shown. */
do k=1 for stems; _=_ right(root.j.k, 9)
end /*k*/
end /*k*/
say substr(_, 2) /*ignore the first (leading) blank. */
$=word('node='j, 1 + (j<1) ) /*define a label for this line's output*/
end /*j*/
say center($, w) substr(_, 2) /*ignore 1st (leading) blank which was */
end /*j*/ /* [↑] caused by concatenation.*/

say left('', stems*11) || '('nodes" nodes)" /*also show number of nodes.*/
say /*show a blank line to separate outputs*/
return</lang>
return /* [↑] extreme indentation to terminal*/</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
═════════ ═════════ ═════════
═════════ ═════════ ═════════
1.00 1.00 1.00
node=1 1.00 1.00 1.00
1.10 1.10 1.10
node=2 1.10 1.10 1.10
1.11 1.11 1.11
node=3 1.11 1.11 1.11
1.12 1.12 1.12
node=4 1.12 1.12 1.12
1.20 1.20 1.20
node=5 1.20 1.20 1.20
1.21 1.21 1.21
node=6 1.21 1.21 1.21
1.22 1.22 1.22
node=7 1.22 1.22 1.22
(7 nodes)


═════════ ═════════ ═════════
═════════ ═════════ ═════════
51.00 51.00 51.00
node=1 51.00 51.00 51.00
51.10 51.10 51.10
node=2 51.10 51.10 51.10
51.11 51.11 51.11
node=3 51.11 51.11 51.11
51.12 51.12 51.12
node=4 51.12 51.12 51.12
51.20 51.20 51.20
node=5 51.20 51.20 51.20
51.21 51.21 51.21
node=6 51.21 51.21 51.21
51.22 51.22 51.22
node=7 51.22 51.22 51.22
(7 nodes)
</pre>
</pre>