Sum of a series: Difference between revisions

(→‎{{header|TXR}}: Variant C.)
Line 1,566:
Reduce with + operator over a lazily generated list.
 
Variant AA1: limit the list generation inside the <code>gen</code> operator.
 
<lang shtxr>txr -c '@(bind sum @[reduce-left + (let ((i 0)) (gen (< i 1000) (/ 1.0 (* (inc i) i)))) 0])'
sum="1.64393456668156"</lang>
</lang>
 
Variant BA2: generate infinite list, but take only the first 1000 items using <code>[list-expr 0..999]</code>.
 
<lang shtxr>txr -c '@(bind sum @[reduce-left + [(let ((i 0)) (gen t (/ 1.0 (* (inc i) i)))) 0..999] 0])'
sum="1.64393456668156"</lang>
 
</lang>
Variant B: generate lazy integer range, and pump it through a series of function with the help of the <code>chain</code> functional combinator and the <code>op</code> partial evaluation/binding operator.
 
<lang txr>txr -c '@(bind sum @[[chain range (op mapcar (op / 1.0 (* @1 @1))) (op reduce-left + @1 0)] 1 1000])'
sum="1.64393456668156"</lang>
 
=={{header|UnixPipes}}==
543

edits