Info |
---|
If using a list for holding a large set of numbers, consider using a vector instead, which will be much more efficient for this purpose. |
list ( list op list )
Operation between two lists. op is one of the operators below :
+ Addition | - Subtraction |
* Multiplication | / Division |
^ Power |
The boolean operators are :
> Larger Than | < Smaller Than |
>= Larger or Equal | <= Smaller or Equal |
= Equal | <> Not Equal |
list ( list op number )
list ( number op list )
Operations between lists and numbers. op is any of the operations defined above.
...
list ( list and list )
list ( list or list )
list ( not list )
Conjunction, Disjunction and Negation.
list ( list and number ) list ( number or list )
...
# copies elements 1, 5, 9, 13, 17 of x into y
Y = X[1,20,4]
list list[vector]
Extract a selection of elements from a list. The vector supplied as the argument provides the set of indices to be used. For example:
# copies elements 2, 1, 3 from x to y
i = |2,1,3|
y = x[i]
number ( any in list )
number ( any not in list )
Tests whether a value is in a list or not. Returns a 0 (false) or 1 (true)
...
list ( list & list )
Concatenate two lists.
...
Note that to add a single element to a list, it must first be converted into a single-element list, for example:
mylist = mylist & [23]
list abs( list )
list acos( list )
list asin( list )
list atan( list )
list cos( list )
list exp( list )
list int( list )
list log( list )
list log10( list )
list neg( list )
list sgn( list )
list sin( list )
list sqrt( list )
list tan( list )
list div( list,list )
list max( list,list )
list min( list,list )
list max( list,number )
list min( list,number )
list mod( list,list )
...
a = nil # version 2
for i = 1 to count(b) do # version 2
a = a & [sin(b[i])] # version 2
end for # version 2
This shows that the types of the elements in the input lists are not restricted – a list can contain many different data types (e.g. [number, vector, geopoints]) and as long as the requested function is valid for each type, the correct result will be returned. If the requested operation is illegal for that element (e.g. sin(['hello'])
) then it will fail on that element. See the descriptions of these functions for the relevant data types.
Anchor count count
number
...
count(
...
list
...
)
Returns the number of elements in a list.
Anchor find find
number or list find
( list,any )
...
number or list find
( list,any,string )
Searches the given list for an item and returns the index of the first occurrence of it. If an optional third argument is given as the string 'all', then a list of the indexes of all occurrences of the item is returned. In both cases, if the item is not contained in the list, nil is returned.
Anchor list list
list list(
...
any,any,...)
Returns a list built from its arguments.
Anchor sort sort
list
...
sort(
...
list
...
)
Sorts a list in ascending order.
list
...
sort(
...
list,string
...
)
Sorts a list given a comparison, expressed as a string : Ascending "<", descending ">"; you may specify the sorting criterium in a comparison function :
...
function
...
compare(a,b)
...
...
return
...
a
...
<
...
b
...
...
end
...
compare
...
...
numbers
...
=
...
[1,5,3,9,0,4,6,7,8,2]
...
...
print
...
(sort(numbers,
...
">"))
...
#
...
prints
...
in
...
decreasing
...
order
...
...
print
...
(sort(numbers,
...
"compare"))
...
#
...
prints
...
in
...
ascending
...
order
Note that it is not valid to sort a list which contains more than one type of data element.
Anchor sort_indices sort_indices
list
...
sort_indices(
...
list
...
)
...
list
...
sort_indices(
...
list,string
...
)
Sorts a list and returns the sorted indices. The default behaviour is to sort in ascending order unless an alternative comparison function is provided. See example under sort_and_indices()
to see how this function works.
Anchor
...
sort_and_indices sort_and_indices
list sort_and_indices(
...
list)
...
list
...
sort_and_indices(
...
list,string
...
)
Sorts a list and returns a list of pairs of list items and their corresponding indices in the original list. The default behaviour is to sort in ascending order unless an alternative comparison function is provided. The following example illustrates sort_indices() and sort_and_indices():
original
...
=
...
[10,
...
12,
...
9,
...
7,
...
6]
...
comparison
...
=
...
"<"
...
sorted_list
...
=
...
sort
...
(original,
...
comparison)
...
sorted_indices
...
=
...
sort_indices
...
(original,
...
comparison)
...
sorted_both
...
=
...
sort_and_indices
...
(original,
...
comparison)
...
print
...
('Original
...
list
...
:
...
',
...
original)
...
print
...
('Sorted
...
list
...
:
...
',
...
sorted_list)
...
print
...
('Sorted
...
indices
...
:
...
',
...
sorted_indices)
...
print
...
('Sort
...
and
...
indices
...
:
...
',
...
sorted_both)
...
Note that in this example it is not necessary to provide a comparison operator, as "<" is the default anyway. The output is as follows:
Original
...
list : [10,12,9,7,6]
...
Sorted
...
list :
...
[6,7,9,10,12]
...
Sorted
...
indices :
...
[5,4,3,1,2]
...
Sort
...
and
...
indices
...
:
...
[[6,5],[7,4],[9,3],[10,1],[12,2]]
...
Anchor unique unique
list unique( list )
Returns a list of the unique elements in the input list.
Anchor vector vector
vector vector( list )
Returns a vector containing the numeric elements of the input list. Any nil
list elements are converted to vector_missing_value
. Any other non-numeric elements will cause an error. If the input list is empty, the function returns nil
.
...