178
Chapter 15: Expressions and The Expression Stack
TI
-
89 / TI
-
92 Plus Developer Guide
Not for Distribution
Beta Version January 26, 2001
15.7. Working With Lists
This section describes some of the routine ways of working with lists and tails. A
list is represented as a LIST_TAG on top of a tail. A tail is a sequence of
expressions on top of an END_TAG. For example, the list {a, 1, ln(x)} takes the
tokenized form
END_TAG X_VAR_TAG LN_TAG 1 1 NONNEGATIVE_INTEGER_TAG
A_VAR_TAG LIST_TAG
where the END_TAG is at the lowest address and the LIST_TAG is at the
highest address.
The system routines that implement calculator functions understand and correctly
handle lists. For example,
push_ln
automatically computes the natural logarithm
of each element of a list.
push_sum
automatically adds two lists, element by
element, and throws an appropriate error if the lists do not have the same
number of elements.
push_negate
changes the sign of each element of a list,
and so on. Thus, depending upon the operations involved, it is often possible to
write code that does not need to check whether its input arguments are lists. The
last future value function of the previous section is an example. Since each of the
called system routines understands lists, the resulting future value function
correctly handles lists.
Sometimes new code must be written to perform some new process on lists.
These new processes generally fall into two categories based on their result.
Either they create a new version of the list or they do not. The functions
mentioned in the previous paragraph create new lists from input lists. Here are
examples that do not create new lists.
is_constant
determines whether every
element of the list is a constant value and returns a Boolean result.
push_sumlist
returns an expression that represents the sum of all the elements
of the list.
push_dimension
returns the number of elements in the list.
Functions that do not create new lists generally use a loop to walk through the
elements of the list. Here is a function that returns the number of elements in a
list.
unsigned short number_of_elements (EStackIndex i)
{ unsigned short count = 0; /* initialize counter */
--i; /* move index from LIST_TAG to first list element */
while (END_TAG != ESTACK(i)) /* while not at end of list */
{ ++count; /* increment counter */
i = next_expression_index (i); /* step to next element */
}
return count;
}