list Operators

The following operations can be used with list expressions:

  • insert expression, at list, { index }: inserts expression at the specified index in the list, shifting up other list elements with higher indices

  • remove list, { index }: removes the element of list at the specified index and returns it, shifting down other list elements with higher indices

  • push expression, onto list: appends expression onto the end of list

  • pop list: removes the last element of list and returns it

  • prepend expression, onto list: prepends expression onto the start of list

  • unprepend list: removes the first element of list and returns it

  • list, { expression }: returns the element of the list at offset expression. The expression index is zero based, as in C, such that 0 refers to the first element, 1 to the second, and so forth. If the offset expression is less than zero, or greater than the length of the list minus one (because the access is zero based), an error is triggered and the simulation is stopped.

  • list, { expression, } = value: sets an element of the list at offset expression to value. The offset index is again zero based. If the offset expression is less than zero or greater than the size of the list an error is triggered and the simulation is stopped. If the offset expression is equal to the size of the list, the list is extended by one element; the operation has the same effect as pushing a value on to the end.

  • sort list, with method-name: sorts list using the method specified with method-name. method-name must be a method which takes two list elements (the keywords are unimportant) and compares them, returning a negative number if the first list element belongs before the second in the sorted list, a positive number if the second belongs before the first, and 0 if the two entries are equal. In most cases, this confusing sounding method returns a certain value associated with one argument minus the same value in the other.

    Unlike the perl sort operator, sort operates on the list it is given and does not return a copy of it. This means that the original list is modified during the sort operation.

  • copylist list: copies the entire list. Normally, assigning a list to a variable will not copy the list but instead will yield two variables pointing to the same list.

  • | list |: gives the length of a list. Lists are automatically converted to integers when use in mathematical expressions, but this construct can be used too force the conversion.