Tree datastructures: Difference between revisions

m
→‎{{header|zkl}}: added Perl6 example
(→‎{{header|Perl 6}}: Add a Perl 6 example)
m (→‎{{header|zkl}}: added Perl6 example)
Line 426:
}(List(),nestTree,0)
}
fcn nestToString(nestTree,dot="."){
fcn(out,dot,node,level){
out.writeln("."dot*level,node[0]); // (name)
if(node.len()>1){ // (name children), (name, (tree))
level+=1;
foreach child in (node[1,*]){
if(String.isType(child)) out.writeln("."dot*level,child);
else self.fcn(out,dot,child,level)
}
}
out
}(Data(),dot,nestTree,0).text
}
 
Line 500:
L("RosettaCode",L("rocks","code","comparison","wiki"),L("mocks","golfing"))
Is this tree the same as what we started with? True
</pre>
I'm choosing to only allow one root per tree/forest so the Perl6 example is coded differently:
<lang zkl>perl6trees:=L(
L("RosettaCode",
L("encourages",
L("code",
"diversity","comparison")),
L("discourages",
"golfing","trolling","emphasising execution speed"),
),
L("code-golf.io",
L("encourages","golfing"),
L("discourages","comparison"),
)
);
println(perl6trees.apply(nestToString).concat());
iTrees := perl6trees.apply(nestToIndent);
println(iTrees.apply(indentToString).concat("\n"));
(iTrees.apply(indentToNest)==perl6trees).println();</lang>
{{out}}
<pre style="height:40ex">
RosettaCode
encourages
code
diversity
comparison
discourages
golfing
trolling
emphasising execution speed
code-golf.io
encourages
golfing
discourages
comparison
 
0 RosettaCode
1 encourages
2 code
3 diversity
3 comparison
1 discourages
2 golfing
2 trolling
2 emphasising execution speed
0 code-golf.io
1 encourages
2 golfing
1 discourages
2 comparison
True
</pre>
Anonymous user