List comprehensions: Difference between revisions

m (syntax highlighting fixup automation)
Line 2,566:
 
<syntaxhighlight lang="ti89b">seq(x^2, x, 1, 4)</syntaxhighlight>
 
=={{header|Transd}}==
The language's construct for list comprehension closely follows the established set-builder notation:
 
<pre>
FOR x IN <SET>
WHERE predicate(x)
PROJECT f(x)
</pre>
The special form of 'for' construct performs list comprehension and returns a collection with selected elements.
 
In basic form, the returned vector is created and filled automatically:
 
<syntaxhighlight lang="Scheme">
(with v (for x in Range(10)
project (* x x))
(textout v))
</syntaxhighlight>
{{out}}
<pre>
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
</pre>
 
Basic form with 'WHERE' clause:
 
<syntaxhighlight lang="Scheme">
(with v (for x in Range(10)
where (mod x 2)
project Vector<ULong>([x,(* x x)]))
(textout v))
</syntaxhighlight>
{{out}}
<pre>
[[1, 1], [3, 9], [5, 25], [7, 49], [9, 81]]
</pre>
 
In cases when more customized behaviour is required, there is an option to define
and fill the returned collection directly:
 
<syntaxhighlight lang="Scheme">
(with v (for x in Range(1 n) project<Vector<ULong>>
(for y in Range(x n) do
(for z in Range(y n)
where (== (* z z) (+ (* y y) (* x x)))
do (append @projRes Vector<ULong>([x,y,z])))))
(textout "Pythagorean triples:\n" v))
}</syntaxhighlight>
{{out}}
<pre>
Pythagorean triples:
[[3, 4, 5], [5, 12, 13], [6, 8, 10], [9, 12, 15]]
</pre>
 
=={{header|Visual Basic .NET}}==
111

edits