Length¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Length[expr]
gives the number of top-level elements in expr (the arity of its head). Length of any atom is 0.
Examples (5)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (1)¶
Applications (4)¶
The number of elements of a list
The argument count of any function
Plus[a, b, c] has three arguments
An atom has no parts
Performance¶
Against other systems, from the benchmark suite (same input, results cross-checked for agreement):
| case | Mathilda | Wolfram | Python |
|---|---|---|---|
| GroebnerBasis cyclic-5 | 43.5 s | 18 s | 8.78e+03 s |
| NSolve degree 40 | 9.27 s | 0.779 s | 341 s |
| Factor x^120 - 1 | 7.01 s | 0.044 s | 3.85 s |
| Graph construction, 40000 edges | 5.81 s | 3.98 s | 14.7 s |
| Characters of 200k chars | 4.38 s | 2.54 s | 0.419 s |
| StringSplit on space, 200k chars | 3.12 s | 4.13 s | 0.723 s |
Implementation notes¶
builtin_length (in src/core.c) returns an EXPR_INTEGER equal to the argument's arg_count when it is an EXPR_FUNCTION, and 0 for atoms (which have no parts).
- Returns the count of top-level arguments for functions.
- Returns
0for all atomic expressions.
Attributes: none registered.
References¶
- Source:
src/core.c - Specification:
docs/spec/builtins/structural-manipulation.md - Tests:
tests/test_assoc_atomicity.c - Tests:
tests/test_association.c - Tests:
tests/test_autocompile.c - Tests:
tests/test_basin_hopping.c
Notes & additional examples¶
Notes¶
Length[expr] returns the number of top-level arguments (arg_count) when
expr is a function, and 0 for every atom ā a symbol, number, or string has no
parts. Because a sum is Plus[...] and a list is List[...] internally, the
same count works on both; Length makes no distinction between a List and any
other head.