List utilities

Library (rnrs lists (6))

[R6RS] The (rnrs lists (6))library, which contains various useful procedures that operate on lists.

Function find proc list

[R6RS] Proc should accept one argument and return a single value. Proc should not mutate list. The find procedure applies _proc_to the elements of list in order. If proc returns a true value for an element, find immediately returns that element. If proc returns #f for all elements of the list, find returns #f.

Function for-all pred list1 list2 ...

[R6RS+] Applies pred across each element of lists, and returns #f as soon as pred returns #f. If all application of pred return a non-false value, for-all returns the last result of the applications.

Function exists pred list1 list2 ...

[R6RS+] Applies pred across each element of lists, and returns as soon as pred returns a non-false value. The return value of any is the non-false value pred returned. If lists are exhausted before _pred_returns a non-false value, #f is returned.

Note: R6RS requires the same length list for for-all and exists. On Sagittarius, however, these can accept different length list and it will finish to process when the shortest list is finish to process.

Function filter proc list
Function partition proc list

[R6RS] Proc should accept one argument and return a single value.

The filter procedure applies proc to each element of list and returns a list of the elements of list for which proc returned a true value. The partition procedure also applies proc to each element of list, but returns two values, the first one a list of the elements of _list_for which proc returned a true value, and the second a list of the elements of list for which proc returned #f. In both cases, the elements of the result list(s) are in the same order as they appear in the input list. If multiple returns occur from filter or partitions, the return values returned by earlier returns are not mutated.

Function fold-left combine nil list1 list2 ...

[R6RS+] Combine must be a procedure. It should accept one more argument than there are lists and return a single value. It should not mutate the list arguments. The fold-left procedure iterates the _combine_procedure over an accumulator value and the elements of the lists from left to right, starting with an accumulator value of nil. More specifically, fold-left returns nil if the lists are empty. If they are not empty, combine is first applied to nil and the respective first elements of the lists in order. The result becomes the new accumulator value, and combine is applied to the new accumulator value and the respective next elements of the list. This step is repeated until the end of the list is reached; then the accumulator value is returned.

Function fold-right combine nil list1 list2 ...

[R6RS+] Combine must be a procedure. It should accept one more argument than there are lists and return a single value. Combine should not mutate the list arguments. The fold-right procedure iterates the combine procedure over the elements of the lists from right to left and an accumulator value, starting with an accumulator value of nil. More specifically, fold-right returns nil if the lists are empty. If they are not empty, combine is first applied to the respective last elements of the lists in order and nil. The result becomes the new accumulator value, and combine is applied to the respective previous elements of the lists and the new accumulator value. This step is repeated until the beginning of the list is reached; then the accumulator value is returned.

Note: R6RS requires the same length list for fold-left and fold-right. On Sagittarius, however, these can accept different length list and it will finish to process when the shortest list is finish to process.

Function remp proc list
Function remove obj list
Function remv obj list
Function remq obj list

[R6RS] Proc should accept one argument and return a single value. _Proc_should not mutate list.

Each of these procedures returns a list of the elements of list that do not satisfy a given condition. The remp procedure applies proc to each element of list and returns a list of the elements of list for which proc returned #f. The remove, remv, and remq procedures return a list of the elements that are not obj. The remq procedure uses eq? to compare obj with the elements of list, while remv uses eqv? and remove uses equal?. The elements of the result list are in the same order as they appear in the input list. If multiple returns occur from remp, the return values returned by earlier returns are not mutated.

Function memp proc list
Function member obj list :optional =
Function memv obj list
Function memq obj list

[R6RS+] Proc should accept one argument and return a single value. Proc should not mutate list.

These procedures return the first sublist of list whose car satisfies a given condition, where the sublists of lists are the lists returned by (list-tail _list_ _k_) for k less than the length of list. The memp procedure applies proc to the cars of the sublists of list until it finds one for which proc returns a true value. The member, memv, and memq procedures look for the first occurrence of obj. If list does not contain an element satisfying the condition, then #f (not the empty list) is returned. The member procedure uses equal? or if = is given use it to compare obj with the elements of list, while memv uses eqv? and memq uses eq?.

Function assp proc alist
Function assc obj alist :optional =
Function assv obj alist
Function assq obj alist

[R6RS+] Alist (for "association list") should be a list of pairs. Proc should accept one argument and return a single value. _Proc_should not mutate alist.

These procedures find the first pair in alist whose car field satisfies a given condition, and returns that pair without traversing alist further. If no pair in alist satisfies the condition, then #f is returned. The assp procedure successively applies proc to the car fields of alist and looks for a pair for which it returns a true value. The assoc, assv, and assq procedures look for a pair that has obj as its car. The assoc procedure uses equal? or if _=_is given use it to compare obj with the car fields of the pairs in alist, while assv uses eqv? and assq uses eq?.

Note: member and assoc procedures are the same behaviour as SRFI-1.

Function cons* obj1 obj2 ...

[R6RS] Like list, but the last argument provides the tail of the constructed list.