mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-16 17:27:33 +02:00
WIP
This commit is contained in:
parent
045e871f7b
commit
dadf091fa5
1 changed files with 334 additions and 5 deletions
|
@ -576,46 +576,375 @@ The last element of the input list may be any value at all.
|
||||||
|
|
||||||
`concatenate!` is the linear-update variant of `concatenate`.
|
`concatenate!` is the linear-update variant of `concatenate`.
|
||||||
|
|
||||||
# unfold
|
|
||||||
|
|
||||||
# fold
|
# fold
|
||||||
|
|
||||||
# pair-fold
|
(fold kons knil clist1 clist2 ...)
|
||||||
|
|
||||||
# reduce
|
The fundamental list iterator.
|
||||||
|
|
||||||
# unfold-right
|
First, consider the single list-parameter case. If `clist1 = (e1 e2 ... en)`, then this procedure returns
|
||||||
|
|
||||||
|
(kons en ... (kons e2 (kons e1 knil)) ... )
|
||||||
|
|
||||||
|
That is, it obeys the (tail) recursion
|
||||||
|
|
||||||
|
(fold kons knil lis) = (fold kons (kons (car lis) knil) (cdr lis))
|
||||||
|
(fold kons knil '()) = knil
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
(fold + 0 lis) ; Add up the elements of LIS.
|
||||||
|
|
||||||
|
(fold cons '() lis) ; Reverse LIS.
|
||||||
|
|
||||||
|
(fold cons tail rev-head) ; See APPEND-REVERSE.
|
||||||
|
|
||||||
|
;; How many symbols in LIS?
|
||||||
|
(fold (lambda (x count) (if (symbol? x) (+ count 1) count))
|
||||||
|
0
|
||||||
|
lis)
|
||||||
|
|
||||||
|
;; Length of the longest string in LIS:
|
||||||
|
(fold (lambda (s max-len) (max max-len (string-length s)))
|
||||||
|
0
|
||||||
|
lis)
|
||||||
|
|
||||||
|
If n list arguments are provided, then the kons function must take n+1 parameters: one element from each list, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest list runs out of values:
|
||||||
|
|
||||||
|
(fold cons* '() '(a b c) '(1 2 3 4 5)) => (c 3 b 2 a 1)
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
# fold-right
|
# fold-right
|
||||||
|
|
||||||
|
(fold-right kons knil clist1 clist2 ...)
|
||||||
|
|
||||||
|
The fundamental list recursion operator.
|
||||||
|
|
||||||
|
First, consider the single list-parameter case. If `clist1 = (e1 e2 ... en)`, then this procedure returns
|
||||||
|
|
||||||
|
(kons e1 (kons e2 ... (kons en knil)))
|
||||||
|
|
||||||
|
That is, it obeys the recursion
|
||||||
|
|
||||||
|
(fold-right kons knil lis) = (kons (car lis) (fold-right kons knil (cdr lis)))
|
||||||
|
(fold-right kons knil '()) = knil
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
(fold-right cons '() lis) ; Copy LIS.
|
||||||
|
|
||||||
|
;; Filter the even numbers out of LIS.
|
||||||
|
(fold-right (lambda (x l) (if (even? x) (cons x l) l)) '() lis))
|
||||||
|
|
||||||
|
If n list arguments are provided, then the kons function must take n+1 parameters: one element from each list, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest list runs out of values:
|
||||||
|
|
||||||
|
(fold-right cons* '() '(a b c) '(1 2 3 4 5)) => (a 1 b 2 c 3)
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
|
# pair-fold
|
||||||
|
|
||||||
|
(pair-fold kons knil clist1 clist2 ...)
|
||||||
|
|
||||||
|
Analogous to fold, but kons is applied to successive sublists of the lists, rather than successive elements -- that is, kons is applied to the pairs making up the lists, giving this (tail) recursion:
|
||||||
|
|
||||||
|
(pair-fold kons knil lis) = (let ((tail (cdr lis)))
|
||||||
|
(pair-fold kons (kons lis knil) tail))
|
||||||
|
(pair-fold kons knil '()) = knil
|
||||||
|
|
||||||
|
For finite lists, the kons function may reliably apply set-cdr! to the pairs it is given without altering the sequence of execution.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
;;; Destructively reverse a list.
|
||||||
|
(pair-fold (lambda (pair tail) (set-cdr! pair tail) pair) '() lis))
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
|
|
||||||
# pair-fold-right
|
# pair-fold-right
|
||||||
|
|
||||||
|
(pair-fold-right kons knil clist1 clist2 ...)
|
||||||
|
|
||||||
|
Holds the same relationship with fold-right that pair-fold holds with fold. Obeys the recursion
|
||||||
|
|
||||||
|
(pair-fold-right kons knil lis) =
|
||||||
|
(kons lis (pair-fold-right kons knil (cdr lis)))
|
||||||
|
(pair-fold-right kons knil '()) = knil
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
(pair-fold-right cons '() '(a b c)) => ((a b c) (b c) (c))
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
|
# reduce
|
||||||
|
|
||||||
|
(reduce f ridentity list)
|
||||||
|
|
||||||
|
`reduce` is a variant of fold.
|
||||||
|
|
||||||
|
`ridentity` should be a "right identity" of the procedure `f` -- that is, for any value `x` acceptable to `f`,
|
||||||
|
|
||||||
|
(f x ridentity) = x
|
||||||
|
|
||||||
|
`reduce` has the following definition:
|
||||||
|
|
||||||
|
If `list = ()`, return `ridentity`;
|
||||||
|
|
||||||
|
Otherwise, return `(fold f (car list) (cdr list))`.
|
||||||
|
|
||||||
|
...in other words, we compute `(fold f ridentity list)`.
|
||||||
|
|
||||||
|
Note that `ridentity` is used only in the empty-list case. You typically use reduce when applying `f` is expensive and you'd like to avoid the extra application incurred when fold applies f to the head of list and the identity value, redundantly producing the same value passed in to f. For example, if f involves searching a file directory or performing a database query, this can be significant. In general, however, fold is useful in many contexts where reduce is not (consider the examples given in the fold definition -- only one of the five folds uses a function with a right identity. The other four may not be performed with reduce).
|
||||||
|
|
||||||
# reduce-right
|
# reduce-right
|
||||||
|
|
||||||
|
(reduce-right f ridentity list)
|
||||||
|
|
||||||
|
`reduce-right` is the `fold-right` variant of `reduce`. It obeys the following definition:
|
||||||
|
|
||||||
|
(reduce-right f ridentity '()) = ridentity
|
||||||
|
(reduce-right f ridentity '(e1)) = (f e1 ridentity) = e1
|
||||||
|
(reduce-right f ridentity '(e1 e2 ...)) =
|
||||||
|
(f e1 (reduce f ridentity (e2 ...)))
|
||||||
|
|
||||||
|
...in other words, we compute (fold-right f ridentity list).
|
||||||
|
|
||||||
|
;; Append a bunch of lists together.
|
||||||
|
;; I.e., (apply append list-of-lists)
|
||||||
|
(reduce-right append '() list-of-lists)
|
||||||
|
|
||||||
|
# unfold
|
||||||
|
|
||||||
|
(unfold p f g seed [tail-gen])
|
||||||
|
|
||||||
|
`unfold` is best described by its basic recursion:
|
||||||
|
|
||||||
|
(unfold p f g seed) =
|
||||||
|
(if (p seed) (tail-gen seed)
|
||||||
|
(cons (f seed)
|
||||||
|
(unfold p f g (g seed))))
|
||||||
|
|
||||||
|
- `p` determines when to stop unfolding.
|
||||||
|
- `f` maps each seed value to the corresponding list element.
|
||||||
|
- `g` maps each seed value to next seed value.
|
||||||
|
- `seed` is the "state" value for the unfold.
|
||||||
|
- `tail-gen` creates the tail of the list; defaults to (lambda (x) '())
|
||||||
|
|
||||||
|
In other words, we use g to generate a sequence of seed values
|
||||||
|
|
||||||
|
seed, g(seed), g2(seed), g3(seed), ...
|
||||||
|
|
||||||
|
These seed values are mapped to list elements by f, producing the elements of the result list in a left-to-right order. P says when to stop.
|
||||||
|
|
||||||
|
unfold is the fundamental recursive list constructor, just as fold-right is the fundamental recursive list consumer. While unfold may seem a bit abstract to novice functional programmers, it can be used in a number of ways:
|
||||||
|
|
||||||
|
;; List of squares: 1^2 ... 10^2
|
||||||
|
(unfold (lambda (x) (> x 10))
|
||||||
|
(lambda (x) (* x x))
|
||||||
|
(lambda (x) (+ x 1))
|
||||||
|
1)
|
||||||
|
|
||||||
|
(unfold null-list? car cdr lis) ; Copy a proper list.
|
||||||
|
|
||||||
|
;; Read current input port into a list of values.
|
||||||
|
(unfold eof-object? values (lambda (x) (read)) (read))
|
||||||
|
|
||||||
|
;; Copy a possibly non-proper list:
|
||||||
|
(unfold not-pair? car cdr lis
|
||||||
|
values)
|
||||||
|
|
||||||
|
;; Append HEAD onto TAIL:
|
||||||
|
(unfold null-list? car cdr head
|
||||||
|
(lambda (x) tail))
|
||||||
|
|
||||||
|
Interested functional programmers may enjoy noting that fold-right and unfold are in some sense inverses. That is, given operations knull?, kar, kdr, kons, and knil satisfying
|
||||||
|
|
||||||
|
(kons (kar x) (kdr x)) = x and (knull? knil) = #t
|
||||||
|
|
||||||
|
then
|
||||||
|
|
||||||
|
(fold-right kons knil (unfold knull? kar kdr x)) = x
|
||||||
|
|
||||||
|
and
|
||||||
|
|
||||||
|
(unfold knull? kar kdr (fold-right kons knil x)) = x.
|
||||||
|
|
||||||
|
This combinator sometimes is called an "anamorphism;" when an explicit tail-gen procedure is supplied, it is called an "apomorphism."
|
||||||
|
|
||||||
|
# unfold-right
|
||||||
|
|
||||||
|
(unfold-right p f g seed [tail])
|
||||||
|
|
||||||
|
unfold-right constructs a list with the following loop:
|
||||||
|
(let lp ((seed seed) (lis tail))
|
||||||
|
(if (p seed) lis
|
||||||
|
(lp (g seed)
|
||||||
|
(cons (f seed) lis))))
|
||||||
|
- `p` determines when to stop unfolding.
|
||||||
|
- `f` maps each seed value to the corresponding list element.
|
||||||
|
- `g` maps each seed value to next seed value.
|
||||||
|
- `seed` is the "state" value for the unfold.
|
||||||
|
- `tail` is the list terminator; defaults to '().
|
||||||
|
|
||||||
|
In other words, we use g to generate a sequence of seed values
|
||||||
|
|
||||||
|
seed, g(seed), g2(seed), g3(seed), ...
|
||||||
|
|
||||||
|
These seed values are mapped to list elements by f, producing the elements of the result list in a right-to-left order. P says when to stop.
|
||||||
|
unfold-right is the fundamental iterative list constructor, just as fold is the fundamental iterative list consumer. While unfold-right may seem a bit abstract to novice functional programmers, it can be used in a number of ways:
|
||||||
|
|
||||||
|
;; List of squares: 1^2 ... 10^2
|
||||||
|
(unfold-right zero?
|
||||||
|
(lambda (x) (* x x))
|
||||||
|
(lambda (x) (- x 1))
|
||||||
|
10)
|
||||||
|
|
||||||
|
;; Reverse a proper list.
|
||||||
|
(unfold-right null-list? car cdr lis)
|
||||||
|
|
||||||
|
;; Read current input port into a list of values.
|
||||||
|
(unfold-right eof-object? values (lambda (x) (read)) (read))
|
||||||
|
|
||||||
|
;; (append-reverse rev-head tail)
|
||||||
|
(unfold-right null-list? car cdr rev-head tail)
|
||||||
|
|
||||||
|
Interested functional programmers may enjoy noting that fold and unfold-right are in some sense inverses. That is, given operations knull?, kar, kdr, kons, and knil satisfying
|
||||||
|
|
||||||
|
(kons (kar x) (kdr x)) = x and (knull? knil) = #t
|
||||||
|
|
||||||
|
then
|
||||||
|
|
||||||
|
(fold kons knil (unfold-right knull? kar kdr x)) = x
|
||||||
|
|
||||||
|
and
|
||||||
|
|
||||||
|
(unfold-right knull? kar kdr (fold kons knil x)) = x.
|
||||||
|
|
||||||
|
This combinator presumably has some pretentious mathematical name; interested readers are invited to communicate it to the author.
|
||||||
|
|
||||||
# append-map
|
# append-map
|
||||||
|
|
||||||
|
(append-map f clist1 clist2 ...)
|
||||||
|
|
||||||
|
Equivalent to
|
||||||
|
|
||||||
|
(apply append (map f clist1 clist2 ...))
|
||||||
|
|
||||||
|
Map `f` over the elements of the lists, just as in the `map` function. However, the results of the applications are appended together to make the final result. `append-map` uses `append` to append the results together.
|
||||||
|
|
||||||
|
The dynamic order in which the various applications of f are made is not specified.
|
||||||
|
|
||||||
# append-map!
|
# append-map!
|
||||||
|
|
||||||
|
(append-map! f clist1 clist2 ...)
|
||||||
|
|
||||||
|
Equivalent to
|
||||||
|
|
||||||
|
(apply append! (map f clist1 clist2 ...))
|
||||||
|
|
||||||
|
Map `f` over the elements of the lists, just as in the `map` function. However, the results of the applications are appended together to make the final result. `append-map!` uses `append!` to append the results together.
|
||||||
|
|
||||||
|
The dynamic order in which the various applications of f are made is not specified.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
(append-map! (lambda (x) (list x (- x))) '(1 3 8))
|
||||||
|
=> (1 -1 3 -3 8 -8)
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
# map!
|
# map!
|
||||||
|
|
||||||
|
(map! f list1 clist2 ...)
|
||||||
|
|
||||||
|
Linear-update variant of `map` -- `map!` is allowed, but not required, to alter the cons cells of list1 to construct the result list.
|
||||||
|
|
||||||
|
The dynamic order in which the various applications of f are made is not specified. In the n-ary case, clist2, clist3, ... must have at least as many elements as list1.
|
||||||
|
|
||||||
# pair-for-each
|
# pair-for-each
|
||||||
|
|
||||||
|
(pair-for-each f clist1 clist2 ...)
|
||||||
|
|
||||||
|
Like for-each, but f is applied to successive sublists of the argument lists. That is, f is applied to the cons cells of the lists, rather than the lists' elements. These applications occur in left-to-right order.
|
||||||
|
|
||||||
|
The f procedure may reliably apply set-cdr! to the pairs it is given without altering the sequence of execution.
|
||||||
|
|
||||||
|
(pair-for-each (lambda (pair) (display pair) (newline)) '(a b c)) ==>
|
||||||
|
(a b c)
|
||||||
|
(b c)
|
||||||
|
(c)
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
# filter-map
|
# filter-map
|
||||||
|
|
||||||
|
(filter-map f clist1 clist2 ...)
|
||||||
|
|
||||||
|
Like `map`, but only true values are saved.
|
||||||
|
|
||||||
|
(filter-map (lambda (x) (and (number? x) (* x x))) '(a 1 b 3 c 7))
|
||||||
|
=> (1 9 49)
|
||||||
|
|
||||||
|
The dynamic order in which the various applications of f are made is not specified.
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
# map-in-order
|
# map-in-order
|
||||||
|
|
||||||
|
(map-in-order f clist1 clist2 ...)
|
||||||
|
|
||||||
|
A variant of the map procedure that guarantees to apply f across the elements of the listi arguments in a left-to-right order. This is useful for mapping procedures that both have side effects and return useful values.
|
||||||
|
|
||||||
|
At least one of the list arguments must be finite.
|
||||||
|
|
||||||
# filter
|
# filter
|
||||||
|
|
||||||
|
(filter pred list)
|
||||||
|
|
||||||
|
Return all the elements of list that satisfy predicate pred. The list is not disordered -- elements that appear in the result list occur in the same order as they occur in the argument list. The returned list may share a common tail with the argument list. The dynamic order in which the various applications of pred are made is not specified.
|
||||||
|
|
||||||
|
(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)
|
||||||
|
|
||||||
# partition
|
# partition
|
||||||
|
|
||||||
|
(partition pred list)
|
||||||
|
|
||||||
|
Partitions the elements of list with predicate pred, and returns two values: the list of in-elements and the list of out-elements. The list is not disordered -- elements occur in the result lists in the same order as they occur in the argument list. The dynamic order in which the various applications of pred are made is not specified. One of the returned lists may share a common tail with the argument list.
|
||||||
|
|
||||||
|
(partition symbol? '(one 2 3 four five 6)) =>
|
||||||
|
(one four five)
|
||||||
|
(2 3 6)
|
||||||
|
|
||||||
# remove
|
# remove
|
||||||
|
|
||||||
|
(remove pred list)
|
||||||
|
|
||||||
|
Returns list without the elements that satisfy predicate pred:
|
||||||
|
(lambda (pred list) (filter (lambda (x) (not (pred x))) list))
|
||||||
|
The list is not disordered -- elements that appear in the result list occur in the same order as they occur in the argument list. The returned list may share a common tail with the argument list. The dynamic order in which the various applications of pred are made is not specified.
|
||||||
|
|
||||||
|
(remove even? '(0 7 8 8 43 -4)) => (7 43)
|
||||||
|
|
||||||
# filter!
|
# filter!
|
||||||
|
|
||||||
|
(filter! pred list)
|
||||||
|
|
||||||
|
Linear-update variant of `filter`. This procedure is allowed, but not required, to alter the cons cells in the argument list to construct the result lists.
|
||||||
|
|
||||||
# partition!
|
# partition!
|
||||||
|
|
||||||
|
(partition! pred list)
|
||||||
|
|
||||||
|
Linear-update variant of `partition`. This procedure is allowed, but not required, to alter the cons cells in the argument list to construct the result lists.
|
||||||
|
|
||||||
# remove!
|
# remove!
|
||||||
|
|
||||||
|
(remove! pred list)
|
||||||
|
|
||||||
|
Linear-update variant of `remove`. This procedure is allowed, but not required, to alter the cons cells in the argument list to construct the result lists.
|
||||||
|
|
||||||
# find
|
# find
|
||||||
|
|
||||||
# find-tail
|
# find-tail
|
||||||
|
|
Loading…
Add table
Reference in a new issue