Tree from nesting levels: Difference between revisions

Content deleted Content added
Rdm (talk | contribs)
m →‎{{header|J}}: add some comments
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 34: Line 34:
===Iterative===
===Iterative===


<lang applescript>on treeFromNestingLevels(input)
<syntaxhighlight lang="applescript">on treeFromNestingLevels(input)
set maxLevel to 0
set maxLevel to 0
repeat with thisLevel in input
repeat with thisLevel in input
Line 80: Line 80:
set output to output as text
set output to output as text
set AppleScript's text item delimiters to astid
set AppleScript's text item delimiters to astid
return output</lang>
return output</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"{} nests to: {}
<syntaxhighlight lang="applescript">"{} nests to: {}
{1, 2, 4} nests to: {1, {2, {{4}}}}
{1, 2, 4} nests to: {1, {2, {{4}}}}
{3, 1, 3, 1} nests to: {{{3}}, 1, {{3}}, 1}
{3, 1, 3, 1} nests to: {{{3}}, 1, {{3}}, 1}
{1, 2, 3, 1} nests to: {1, {2, {3}}, 1}
{1, 2, 3, 1} nests to: {1, {2, {3}}, 1}
{3, 2, 1, 3} nests to: {{{3}, 2}, 1, {{3}}}
{3, 2, 1, 3} nests to: {{{3}, 2}, 1, {{3}}}
{3, 3, 3, 1, 1, 3, 3, 3} nests to: {{{3, 3, 3}}, 1, 1, {{3, 3, 3}}}"</lang>
{3, 3, 3, 1, 1, 3, 3, 3} nests to: {{{3, 3, 3}}, 1, 1, {{3, 3, 3}}}"</syntaxhighlight>


===Recursive===
===Recursive===


Same task code and output as above.
Same task code and output as above.
<lang applescript>on treeFromNestingLevels(input)
<syntaxhighlight lang="applescript">on treeFromNestingLevels(input)
script recursion
script recursion
property emptyList : {}
property emptyList : {}
Line 119: Line 119:
return recursion's recurse(input, 1)
return recursion's recurse(input, 1)
end treeFromNestingLevels</lang>
end treeFromNestingLevels</syntaxhighlight>




Line 126: Line 126:
:# a generic ''forestFromNestLevels'' function to map from a normalised input list to a generic tree, and
:# a generic ''forestFromNestLevels'' function to map from a normalised input list to a generic tree, and
:# a standard catamorphism over trees (''foldTree'') to generate both the nested list format, and the round-trip regeneration of a sparse list from the generic tree.
:# a standard catamorphism over trees (''foldTree'') to generate both the nested list format, and the round-trip regeneration of a sparse list from the generic tree.
<lang applescript>----------------- FOREST FROM NEST LEVELS ----------------
<syntaxhighlight lang="applescript">----------------- FOREST FROM NEST LEVELS ----------------


-- forestFromNestLevels :: [(Int, a)] -> [Tree a]
-- forestFromNestLevels :: [(Int, a)] -> [Tree a]
Line 581: Line 581:
end repeat
end repeat
v
v
end |until|</lang>
end |until|</syntaxhighlight>
<pre>
<pre>
INPUT -> NESTED -> ROUND-TRIP
INPUT -> NESTED -> ROUND-TRIP
Line 593: Line 593:
=={{header|C++}}==
=={{header|C++}}==
Uses C++20
Uses C++20
<lang cpp>#include <any>
<syntaxhighlight lang="cpp">#include <any>
#include <iostream>
#include <iostream>
#include <iterator>
#include <iterator>
Line 674: Line 674:
}
}
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 708: Line 708:
===Iterative===
===Iterative===
{{trans|Python}}
{{trans|Python}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 748: Line 748:
fmt.Printf("%17s => %v\n", fmt.Sprintf("%v", test), nest)
fmt.Printf("%17s => %v\n", fmt.Sprintf("%v", test), nest)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 762: Line 762:
===Recursive===
===Recursive===
{{trans|Python}}
{{trans|Python}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 803: Line 803:
fmt.Printf("%17s => %v\n", fmt.Sprintf("%v", test), nest)
fmt.Printf("%17s => %v\n", fmt.Sprintf("%v", test), nest)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 811: Line 811:


=={{header|Guile}}==
=={{header|Guile}}==
<lang Scheme>;; helper function that finds the rest that are less than or equal
<syntaxhighlight lang="scheme">;; helper function that finds the rest that are less than or equal
(define (rest-less-eq x ls)
(define (rest-less-eq x ls)
(cond
(cond
Line 847: Line 847:


(run-examples examples)
(run-examples examples)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 871: Line 871:
See ''sparseLevelsFromTree'' below:
See ''sparseLevelsFromTree'' below:


<lang haskell>{-# LANGUAGE TupleSections #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}


import Data.Bifunctor (bimap)
import Data.Bifunctor (bimap)
Line 939: Line 939:
(x, Just x) :
(x, Just x) :
(succ x, Nothing) : normalised (y : xs)
(succ x, Nothing) : normalised (y : xs)
| otherwise = (x, Just x) : normalised (y : xs)</lang>
| otherwise = (x, Just x) : normalised (y : xs)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>From: []
<pre>From: []
Line 1,048: Line 1,048:
As a side note here, the notation used to describe these trees has some interesting consequences in the context of J:
As a side note here, the notation used to describe these trees has some interesting consequences in the context of J:


<lang J> [[[3]], 1, [[3]], 1
<syntaxhighlight lang="j"> [[[3]], 1, [[3]], 1
1 1
1 1
[[[3]], 1, [[3]], 1]
[[[3]], 1, [[3]], 1]
|syntax error</lang>
|syntax error</syntaxhighlight>


But, on a related note, there are type issues to consider -- in J's type system, a box (which is what we would use here to represent a tree node) cannot exist in a tuple with an integer. A box can, however, contain an integer. This makes a literal interpretation of the task somewhat... difficult. We might, hypothetically, say that we are working with boxes containing integers and that it's these boxes which must achieve a specific nesting level. (If we fail to make this distinction then we wind up with a constraint which forces some tree nodes to be structured different from what appears to be the task specification. Whether or not this is an important issue is difficult to determine without use cases. So, for now, let's assume that this is an important distinction.)
But, on a related note, there are type issues to consider -- in J's type system, a box (which is what we would use here to represent a tree node) cannot exist in a tuple with an integer. A box can, however, contain an integer. This makes a literal interpretation of the task somewhat... difficult. We might, hypothetically, say that we are working with boxes containing integers and that it's these boxes which must achieve a specific nesting level. (If we fail to make this distinction then we wind up with a constraint which forces some tree nodes to be structured different from what appears to be the task specification. Whether or not this is an important issue is difficult to determine without use cases. So, for now, let's assume that this is an important distinction.)
Line 1,057: Line 1,057:
Anyways, here's an interpretation which might be close enough to the task description:
Anyways, here's an interpretation which might be close enough to the task description:


<lang J>NB. first we nest each integer to the required depth, independently
<syntaxhighlight lang="j">NB. first we nest each integer to the required depth, independently
NB. then we recursively merge deep boxes
NB. then we recursively merge deep boxes
NB. for consistency, if there are no integers, we box that empty list
NB. for consistency, if there are no integers, we box that empty list
Line 1,069: Line 1,069:
group=. shallow} (+/\ shallow),:-#\y NB. find groups of adjacent deep boxes
group=. shallow} (+/\ shallow),:-#\y NB. find groups of adjacent deep boxes
merge each group ,each//. y NB. combine them and recursively merge their contents
merge each group ,each//. y NB. combine them and recursively merge their contents
}}</lang>
}}</syntaxhighlight>


Task example:
Task example:


<lang J> dtree ''
<syntaxhighlight lang="j"> dtree ''
┌┐
┌┐
││
││
Line 1,128: Line 1,128:
││└───────┘│ │ │└───────┘││
││└───────┘│ │ │└───────┘││
│└─────────┴─┴─┴─────────┘│
│└─────────┴─┴─┴─────────┘│
└─────────────────────────┘</lang>
└─────────────────────────┘</syntaxhighlight>


Note that merge does not concern itself with the contents of boxes, only their nesting depth. This means that we could replace the implementation of dtree with some similar mechanism if we wished to use this approach with something else. For example:
Note that merge does not concern itself with the contents of boxes, only their nesting depth. This means that we could replace the implementation of dtree with some similar mechanism if we wished to use this approach with something else. For example:


<lang J> t=: ;:'(a b c) d (e f g)'
<syntaxhighlight lang="j"> t=: ;:'(a b c) d (e f g)'
p=: ;:'()'
p=: ;:'()'
d=: +/\-/p=/t
d=: +/\-/p=/t
Line 1,141: Line 1,141:
││a│b│c││ ││e│f│g││
││a│b│c││ ││e│f│g││
│└─┴─┴─┘│ │└─┴─┴─┘│
│└─┴─┴─┘│ │└─┴─┴─┘│
└───────┴─┴───────┘</lang>
└───────┴─┴───────┘</syntaxhighlight>


Or, generalizing:
Or, generalizing:


<lang J>pnest=: {{
<syntaxhighlight lang="j">pnest=: {{
t=. ;:y NB. tokens
t=. ;:y NB. tokens
p=. (;:'()')=/t NB. paren token matches
p=. (;:'()')=/t NB. paren token matches
Line 1,151: Line 1,151:
k=: =/p NB. keep non-paren tokens
k=: =/p NB. keep non-paren tokens
merge d <@]^:[&.>&(k&#) t NB. exercise task
merge d <@]^:[&.>&(k&#) t NB. exercise task
}}</lang>
}}</syntaxhighlight>


Example use:
Example use:


<lang J> pnest '((a b) c (d e) f) g (h i)'
<syntaxhighlight lang="j"> pnest '((a b) c (d e) f) g (h i)'
┌─────────────────┬─┬─────┐
┌─────────────────┬─┬─────┐
│┌─────┬─┬─────┬─┐│g│┌─┬─┐│
│┌─────┬─┬─────┬─┐│g│┌─┬─┐│
Line 1,162: Line 1,162:
││└─┴─┘│ │└─┴─┘│ ││ │ │
││└─┴─┘│ │└─┴─┘│ ││ │ │
│└─────┴─┴─────┴─┘│ │ │
│└─────┴─┴─────┴─┘│ │ │
└─────────────────┴─┴─────┘</lang>
└─────────────────┴─┴─────┘</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>function makenested(list)
<syntaxhighlight lang="julia">function makenested(list)
nesting = 0
nesting = 0
str = isempty(list) ? "[]" : ""
str = isempty(list) ? "[]" : ""
Line 1,187: Line 1,187:
end
end


</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
[] => []
[] => []
Line 1,200: Line 1,200:
In a strongly and statically typed language as Nim, there is no way to mix integer values and lists. So, we have defined a variant type <code>Node</code> able to contain either an integer value or a list of Node objects, depending on the value of a discriminator. The procedure <code>newTree</code> converts a list of levels into a list of nodes with the appropriate nesting.
In a strongly and statically typed language as Nim, there is no way to mix integer values and lists. So, we have defined a variant type <code>Node</code> able to contain either an integer value or a list of Node objects, depending on the value of a discriminator. The procedure <code>newTree</code> converts a list of levels into a list of nodes with the appropriate nesting.


<lang Nim>import sequtils, strutils
<syntaxhighlight lang="nim">import sequtils, strutils


type
type
Line 1,246: Line 1,246:
@[3, 2, 1, 3],
@[3, 2, 1, 3],
@[3, 3, 3, 1, 1, 3, 3, 3]]:
@[3, 3, 3, 1, 1, 3, 3, 3]]:
echo ($list).align(25), " → ", newTree(list)</lang>
echo ($list).align(25), " → ", newTree(list)</syntaxhighlight>


{{out}}
{{out}}
Line 1,258: Line 1,258:
=={{header|OxygenBasic}}==
=={{header|OxygenBasic}}==


<lang>
<syntaxhighlight lang="text">
uses console
uses console
declare DemoTree(string src)
declare DemoTree(string src)
Line 1,403: Line 1,403:


end sub
end sub
</syntaxhighlight>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
===String Eval===
===String Eval===
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict;
use strict;
Line 1,430: Line 1,430:
my $after = eval;
my $after = eval;
dd { after => $after };
dd { after => $after };
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,448: Line 1,448:
===Iterative===
===Iterative===
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use 5.020_000; # Also turns on `strict`
<syntaxhighlight lang="perl">use 5.020_000; # Also turns on `strict`
use warnings;
use warnings;
use experimental qw<signatures>;
use experimental qw<signatures>;
Line 1,471: Line 1,471:
}
}
my @tests = ([],[1,2,4],[3,1,3,1],[1,2,3,1],[3,2,1,3],[3,3,3,1,1,3,3,3]);
my @tests = ([],[1,2,4],[3,1,3,1],[1,2,3,1],[3,2,1,3],[3,3,3,1,1,3,3,3]);
say sprintf('%15s => ', join(' ', @{$_})), pp(to_tree_iterative(@{$_})) for @tests;</lang>
say sprintf('%15s => ', join(' ', @{$_})), pp(to_tree_iterative(@{$_})) for @tests;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,484: Line 1,484:
=={{header|Phix}}==
=={{header|Phix}}==
I was thinking along the same lines but admit I had a little peek at the (recursive) python solution..
I was thinking along the same lines but admit I had a little peek at the (recursive) python solution..
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">level</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">part</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">part</span>
Line 1,513: Line 1,513:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%v nests to %v\n or %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rpp</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%v nests to %v\n or %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rpp</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,553: Line 1,553:
</pre>
</pre>
=== iterative ===
=== iterative ===
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">nest</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">nest</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
Line 1,576: Line 1,576:
<span style="color: #008080;">return</span> <span style="color: #000000;">input</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">input</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Same output (using nest instead of test)
Same output (using nest instead of test)


Line 1,583: Line 1,583:
===Python: Procedural===
===Python: Procedural===
====Python: Recursive====
====Python: Recursive====
<lang python>def to_tree(x, index=0, depth=1):
<syntaxhighlight lang="python">def to_tree(x, index=0, depth=1):
so_far = []
so_far = []
while index < len(x):
while index < len(x):
Line 1,617: Line 1,617:
nest = to_tree(flat)
nest = to_tree(flat)
print(f"{flat} NESTS TO: {nest}")
print(f"{flat} NESTS TO: {nest}")
pnest(nest)</lang>
pnest(nest)</syntaxhighlight>


{{out}}
{{out}}
Line 1,657: Line 1,657:


====Python: Iterative====
====Python: Iterative====
<lang python>def to_tree(x: list) -> list:
<syntaxhighlight lang="python">def to_tree(x: list) -> list:
nested = []
nested = []
stack = [nested]
stack = [nested]
Line 1,671: Line 1,671:


return nested
return nested
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,691: Line 1,691:


<pre>Node (None|Int) :: ((None|Int), [Node])</pre>
<pre>Node (None|Int) :: ((None|Int), [Node])</pre>
<lang python>'''Tree from nesting levels'''
<syntaxhighlight lang="python">'''Tree from nesting levels'''


from itertools import chain, repeat
from itertools import chain, repeat
Line 1,992: Line 1,992:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>From: []
<pre>From: []
Line 2,128: Line 2,128:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ stack ] is prev ( --> s )
<syntaxhighlight lang="quackery"> [ stack ] is prev ( --> s )


[ temp take
[ temp take
Line 2,160: Line 2,160:
witheach
witheach
[ dup echo say " --> "
[ dup echo say " --> "
nesttree echo cr cr ]</lang>
nesttree echo cr cr ]</syntaxhighlight>


{{out}}
{{out}}
Line 2,180: Line 2,180:
===Iterative===
===Iterative===
{{trans|Python}}
{{trans|Python}}
<lang perl6>sub new_level ( @stack --> Nil ) {
<syntaxhighlight lang="raku" line>sub new_level ( @stack --> Nil ) {
my $e = [];
my $e = [];
push @stack.tail, $e;
push @stack.tail, $e;
Line 2,198: Line 2,198:
}
}
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
say .Str.fmt( '%15s => ' ), .&to_tree_iterative for @tests;</lang>
say .Str.fmt( '%15s => ' ), .&to_tree_iterative for @tests;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,210: Line 2,210:
===Recursive===
===Recursive===
{{trans|Python}}
{{trans|Python}}
<lang perl6>sub to_tree_recursive ( @list, $index is copy, $depth ) {
<syntaxhighlight lang="raku" line>sub to_tree_recursive ( @list, $index is copy, $depth ) {
my @so_far = gather while $index <= @list.end {
my @so_far = gather while $index <= @list.end {
my $t = @list[$index];
my $t = @list[$index];
Line 2,234: Line 2,234:
}
}
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
say .Str.fmt( '%15s => ' ), to_tree_recursive( $_, 0, 1 ).[1] for @tests;</lang>
say .Str.fmt( '%15s => ' ), to_tree_recursive( $_, 0, 1 ).[1] for @tests;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,247: Line 2,247:
===String Eval===
===String Eval===
{{trans|Perl}}
{{trans|Perl}}
<lang perl6>use MONKEY-SEE-NO-EVAL;
<syntaxhighlight lang="raku" line>use MONKEY-SEE-NO-EVAL;
sub to_tree_string_eval ( @xs --> Array ) {
sub to_tree_string_eval ( @xs --> Array ) {
my @gap = [ |@xs, 0 ] Z- [ 0, |@xs ];
my @gap = [ |@xs, 0 ] Z- [ 0, |@xs ];
Line 2,259: Line 2,259:
}
}
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
my @tests = (), (1, 2, 4), (3, 1, 3, 1), (1, 2, 3, 1), (3, 2, 1, 3), (3, 3, 3, 1, 1, 3, 3, 3);
say .Str.fmt( '%15s => ' ), .&to_tree_string_eval for @tests;</lang>
say .Str.fmt( '%15s => ' ), .&to_tree_string_eval for @tests;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,275: Line 2,275:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/seq" for Stack
<syntaxhighlight lang="ecmascript">import "/seq" for Stack
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 2,308: Line 2,308:
var nest = toTree.call(test)
var nest = toTree.call(test)
Fmt.print("$24n => $n", test, nest)
Fmt.print("$24n => $n", test, nest)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,322: Line 2,322:
===Recursive===
===Recursive===
{{trans|Python}}
{{trans|Python}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var toTree // recursive
var toTree // recursive
Line 2,356: Line 2,356:
var n = toTree.call(test, 0, 1)
var n = toTree.call(test, 0, 1)
Fmt.print("$24n => $n", test, n[1])
Fmt.print("$24n => $n", test, n[1])
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}