Talk:Tree from nesting levels: Difference between revisions

m
 
(2 intermediate revisions by the same user not shown)
Line 70:
[[User:Hout|Hout]] ([[User talk:Hout|talk]]) 12:23, 4 February 2021 (UTC)
 
We can obtain a self-consistent representation of these forests as lists of tuples, in which the first value is a kind of sum type – in Python terms (IntNone or Noneinteger), and the second value is itself a (possibly empty) forest:.
To put this in a Python notation, if we '''start''' with one of these forests ('''lists''' of trees), mapping over each of its members with a foldTree catamorphism, passing a function like this to it:
 
A consistent recursive data structure – lets give it a name like '''Node'''.
<lang python># levelList :: Tree (Int|None)
def levelList(x):
'''A Tree in which is node is a tuple of two values:
A possible integer, and a list of trees.
(Int or None, [Tree])
'''
def go(xs):
if x.get('Nothing', False):
return (None, concat(xs))
else:
return [(x.get('Just', 0), concat(xs))]
return go</lang>
 
In Python terms, '''Node''' here is a tuple of a possible integer with a list of '''Nodes''':
We can obtain a self-consistent representation of these forests as lists of tuples, in which the first value is a kind of sum type (Int or None), and the second value is itself a (possibly empty) forest:
<pre>Node (None|Int) :: ((None|Int), [Node])</pre>
 
<lang python>(None, [])
[(None, [(1, [(2, [(None, [(4, [])])])])]])
[(None, [(None, [(None, [(3, [])])])], [(1, [(None, [(3, [])])])], [(1, [])]])
[(None, [(1, [(2, [(3, [])])])], [(1, [])]])
[(None, [(None, [(None, [(3, [])]), (2, [])])], [(1, [(None, [(3, [])])])]])
[(None, [(None, [(None, [(3, []), (3, []), (3, [])])])], [(1, [])], [(1, [(None, [(3, []), (3, []), (3, [])])])]])</lang>
[[User:Hout|Hout]] ([[User talk:Hout|talk]]) 14:00, 4 February 2021 (UTC)
 
9,655

edits