chickadee » special-case » with-special-cased-procedures

(with-special-cased-procedures ((var predefined) ...) body ...)syntax

Examples:

Defines the SRFI-1 `member' procedure with optional equality argument, but duplicates the inner loop conditioned on eq, special-casing one on the eq? case.

(define (member key ls . o)
  (let ((eq (if (pair? o) (car o) equal?)))
    (with-special-cased-procedures ((eq eq?))
      (let lp ((ls ls))
        (cond
         ((null? ls)
          #f)
         ((eq key (car ls))
          ls)
         (else
          (lp (cdr ls))))))))

Defines a for-each variation that optionally writes trace output every 100 elements. Two versions of the inner loop get created, one with and one without the tracing call, so that there's no extra overhead when not tracing.

(define (for-each/trace proc ls . o)
  (with-special-cased-conditional (trace? (and (pair? o) (car o)))
    (let lp ((ls ls) (i 0))
      (if (and (trace?) (zero? (modulo i 100)))
          (print "iteration: " i))
      (cond
       ((pair? ls)
        (proc (car ls))
        (lp (cdr ls) (+ i 1)))))))