A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name.

Some attributes of a list comprehension are:

 They should be distinct from (nested) for loops and the use of map and filter functions within the syntax of the language.
 They should return either a list or an iterator (something that returns successive members of a collection, in order).
 The syntax has parts corresponding to that of set-builder notation. 


Task

Write a list comprehension that builds the list of all Pythagorean triples with elements between   1   and   n.

If the language has multiple ways for expressing such a construct (for example, direct list comprehensions and generators), write one example for each.




// USING A LIST MONAD DIRECTLY, WITHOUT SPECIAL SYNTAX FOR LIST COMPREHENSIONS
 
(function (n) {
 
    return mb(r(1,     n), function (x) {  // x <- [1..n]
    return mb(r(1 + x, n), function (y) {  // y <- [1+x..n]
    return mb(r(1 + y, n), function (z) {  // z <- [1+y..n]
 
       return x * x + y * y === z * z ? [[x, y, z]] : [];
 
    })})});
 
 
    // LIBRARY FUNCTIONS
 
    // Monadic bind for lists
    function mb(xs, f) {
        return [].concat.apply([], xs.map(f));
    }
 
    // Monadic return for lists is simply lambda x -> [x]
    // as in [[x, y, z]] : [] above
 
    // Integer range [m..n]
    function r(m, n) {
        return Array.apply(null, Array(n - m + 1))
            .map(function (n, x) {
                return m + x;
            });
    }
 
})(100);