Programming Language for Old Timers


by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.


Previous page   Table of Contents   Next page


Array Protocol

Array is a subprotocol of collection and keyed-sequence. An array has a sequence of elements in a defined order, keyed by consecutive integers starting at zero. The keys are also called subscripts. Negative subscripts index from the end of the sequence. Thus key 0 selects the first element and key -1 selects the last element.

The length of an array is the number of elements.

Some array types have restrictions on the types of their elements.

Array is implemented by list, stack, argument-list, bit-vector, etc.

String also implements the method requirements of array, but does not inherit from the protocol itself. This is because the + operator with a string as its right-hand argument works differently depending on the type of its left-hand argument. If the left-hand argument is an array, the result is an array with one more element, the string. If the left-hand argument is a string, the result is the two strings concatenated. Adding a string to an array does not take the string apart into its individual characters. For this reason, some methods may be specialized for "array or string."

The following method requirements, in addition to the collection and sequence methods, constitute the array protocol:

defprotocol array is sequence, keyed-sequence, collection
  x is array[subscript is integer, result: element]
  x is array[range is range, result: subseq is array]
  position(element, x is array, result: integer or false)
  x is array + y is array, result: concatenation is array
  x is array + y is anything, result: appended is array
  first(x is array, result: element)
  last(x is array, result: element)

Many arrays also implement assignable-sequence. There is a protocol assignable-array that inherits from array and assignable-keyed-sequence and adds these method requirements:

defprotocol assignable-array is array, assignable-keyed-sequence
  x is array[subscript is integer] := new-element
  first:=(x is array, new-element)
  last:=(x is array, new-element)

A superprotocol of array is collection. A collection is like an array except that it does not have subscripts, so the [], []:=, position, first, last, and reverse methods are not present.


Previous page   Table of Contents   Next page