This site is a static rendering of the Trac instance that was used by R7RS-WG1 for its work on R7RS-small (PDF), which was ratified in 2013. For more information, see Home.

Source for wiki CyclesMedernach version 7

author

cowan

comment


    

ipnr

198.185.18.207

name

CyclesMedernach

readonly

0

text

== Cycle type ==

Cycles are an immutable ordered, but unindexed, container type similar to circular lists.

The idea is to make a cycle type disjoint from the other types, only interface is standardized. Scheme implementations are free to use any underlying structure to achieve it.

=== Constructors and type conversion === 

`(make-cycle `''list''`)`

Returns a newly allocated cycle whose elements are the elements of ''list''.  Order is preserved.

`(cycle `''element'' ...`)`

Returns a newly allocated cycle containing ''elements''.  Order is preserved.

`(cycle->list `''cycle''`)`

Returns a newly allocated list whose elements are those of ''cycle''.  Order is preserved.

=== Predicates ===

`(cycle? `''obj''`)`

Returns `#t` if obj is a cycle, and otherwise returns `#f`.

`(cycle=? `''equivalence cycle,,1,, cycle,,2,,''`)`

Return `#t` if ''cycle,,1,,'' and ''cycle,,2,,'' contain the same values (in the sense of the ''equivalence'' predicate) in the same order, independent of their rotations; otherwise return `#f`.

Example:  `(cycle=? (make-cycle 1 2 3) (make-cycle 3 1 2)) => t`

=== Accessors ===

`(cycle-front `''cycle''`)`

Returns the element in front of ''cycle''.

`(cycle-take `''cycle k''`)`

Returns a list containing the first ''k'' elements of ''cycle''.

`(cycle-reverse-take `''cycle k''`)`

Returns a list containing the last ''k'' elements of ''cycle'' in reverse order.

`(cycle-drop `''cycle k''`)`

Returns a list containing all but the first ''k'' elements of ''cycle''.

`(cycle-reverse-drop `''cycle k''`)`

Returns a list containing all but the last ''k'' elements of ''cycle'' in reverse order.

=== Rotation ===

`(cycle-rotate `''cycle''` `''k''`)`

''cycle-rotate'' returns a cycle obtained from ''cycle'' by a rotation of ''k'' steps forward, where ''k'' is an exact non-negative integer.

`(cycle-reverse-rotate `''cycle''` `''k''`)`

''cycle-reverse-rotate'' returns a cycle obtained from ''cycle'' by a rotation of ''k'' steps backward, where ''k'' is an exact non-negative integer.

`(cycle-find `''cycle predicate''`)`

''cycle-find'' returns a cycle obtained from ''cycle'' by a forward rotation of as few steps as possible such that the value of `cycle-front` satisfies `predicate`.

`(cycle-reversed-find `''cycle predicate''`)`

''cycle-reversed-find'' returns a cycle obtained from ''cycle'' by a backward rotation of as few steps as possible such that the value of `(cycle-front ` ''cycle''`)` satisfies `predicate`.

=== Functional update ===

`(cycle-remove-front `''cycle''`)`

''cycle-remove-front'' returns a newly allocated cycle obtained from ''cycle'', but lacking the front element.

`(cycle-insert `''cycle''` `''obj''`)`

Return a newly allocated cycle obtained from ''cycle'', but where ''obj'' is added as the front element.

=== Mapping and folding ===

`(cycle-map `''proc''` `''cycle'' ...`)`

All cycles must have the same length, and ''proc'' must be a procedure taking as many arguments as there are ''cycles'' and returning a single value. `cycle-map` applies ''proc'' to the elements of the cycle(s) and returns a newly allocated cycle of the corresponding results.

`(cycle-for-each `''proc''` `''cycle'' ...`)`

All cycles must have the same length, and ''proc'' must be a procedure taking as many arguments as there are ''cycles''. `cycle-for-each` applies ''proc'' to the elements of the cycle(s) and discards any results.

`(cycle-fold `''proc''` `''nil''` `''cycle'' ...`)`

All cycles must have the same length, and ''proc'' must be a procedure taking as many arguments as there are ''cycles'', plus one additional argument, and returning a single value. `cycle-fold` applies ''proc'' to the elements of the cycle(s) and the value previously returned by ''proc''.  On the first call to ''proc'', the additional argument is ''nil''.  Returns the result of the final call to ''proc''.

`(cycle-unfold `''continue? successor mapper seed''`)`

Start with an empty list.  If the result of applying the predicate ''continue?'' to ''seed'' is `#f`, apply `make-cycle` to the reversed list and return the result.  Otherwise, apply the procedure ''mapper'' to ''seed''.  The value of ''mapper'' is prepended onto the list.  Then get a new seed by applying the procedure ''successor'' to ''seed'', and repeat this algorithm.

=== The whole cycle ===

`(cycle-length `''cycle''`)`

Returns the number of elements in ''cycle''.

`(cycle-reverse `''cycle''`)`

Return a newly allocated cycle containing the same elements as this cycle but in reverse order.  Navigating a reversed cycle forward is the same as navigating the original cycle backward.

`(cycle-count `''cycle predicate''`)`

Returns the number of elements of ''cycle'' which satisfy ''predicate''.

`(cycle-any `''cycle predicate''`)`

Returns `#t` if any element of ''cycle'' satisfies ''predicate'', and `#f` otherwise.

`(cycle-every `''cycle predicate''`)`

Returns `#t` if every element of ''cycle'' satisfies ''predicate'', and `#f` otherwise.

`(cycle-filter `''cycle predicate''`)`

Returns a newly allocated cycle containing those elements which satisfy ''predicate''.  Order is preserved.

`(cycle-remove `''cycle predicate''`)`

Returns a newly allocated cycle containing those elements which do not satisfy ''predicate''.  Order is preserved.

`(cycle-partition `''cycle predicate''`)`

Returns two values, a newly allocated cycle containing those elements which satisfy ''predicate'', and another newly allocated cycle containing those elements which do not.  Order is preserved.

== External representation ==

The cycle composed of elements 1, 2 and 3 with 1 in front can be written as following:

  `#cycle(1 2 3)`

time

2013-05-21 02:11:33

version

7