; doubled pairs

(define-syntax fold-of
  (syntax-rules (range in is)
    ((_ "z" f b e) (set! b (f b e)))
    ((_ "z" f b e (v range fst pst stp) c ...)
      (let* ((x fst) (p pst) (s stp)
             (le? (if (positive? s) <= >=)))
        (do ((v x (+ v s))) ((le? p v) b)
          (fold-of "z" f b e c ...))))
    ((_ "z" f b e (v range fst pst) c ...)
      (let* ((x fst) (p pst) (s (if (< x p) 1 -1)))
        (fold-of "z" f b e (v range x p s) c ...)))
    ((_ "z" f b e (v range pst) c ...)
      (fold-of "z" f b e (v range 0 pst) c ...))
    ((_ "z" f b e (x in xs) c ...)
      (do ((t xs (cdr t))) ((null? t) b)
        (let ((x (car t)))
          (fold-of "z" f b e c ...))))
    ((_ "z" f b e (x is y) c ...)
      (let ((x y)) (fold-of "z" f b e c ...)))
    ((_ "z" f b e p? c ...)
      (if p? (fold-of "z" f b e c ...)))
    ((_ f i e c ...)
      (let ((b i)) (fold-of "z" f b e c ...)))))

(define-syntax list-of (syntax-rules ()
  ((_ arg ...) (reverse (fold-of
    (lambda (d a) (cons a d)) '() arg ...)))))

(define (g xs)
  (list-of (list y z)
    (y in xs)
    (z in xs)
    (= (+ y y) z)))

(display (g '(1 2 3 4))) (newline)
(display (g '(1 3 5 7 9))) (newline)

(define (f xs)
  (let ((ht (make-eq-hashtable)) (zs (list)))
    (do ((xs xs (cdr xs))) ((null? xs))
      (hashtable-set! ht (car xs) (car xs)))
    (do ((xs xs (cdr xs))) ((null? xs) zs)
      (when (hashtable-contains? ht (* (car xs) 2))
            (set! zs (cons (list (car xs) (* (car xs) 2)) zs))))))

(display (f '(1 2 3 4))) (newline)
(display (f '(1 3 5 7 9))) (newline)