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.




 
implement main
    open core, std
 
domains
    pythtrip = pt(integer, integer, integer).
 
class predicates
    pythTrips : (integer) -> pythtrip nondeterm (i).
 
clauses
    pythTrips(Limit) = pt(X,Y,Z) :-
        X = fromTo(1,Limit),
        Y = fromTo(X,Limit),
        Z = fromTo(Y,Limit),
        Z^2 = X^2 + Y^2.
 
    run():-
        console::init(),
        Triples = [ X || X = pythTrips(20) ],
        console::write(Triples),
        Junk = console::readLine(),
        succeed(). 
end implement main
 
goal
    mainExe::run(main::run).
 