2.4 (aux list)

This module provides procedures to operate on lists.

2.4.1 Procedures

Procedure: add element lst

Append element to the list lst.

Specification:

element (anything): Any type of data. For example: "mango".

lst (list of anything): A list containing elements of any type.

Return value (list of anything): A new list containing all the elements of the initial list plus the new element at the end.

Examples:

> (add "mango" '("grape" "banana" "kiwi"))
("grape" "banana" "kiwi" "mango")
Procedure: all-true? lob

Return true if all the elements in the list lob are true.

Specification:

lob (list of boolean): For example: (list #true #true #true), (list #true #false #true).

Return value (boolean): #true if all elements are true; otherwise, #false.

Procedure: empty? list

Return true if the given list is empty. False otherwise.

Procedure: flatten los

Return a flat list containing all the atomic elements of the nested list los.

The current implementation of this procedure is from Mark Miyashita (http://markmiyashita.com/cs61a/scheme/flatten_nested_lists/).

Specification:

los (list of s-expressions): This list can be nested to any level of depth. For example: (list 'blue (list 'red (list 'yellow))).

Return value (list of atoms): For example, from the previous example: (blue red yellow).

Procedure: group los limit

Return a new list containing all the elements of the list los grouped in sets of limit elements per set or less.

Specification:

los (list of s-expressions): See the examples.

limit (natural number): The limit indicates the maximum number of elements per group. It must be greater than zero.

Return value (list of list): See the examples.

Examples:

> (group (list 'Onnet 'Twoson 'Threed 'Fourside) 2)
((Onnet Twoson) (Threed Fourside))
> (group (list 'Key 'Phone 'Pencil 'Ruler 'Tape) 3)
((Key Phone Pencil) (Ruler Tape))
Procedure: rest los

Return a list containing all the elements of the list los, except the first one.

Specification:

los (list of s-expression): See the examples.

Return value (list of s-expression): All the elements of los but the first one. If los is an empty list or has only one element, an empty list is returned.

Examples:

> (rest (list 'uva 'mora 'mango 'kiwi))
(mora mango kiwi)
> (rest (list 'vaca))
()
> (rest (list))
()
Procedure: separate los separator

Return a list containing the same elements in the list los but separated by separator.

Specification:

los (list of s-expression): See the example.

separator (s-expression): Any s-expression that will be added between the elements of the given list.

Return value (list of s-expression): A list where all elements are separated by separator. When the given list has less than two elements, return the same list.

Example:

> (separate (list "mango" "kiwi" "papaya" "lemon") "|")
("mango" "|" "kiwi" "|" "papaya" "|" "lemon")
Procedure: slice los index-a OPT: index-b

Return a list whose elements are the elements of the list los from index-a to index-b (exclusive).

Specification:

los (list of s-expression): See the examples.

index-a (natural number): The position in the list at which the slice starts.

index-b (natural number): Optional. Indicates the position in the list at which the slice ends. The element in this position is excluded from the resulting list.

If this argument is omitted, the slice will include all the elements of the list, starting at index-a.

Return value (list of s-expression): A portion of the original list.

Examples:

> (slice (list 'brass 'copper 'iron 'steel) 0 2)
(brass copper)
> (slice (list 'brass 'copper 'iron 'steel) 1)
(copper iron steel)
Procedure: substitute placeholder list replacements

Substitute all the elements in the list that match the placeholder regular expression with the replacements.

Specification:

placeholder (string): Regex string pattern used to match the placeholders in the list (see example).

list (list of string): List of strings where some of its elements are placeholders intended to be replaced (see example).

replacements (list of anything): List of any type of s-expressions that will be inserted in place of the placeholder elements in the list, in order (see example).

Return value (list of anything): The list with placeholders subtituted.

Example:

> (substitute
   "~[A-Z]+~"
   (list "Hello!" " I am a" "~AGE~" " year old " "~BEING~" ".")
   (list 5 "robot"))
("Hello!" " I am a" 5 " year old " "robot" ".")
Procedure: take n list

Return a list with the first n elements of the list.

Specification:

n (number): Natural from 1 and up indicating the number of elements to take from the list.

list (list of anything): See example.

Return value (list of anything): A new list with the n elements. If the length of the list is less than n, the list is returned instead.

Example:

> (take 3 (list 'Aplite 'Basalt 'Diorite 'Gabbro 'Norite))
(Aplite Basalt Diorite)