chickadee » ck-macros » c-make-next

(c-make-next '(X1 X2 ...)) → '(syntax-rules ...)syntax

Build a CK-macro that yields the next item in a sequence. Added in version 0.3.0.

Given a list of unique items, yields a syntax-rules form for a CK-macro with the following behavior:

  • Given an item in the list, yields the item following it. Yields '#f if given the final item or an item not in the list.

E.g. with a list of increasing integers, the CK-macro behaves like c-dadd1. With a list of decreasing integers, it behaves like c-dsub1.

The list must have at least two items, with no duplicates. The items should be literal constants: booleans, numbers, strings, characters; or (possibly nested) pairs, lists, or vectors of those things. Symbols are allowed but the result may not be what you expect because they are treated as identifiers in the patterns.

Be advised that compilation can be very slow if there are many items because it generates a syntax-rules with many branches.

(ck () `(define-syntax c-next-square
          ,(c-make-next '(0 1 4 9 16))))

;; The above is basically equivalent to:
;; (define-syntax c-next-square
;;  (syntax-rules (quote)
;;    ((_ s '0) (ck s '1))
;;    ((_ s '1) (ck s '4))
;;    ((_ s '4) (ck s '9))
;;    ((_ s '9) (ck s '16))
;;    ((_ s otherwise) (ck s '#f))))

(ck () (c-next-square '4))
;; ==> '9
(ck () (c-next-square '16))
;; ==> '#f