Functional Problem Solving (CSC 151 2015F) : Reference

# Lists

## The Basics

Believe it or not, but every list operation (more or less) can be defined in terms of these five basic operations.

`null`
Standard list constant. The empty list.
```(cons value lst)```
Standard List Procedure. Create a new list by prepending `value` to the front of `lst`.
```(cdr lst)```
Standard List Procedure. Get a list the same as `lst` but without the first element.
```(car lst)```
Standard List Procedure. Get the first element of `lst`.
```(null? lst)```
Standard list predicate. Checks if `lst` is the empty list.

## Making Lists and Sublists

```(list val_0 val_1 ... val_n)```
Standard List Procedure. Create a new list of size `n`+1 of the form ```(val_0 val_1 ... val_n)```.
```(make-list n val)```
Customary List Procedure. Make a new list that consists of `n` copies of `val`.
```(list-drop lst n)```
List Procedure. Build a new list that consists of all but the first `n` elements of `lst`.
```(list-take lst n)```
List Procedure. Build a new list that consists of the first `n` elements of `lst`.

## List Iteration Procedures

```(for-each proc! lst)```
Standard Higher-Order List Procedure. Apply `proc!` to each element of the given list. Called primarily for side effects.
```(map func lst)```
Standard Higher-Order List Procedure. Create a new list, each of whose elements is computed by applying `func` to the corresponding element of `lst`.

Most of these are forthcoming.

```(list-ref lst n)```
Standard List Procedure. Get the `n`th element of `lst`. Note that elements are numbered starting at 0.
```(reverse lst)```
Standard List Procedure. Build a new list whose elements are the same as those of `lst`, but in the opposite order.
```(member? val lst)```
MediaScheme List Procedure. Determine if `val` appears in `lst`. If so, returns true (`#t`). If not, returns false (`#f`).
```(member val lst)```
Standard List Procedure. Determine if `val` appears in `lst`. If so, returns the sublist that starts with `val`. If not, returns false (`#f`).

## Hybrids of `car` and `cdr`

```(caar lst)```
Standard List Procedure. If `lst`'s first element is a list, gets the first element of that first element, the the `car` of the `car` of `lst`. If `lst` is not a list, or its first element is not a list, reports an error.
```(cadr lst)```
Standard List Procedure. Get the second element of `lst`, the `car` of the `cdr` of `lst`
```(cddr lst)```
Standard List Procedure. Get all but the first two elements of `lst`, the `cdr` of the `cdr` of `lst`
```(caddr lst)```
Standard List Procedure. Get the third element of `lst`, the `car` of the `cdr` of the `cdr` of `lst`.