Visualize a tree: Difference between revisions
m
→{{header|Fōrmulæ}}
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 1,671:
=={{header|Fōrmulæ}}==
{{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]]
'''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}}==
Line 4,573 ⟶ 4,610:
{{trans|C}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="
import "random" for Random
|