[racket-dev] An Improvement to for/set

From: Daniel King (danking at ccs.neu.edu)
Date: Fri Feb 17 13:07:25 EST 2012

I've noticed myself desiring a for/set that would allow me to
optionally add zero, one, or more elements to the accumulated set
during each iteration. Is this something that other people would be
interested in having in the set library? If so, I can send a pull
request to the github repo.

My implementation should be backwards compatible with the old one. I
also created set-add* because I found myself wanting to add arbitrary
numbers of elements to sets as well.

;; set-add* : [Set X] X ... -> [Set X]
(define (set-add* s . vs)
  (for/fold ((s s)) ((v vs))
    (set-add s v)))

(define-syntax (for/set stx)
  (syntax-case stx ()
    [(_ clauses . body)
     #'(for/fold/derived stx ((accum-set (set))) clauses
                         (call-with-values (lambda () . body)
                           (curry set-add* accum-set)))]))


> (for/set ((x '(1 2 3 4 5)))
  (sqr x))
(set 1 4 9 16 25)

> (for/set ((x '(1 2 3 4 5)))
    (if (odd? x) x (values)))
(set 1 3 5)

Dan King
College of Computer and Information Science
Northeastern University

Posted on the dev mailing list.