Scheme: Non-lazy Variant of Perl's Gather-Take Defined

gather

This uses syntax-case, which is not in R5RS but which many Scheme's have.

;;; Return list of certain values occurring during evaluation of a sequence of statements.
;
;      (gather body ...) => list of values passed to `take' inside body
;
;       Examples:
;
;         (gather (take 1) (take 2) (take 3)) ; => (1 2 3)
;         (gather (take (gather (for-each take '(1 2 3))))) ; => ((1 2 3))
;
(define-syntax (gather stx)
  (syntax-case stx ()
    ((src-gather body ...)
     (with-syntax ((take (datum->syntax-object (syntax src-gather) 'take)))
       (syntax
         (letrec ((gathered '())
                  (take (lambda (e) (set! gathered (cons e gathered)))))
           body ...
           (reverse gathered)))))))