File: length.tex

package info (click to toggle)
cadabra 1.46-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,244 kB
  • sloc: cpp: 33,188; ansic: 2,724; makefile: 329; yacc: 180; sh: 157; python: 45; lex: 38; lisp: 19
file content (39 lines) | stat: -rw-r--r-- 1,000 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
\cdbalgorithm{length}{}

Replaces the expression with its length, that is, the number of terms
in a sum, the number of factors in a product or the number of
elements in a list.
\begin{screen}{1,2,4,5,7,8}
lst:= {a,b,c,d}:
@length(%);
lst:= 4;
sum:= a + b + c + d + e:
@length(%);
sum:= 5;
prod:= a*b*c:
@length(%);
prod:= 3;
\end{screen}
Note that the original expression gets replaced; if you need a new
expression use a square-bracket construction as in
\begin{screen}{1,2}
sum:= a + b + c + d;
@length[@(sum)];
2:= 4;
\end{screen}

Here is a more complicated example which takes all terms except the
first and last from an expression:
\begin{screen}{1,2}
a+b+c+d+e+f+g;
@take(%){ 1..@collect_terms[@length[@(%)]-2] };
b+c+d+e+f;
\end{screen}
(remember that counting terms starts from zero).

If you just want to display the number of elements, use
\subscommand{number\_of\_terms}; this is an output command and does
not change the expression.

\cdbseealgo{number_of_terms}
\cdbseealgo{take_match}