Jump to content

Loops/With multiple ranges: Difference between revisions

→‎{{header|TXR}}: Make macro work harder, so programmer just specifies loop initial and final values and step.
(→‎{{header|TXR}}: New section.)
(→‎{{header|TXR}}: Make macro work harder, so programmer just specifies loop initial and final values and step.)
Line 2,719:
 
<syntaxhighlight lang="txrlisp">(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 (initfrom testto step) rt
^((set ,var ,initfrom)
(for* () (,test)toval (,stepto) (,body)))))))))
(,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) (<= j (expt 3 3)) (inc j three)
(- seven) (<= j seven) (inc j x)
555 (<= j (- 550 y)) (inc j)1
22 (>= j -28) (dec j- three)
1927 (<= j 1939) (inc j 1)
x (>= j y) (inc j z)
(expt 11 x) (<= j (succ (expt 11 x))) (inc j)1)
(upd sum (+ (abs j)))
(if (and (< (abs prod) (ash 1 27))
Line 2,746 ⟶ 2,752:
 
<pre>sum = 348173; prod = -793618560</pre>
 
 
=={{header|uBasic/4tH}}==
543

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.