julia> [(x, y, z) for x = 1:n for y = x:n for z = y:n if x^2 + y^2 == z^2]
6-element Array{Tuple{Int64,Int64,Int64},1}:
 (3,4,5)
 (5,12,13)
 (6,8,10)
 (8,15,17)
 (9,12,15)
 (12,16,20)

On Wed, Aug 10, 2016 at 12:58 PM, Ismael Venegas Castelló <
[email protected]> wrote:

> In order to be a little more specific I wanted to add, that it seems weird
> that I can use the variables for the if clause, but not for creating the
> other ranges, it's just that I don't know how to express myself correctly,
> I hope you can understand me.
>
>
> El miércoles, 10 de agosto de 2016, 11:56:00 (UTC-5), Ismael Venegas
> Castelló escribió:
>>
>> Is there a way to make reference of the internal variables of an array
>> comprehension? I'm trying to improve this Rosetta Code task:
>>
>>
>>    - https://rosettacode.org/wiki/List_comprehensions#Julia
>>
>>
>> const n = 20
>> sort(filter(x -> x[1] < x[2] && x[1]^2 + x[2]^2 == x[3]^2, [(a, b, c) for
>> a=1:n, b=1:n, c=1:n]))
>>
>>
>> In Python it's:
>>
>> In [2]: n = 20
>>
>> In [3]: [(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in
>> xrange(y,n+1) if x**2 + y**2 == z**2]
>> Out[3]: [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (
>> 12, 16, 20)]
>>
>>
>> I'll update the task with:
>>
>> julia> [(x, y, z) for x = 1:n, y = 1:n, z = 1:n if x < y && x^2 + y^2 ==
>> z^2] |> sort
>> 6-element Array{Tuple{Int64,Int64,Int64},1}:
>>  (3,4,5)
>>  (5,12,13)
>>  (6,8,10)
>>  (8,15,17)
>>  (9,12,15)
>>  (12,16,20)
>>
>> But I tried this and it doesn't work, I wonder why?
>>
>> julia> [(x, y, z) for x = 1:n, y = x:n, z = y:n if x^2 + y^2 == z^2]
>> ERROR: UndefVarError: x not defined
>>
>> Is there a way to do this? Thanks in advance!
>>
>

Reply via email to