chickadee » ck-macros » ck-wrapper

ck-wrapper procprocedure

Wrap a procedure in a CK-macro, returning a macro transformer that can be used with define-syntax or let-syntax.

ck-wrapper requires version 0.2.0 or later of the ck-macros egg. It is considered non-portable because it depends on er-macro-transformer, which is not defined in standard R5RS. However, it should work on any Scheme which defines er-macro-transformer in the usual way.

You can wrap any other expression that evaluates to a procedure, such as a procedure name, a lambda form, a "let over lambda" expression, etc. The expression will be evaluated once, when the call to ck-wrapper is evaluated. Therefore, the procedure you are wrapping must be available in the syntax environment at macro expansion time. (In other words, you should use define-for-syntax, import-for-syntax, etc.)

Examples:

;;; Import iota from SRFI 1.
(cond-expand
  (chicken-4
   (use-for-syntax (only srfi-1 iota)))
  (chicken-5
   (import-for-syntax (only (srfi 1) iota))))

(define-syntax c-iota (ck-wrapper iota))

(ck () (c-quote (c-iota '5)))
;; ==> '(0 1 2 3 4)

;;; A helper procedure that returns a closure.
(define-for-syntax (make-adder n)
  (lambda (x) (+ x n)))

;;; Temporarily define a ck-wrapper around a closure.
(let-syntax ((c-add2 (ck-wrapper (make-adder 2))))
  ;; Map c-add2 over the result of c-iota.
  (ck () (c-quote (c-map1 '(c-add2) (c-iota '5)))))
;; ==> '(2 3 4 5 6)