LeafCount
LeafCount[expr]
gives the total number of indivisible subexpressions in expr.
Details and Options
- LeafCount gives a measure of the total "size" of an expression.
- LeafCount counts the number of subexpressions in expr that correspond to "leaves" on the expression tree.
- For ordinary expressions, LeafCount is based on FullForm representation.
- For raw and other atomic expressions, the LeafCount is 1 with the following exceptions: »
-
Association based on the leaf count of the values Complex based on FullForm Rational based on FullForm - With the option setting HeadsFalse, LeafCount excludes heads of expressions and their parts.
Examples
open allclose allBasic Examples (1)
Applications (3)
Find leaf count measures of the sizes of successive integrals:
Make Simplify pick expressions with minimum LeafCount:
Find leaf counts in combinator expressions [more info]:
Properties & Relations (6)
LeafCount by default includes the head and all elements inside it:
For expressions with atomic heads, this corresponds to the number of vertices in TreeForm[expr]:
LeafCount[expr,HeadsFalse] typically gives the number of leaves in TreeForm[expr]:
LeafCount counts elements in rational and complex numbers just as in FullForm:
LeafCount counts the head and values of an Association:
These are the five leaves in the expression:
Reversing the keys and values gives a different result:
LeafCount treats atomic objects, such as SparseArray objects, as single expression leaves:
SparseArray objects have a complicated FullForm:
For most expressions, LeafCount equals the count matching Blank[] at level {-1}:
LeafCount counts Rational and Complex numbers using their FullForm:
Possible Issues (3)
LeafCount is based on the FullForm of expressions:
LeafCount by default includes leaves in heads:
The head f is considered a leaf, even though it is not a leaf in a tree visualization of the expression:
Use the setting HeadsFalse to only count the leaves of the tree visualization:
Unlike TreeForm, LeafCount takes into consideration the structure of expression heads:
This shows the expression tree whose leaves are counted by LeafCount:
Text
Wolfram Research (1988), LeafCount, Wolfram Language function, https://reference.wolfram.com/language/ref/LeafCount.html (updated 2022).
CMS
Wolfram Language. 1988. "LeafCount." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/LeafCount.html.
APA
Wolfram Language. (1988). LeafCount. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LeafCount.html