RosettaCodeData/Task/Loops-With-multiple-ranges/TXR/loops-with-multiple-ranges.txr
2023-07-01 13:44:08 -04:00

30 lines
1.1 KiB
Text

(defmacro mfor (:form f (var . range-triplets) . forms)
(with-gensyms (body toval stepval test)
^(let (,var)
(flet ((,body () ,*forms))
,*(append-each ((rt (tuples 3 range-triplets)))
(mac-param-bind f (from to step) rt
^((set ,var ,from)
(for* ((,toval ,to)
(,stepval ,step)
(,test (if (<= ,var ,toval)
(fun <=) (fun >=))))
([,test ,var ,toval])
((inc ,var ,stepval))
(,body)))))))))
(let ((prod 1) (sum 0)
(x 5) (y -5) (z -2)
(one 1) (three 3) (seven 7))
(mfor (j (- three) (expt 3 3) three
(- seven) seven x
555 (- 550 y) 1
22 -28 (- three)
1927 1939 1
x y z
(expt 11 x) (succ (expt 11 x)) 1)
(upd sum (+ (abs j)))
(if (and (< (abs prod) (ash 1 27))
(nzerop j))
(upd prod (* j))))
(put-line `sum = @sum; prod = @prod`))