1.5. Lists

A list consists of a finite sequence of values. The empty list is written as [].

1.5.1. Syntax

[4]List::= [ Expression , , Expression ]  

1.5.2. Constructors

The list expression [ E0 ,, En ] publishes the list [ v0 ,, vn ] only if each expression Ei deflates to value vi. Otherwise, it halts silently.

1.5.3. Operations

Notable list operations include:

  • Cons (construct) a list with first element h and remaining elements t: h:t

  • Publish true iff the list l has no elements: empty(l)

  • Publish the length of list l: length(l)

  • Publish the first element of list l: head(l)

  • Publish a list with every element in list l except the first: tail(l)

  • Publish all but the last element of list l: init(l)

  • Publish the last element of list l: last(l)

  • Publish the nth element of a list, counting from 0: index(l, n)

  • Publish a list with the first n elements of the list l: take(n, l)

  • Publish a list with all but the first n elements of the list l: drop(n, l)

  • Publish every value in list l, simultaneously: each(l)

  • Concatenate list a and list b: append(a, b)

  • Publish a list with the elements of list l in reverse order: reverse(l)

  • Publish a list containing only those elements of l which satisfy the function f: filter(f, l)

  • Apply unary function f to every element of list l (in parallel), and return a list of the results: map(f, l)

  • Apply a binary function to every element of a list: foldl, foldr, and many variations thereof.

  • Combine two lists into a list of pairs, and its reverse: zip and unzip

  • Concatenate a list of lists l into a single list: concat(l)

  • Publish true if item x is a member of list l: member(l)

1.5.4. Type

The type of a list is List[U], where U is the join of the types of each of its elements. In particular, if all of the elements have the same type T, then the list will have type List[T].

1.5.5. Java calls

Orc lists do not correspond to any Java value, so if a list is passed to Java code, it will be as a java.lang.Object of a type not specified here.

1.5.6. Examples

Sample Lists
  []                       -- empty list
| [1, 2, 3]                -- a list of integers
| [(1, 2), (2, 3), (3, 4)] -- a list of tuples of integers
| [1, 1 * 2, 1 * 2 * 3]    -- a list of the first 3 factorials
| [[1], [2, 2], [3, 3, 3]] -- a list of lists of integers

{-
OUTPUT:PERMUTABLE:
[]
[1, 2, 3]
[(1, 2), (2, 3), (3, 4)]
[1, 2, 6]
[[1], [2, 2], [3, 3, 3]]
-}
Lists Are Not Sets
{- 
  Lists do not behave like sets.
  The order and number of elements in a list do matter.
-}

  [2,3] /= [3,2]  
| [2] /= [2,2]

{-
OUTPUT:
true
true
-}
Building Lists with Cons
  3:[]
| 4:3:[2,1]

{-
OUTPUT:PERMUTABLE:
[3]
[4, 3, 2, 1]
-}

1.5.7. Related Links

Related Tutorial Sections