# prepend -- add an element to the beginning of a list

## Synopsis

• Usage:
prepend(x, L)
• Inputs:
• L,
• x,
• Outputs:
• , the list obtained by adding the element x to the beginning of list L

## Description

 i1 : prepend(3, {1, 7, 8, 3}) o1 = {3, 1, 7, 8, 3} o1 : List i2 : L = {"old", "old", "old"}; i3 : prepend("new", L) o3 = {new, old, old, old} o3 : List

The new list will be of the same class as L.

 i4 : K = (a, b, c); i5 : prepend(z, K) o5 = (z, a, b, c) o5 : Sequence

Only a single element can be prepended with this function. To prepend the elements of a list, use join. To add the new element to the end of the list, or at a particular index, use append or insert, respectively.

 i6 : join((x, y, z), K) o6 = (x, y, z, a, b, c) o6 : Sequence i7 : append(K, z) o7 = (a, b, c, z) o7 : Sequence i8 : insert(1, z, K) o8 = (a, z, b, c) o8 : Sequence

Prepend always returns a new list, rather than modifying the input list, even if L is a MutableList.

 i9 : L = new MutableList from {2,3,5}; i10 : peek prepend(7, L) o10 = MutableList{7, 2, 3, 5} i11 : peek L o11 = MutableList{2, 3, 5}

Notice that the order of the arguments is switched in prepend versus append: we write prepend(x, L) and append(L, x). A good way to remember this is that the new element is visually placed before or after the list, depending on where we want it to appear in the output.