### 6Utilities for Lists and Natural Numbers

 (require lathe-comforts/list) package: lathe-comforts-lib

#### 6.1Utilities for Natural Numbers

 procedure n : natural?
Finds the predecessor of the given natural number, if any. If there is one, the result is a just? of that value. If there isn’t, the result is a nothing?.

#### 6.2Utilities for Lists

 procedure(list-foldl state lst func) → any/c state : any/c lst : list? func : (-> any/c any/c any/c)
Returns the final state value obtained by initializing the state as the given value state and updating the state by calling the given function func with each element of the list lst from first to last.

The function func is passed two values: The current state value and the list element to process. Its return value is used as the updated state value.

Racket already provides a foldl function, but this one arranges the function to be the last argument.

 procedure(list-foldr lst state func) → any/c lst : list? state : any/c func : (-> any/c any/c any/c)
Returns the final state value obtained by initializing the state as the given value state and updating the state by calling the given function func with each element of the list lst from last to first.

The function func is passed two values: The list element to process and the current state value. Its return value is used as the updated state value.

Racket already provides a foldr function, but this one arranges the function to be the last argument, and it switches the order of the list and initial state arguments so the initial state is visually closer to the side of the list it interacts with.

 procedure(list-bind lst func) → list? lst : list? func : (-> any/c list?)
Creates a list by replacing every element of the given list with zero, one, or more elements according to the given function.

The elements are processed from first to last.

Racket already provides an append-map function, but this one arranges the function to be the last argument.

 procedure(list-map lst func) → list? lst : list? func : (-> any/c any/c)
Creates a list by replacing every element of the given list with another according to the given function.

The elements are processed from first to last.

Racket already provides a map function, but this one arranges the function to be the last argument.

 procedure(list-any lst func) → any lst : list? func : (-> any/c any)
Iterates over the given list’s elements from first to last and calls the given function on each one, stopping early if the function returns a non-#f value for any but the last element.

If the list is empty, the overall result is #f. Otherwise, if the function does return a non-#f value for any but the last element, then that value is used as the overall result. Otherwise, the (possibly multiple) return values of calling the function with the last element are used as the result.

Racket already provides an ormap function, but this one arranges the function to be the last argument.

 procedure(list-all lst func) → any lst : list? func : (-> any/c any)
Iterates over the given list’s elements from first to last and calls the given function on each one, stopping early if the function returns #f for any but the last element.

If the list is empty, the overall result is #t. Otherwise, if the function does return #f for any but the last element, then the overall result is #f. Otherwise, the (possibly multiple) return values of calling the function with the last element are used as the result.

Racket already provides an andmap function, but this one arranges the function to be the last argument.

 procedure(list-each lst func) → void? lst : list? func : (-> any/c any)
Iterates over the given list’s elements from first to last and calls the given procedure on each one. Ignores the procedure’s results.

Racket already provides a for-each function, but this one arranges the function to be the last argument.

 procedure(list-kv-map lst func) → list? lst : list? func : (-> natural? any/c any/c)
Returns a list constructed by iterating over the given list’s entries from first to last, calling the given function with each entry’s index and value, and collecting the results.

 procedure(list-kv-any lst func) → any lst : list? func : (-> natural? any/c any)
Iterates over the given list’s entries from first to last and calls the given function on each entry’s index and value, stopping early if the function returns a non-#f value for any but the last element.

If the list is empty, the overall result is #f. Otherwise, if the function does return a non-#f value for any but the last entry, then that value is used as the overall result. Otherwise, the (possibly multiple) return values of calling the function with the last entry’s index and value are used as the result.

 procedure(list-kv-all lst func) → any lst : list? func : (-> natural? any/c any)
Iterates over the given list’s entries from first to last and calls the given function on each entry’s index and value, stopping early if the function returns #f for any but the last element.

If the list is empty, the overall result is #t. Otherwise, if the function does return #f for any but the last entry, then the overall result is #f. Otherwise, the (possibly multiple) return values of calling the function with the last entry’s index and value are used as the result.

 procedure(list-kv-each lst func) → void? lst : list? func : (-> natural? any/c any)
Iterates over the given list’s entries from first to last and calls the given procedure on each entry’s index and value. Ignores the procedure’s results.

 procedure(list-zip-map a b func) → list? a : list? b : list? func : (-> any/c any/c any/c)
Creates a list by replacing every pair of elements of the given two lists (which must be of the same length) with a single element according to the given function.

The elements are processed from first to last.

Racket already provides a map function, but this one arranges the function to be the last argument.

 procedure(list-zip-any a b func) → any a : list? b : list? func : (-> any/c any/c any)
Iterates over the given two lists (which must be of the same length) and calls the given function on each pair of elements from first to last, stopping early if the function returns a non-#f value for any but the last pair of elements.

If the lists are empty, the overall result is #f. Otherwise, if the function does return a non-#f value for any but the last pair of elements, then that value is used as the overall result. Otherwise, the (possibly multiple) return values of calling the function with the last pair of elements are used as the result.

Racket already provides an ormap function, but this one arranges the function to be the last argument.

 procedure(list-zip-all a b func) → any a : list? b : list? func : (-> any/c any/c any)
Iterates over the given two lists (which must be of the same length) and calls the given function on each pair of elements from first to last, stopping early if the function returns #f for any but the last pair of elements.

If the lists are empty, the overall result is #t. Otherwise, if the function does return #f for any but the last pair of elements, then the overall result is #f. Otherwise, the (possibly multiple) return values of calling the function with the last pair of elements are used as the result.

Racket already provides an andmap function, but this one arranges the function to be the last argument.

 procedure(list-zip-each a b func) → void? a : list? b : list? func : (-> any/c any/c any)
Iterates over the given two lists (which must be of the same length) and calls the given procedure on each pair of elements from first to last. Ignores the procedure’s results.

Racket already provides a for-each function, but this one arranges the function to be the last argument.

#### 6.3Utilities for Natural Numbers and Lists Together

 procedure(list-length<=nat? lst n) → boolean? lst : list? n : natural? (nat<=list-length? n lst) → boolean? n : natural? lst : list? (list-length=nat? lst n) → boolean? lst : list? n : natural? (list-length
These procedures compare a given natural number with the length of a given list. This can be more efficient than comparing the number to (length lst) when that length is larger than the number.