Rotating a list: rotate

### 5.40.16 Rotating a list: rotate

The rotate command rotates a list; namely it moves elements from one side and puts them on the other.

- rotate takes one mandatory argument and one optional argument:
  - L, a list.
  - Optionally, n, an integer (by default n=−1).
- rotate(L) returns the list formed by rotating L n places to the left if n>0 or −n places to the right if n<0. Elements leaving the list from one side come back on the other side. By default n=−1 and the last element becomes the first.

#### Examples

- Input:
  rotate([0,1,2,3,4])
  Output:

  ⎡ ⎣ | 4,0,1,2,3 | ⎤ ⎦

- Input:
  rotate([0,1,2,3,4],2)
  Output:

  ⎡ ⎣ | 2,3,4,0,1 | ⎤ ⎦

- Input:
  rotate([0,1,2,3,4],-2)
  Output:

  ⎡ ⎣ | 3,4,0,1,2 | ⎤ ⎦