Visualize a tree: Difference between revisions
Content added Content deleted
Line 1,672: | Line 1,672: | ||
{{FormulaeEntry|page=https://formulae.org/?script=examples/Visualize_a_tree}} |
{{FormulaeEntry|page=https://formulae.org/?script=examples/Visualize_a_tree}} |
||
'''Solution''' |
|||
Fōrmulæ is a homoiconic language using expression trees as its fundamental structure, so managing trees is very natural. |
|||
'''Creating trees from expressions''' |
|||
The ToTree expression creates a tree from a given expression. see the following examples: |
|||
[[File:Fōrmulæ - Visualize a tree 01.png]] |
|||
[[File:Fōrmulæ - Visualize a tree 02.png]] |
|||
Be aware the the expression will be first reduced, like the following: |
|||
[[File:Fōrmulæ - Visualize a tree 03.png]] |
|||
[[File:Fōrmulæ - Visualize a tree 04.png]] |
|||
It order to avoid the reduction, the use of the Protect expression is recommended: |
|||
[[File:Fōrmulæ - Visualize a tree 05.png]] |
|||
[[File:Fōrmulæ - Visualize a tree 06.png]] |
|||
In the following example, it is shown that a matrix is a list containing (same cardinality) sublists: |
|||
[[File:Fōrmulæ - Visualize a tree 07.png]] |
|||
[[File:Fōrmulæ - Visualize a tree 08.png]] |
|||
In the following example, it is shown that, according to the Fōrmulæ arithmetic canon, a negative number is a Negative expression containing a Number expression: |
|||
[[File:Fōrmulæ - Visualize a tree 09.png]] |
|||
[[File:Fōrmulæ - Visualize a tree 10.png]] |
|||
'''Creating trees programatically''' |
|||
The tree, like any other expression in Fōrmulæ is a first-class citizen of the language. Fōrmulæ also supports high-order functions, so trees can be set as parameters of functions, or retrieved from functions. |
|||
The following example shows a function that produces a Fibonacci tree, a tree that shows the calls of a function that recursively calculates Fibonacci numbers: |
|||
[[File:Fōrmulæ - Fibonacci sequence 03.png]] |
|||
[[File:Fōrmulæ - Fibonacci sequence 04.png]] |
|||
[[File:Fōrmulæ - Fibonacci sequence 05.png]] |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |