chickadee » ck-macros » c-unfold

(c-unfold '(P ...) '(F ...) '(G ...) SEED) → listsyntax
(c-unfold '(P ...) '(F ...) '(G ...) SEED '(TAIL-GEN ...)) → listsyntax

Generate a list by recursively "unfolding" from a seed. Analogous to unfold from SRFI 1. Added in version 0.3.0.

Takes several operations which are called with the seed:

  • (P ... SEED) should yield non-'#f if it is time to stop generating.
  • (F ... SEED) should yield an item to be appended to the list.
  • (G ... SEED) should yield the next SEED.
  • (TAIL-GEN ... SEED) should yield the tail of the list. Called with the final SEED after P yields non-'#f. If omitted, '(TAIL-GEN ...) defaults to '(c-constantly '()).
(ck () (c-quote
        (c-unfold '(c-> '1)                        ; P
                  '(c-identity)                    ; F
                  '(c-dsub1)                       ; G
                  '10                              ; SEED
                  '(c-constantly '(BLASTOFF!)))))  ; TAIL-GEN
;; ==> '(10 9 8 7 6 5 4 3 2 1 BLASTOFF!)