views:

134

answers:

1

I am learning hygiene and I tried to make a simple for loop in Scheme. I want to support three kinds of constructs as shown in example below

(for i = 1 : (< i 4) : (++ i)
  (printf "Multiplication Table for ~s\n" i)
  (for j = 1 to 5
    (printf "~s * ~s = ~s\n" i j (* i j))))

I want to also support for loops with filters like this:

(for k = 1 : 10 : (list even? (λ(x) (> x 4))) : (++ k)
  (print k))

I have this but I can see a lot of repetition. Please help me remove redundancies.

(define-syntax for
  (syntax-rules (= to :)
    [(for x = initial : final : conditions : increment body ...)
     (letrec ([loop (λ(x)
                      (when (<= x final)
                        (when (andmap (λ(condition) (condition x)) conditions)
                          body ...)
                        (loop increment)))])
       (loop initial))]
    [(for x = initial : condition : increment body ...)
     (letrec ([loop (λ(x)
                      (when condition
                        body ...
                        (loop increment)))])
       (loop initial))]
    [(for x = initial to n body)
     (for x = initial : (<= x n) : (+ x 1) body)]))
+6  A: 

I don't see a lot of repetition here. Just one. It can be removed that way:

(define-syntax for
  (syntax-rules (= to :)
    [(for x = initial : final : conditions : increment body ...)
     (for x = initial : (<= x final): increment
          (when (andmap (λ(condition) (condition x)) conditions)
            body ...))]
    [(for x = initial : condition : increment body ...)
     (letrec ([loop (λ(x)
                      (when condition
                        body ...
                        (loop increment)))])
       (loop initial))]
    [(for x = initial to n body)
     (for x = initial : (<= x n) : (+ x 1) body)]))
Alexey Voinov
This is pretty good. Thanks.
kunjaan