Loops/With multiple ranges: Difference between revisions

Content added Content deleted
(→‎{{header|TXR}}: New section.)
(→‎{{header|TXR}}: Make macro work harder, so programmer just specifies loop initial and final values and step.)
Line 2,719: Line 2,719:


<syntaxhighlight lang="txrlisp">(defmacro mfor (:form f (var . range-triplets) . forms)
<syntaxhighlight lang="txrlisp">(defmacro mfor (:form f (var . range-triplets) . forms)
(with-gensyms (body)
(with-gensyms (body toval stepval test)
^(let (,var)
^(let (,var)
(flet ((,body () ,*forms))
(flet ((,body () ,*forms))
,*(append-each ((rt (tuples 3 range-triplets)))
,*(append-each ((rt (tuples 3 range-triplets)))
(mac-param-bind f (init test step) rt
(mac-param-bind f (from to step) rt
^((set ,var ,init)
^((set ,var ,from)
(for () (,test) (,step) (,body)))))))))
(for* ((,toval ,to)
(,stepval ,step)
(,test (if (<= ,var ,toval)
(fun <=) (fun >=))))
([,test ,var ,toval])
((inc ,var ,stepval))
(,body)))))))))


(let ((prod 1) (sum 0)
(let ((prod 1) (sum 0)
(x 5) (y -5) (z -2)
(x 5) (y -5) (z -2)
(one 1) (three 3) (seven 7))
(one 1) (three 3) (seven 7))
(mfor (j (- three) (<= j (expt 3 3)) (inc j three)
(mfor (j (- three) (expt 3 3) three
(- seven) (<= j seven) (inc j x)
(- seven) seven x
555 (<= j (- 550 y)) (inc j)
555 (- 550 y) 1
22 (>= j -28) (dec j three)
22 -28 (- three)
1927 (<= j 1939) (inc j 1)
1927 1939 1
x (>= j y) (inc j z)
x y z
(expt 11 x) (<= j (succ (expt 11 x))) (inc j))
(expt 11 x) (succ (expt 11 x)) 1)
(upd sum (+ (abs j)))
(upd sum (+ (abs j)))
(if (and (< (abs prod) (ash 1 27))
(if (and (< (abs prod) (ash 1 27))
Line 2,746: Line 2,752:


<pre>sum = 348173; prod = -793618560</pre>
<pre>sum = 348173; prod = -793618560</pre>



=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==