On this page:
6.1 Utilities for Natural Numbers
nat->maybe
6.2 Utilities for Lists
list-foldl
list-foldr
list-bind
list-map
list-any
list-all
list-each
list-kv-map
list-kv-any
list-kv-all
list-kv-each
list-zip-map
list-zip-any
list-zip-all
list-zip-each
6.3 Utilities for Natural Numbers and Lists Together
list-length<=nat?
nat<=list-length?
list-length=nat?
list-length<nat?
nat<list-length?

6 Utilities for Lists and Natural Numbers🔗ℹ

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

6.1 Utilities for Natural Numbers🔗ℹ

procedure

(nat->maybe n)  (maybe/c natural?)

  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.2 Utilities 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.3 Utilities 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<nat? lst n)  boolean?
  lst : list?
  n : natural?
(nat<list-length? n lst)  boolean?
  n : natural?
  lst : list?
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.