# Inconsistency in comprehension syntax

**URL:** <https://discourse.julialang.org/t/inconsistency-in-comprehension-syntax/4949>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 19, 2017, 4:57pm UTC](https://discourse.julialang.org/t/inconsistency-in-comprehension-syntax/4949 "2017-07-19T16:57:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [July 19, 2017, 4:57pm UTC](https://discourse.julialang.org/t/inconsistency-in-comprehension-syntax/4949/1 "2017-07-19T16:57:03Z")

</div>

Consider:

```
for k in 0:8 println(k) end # works
println([k for k in 0:8]) # works

for k in 0:8, j in 0:k println(binomial(k,j)) end # works
println([binomial(k, j) for k in 0:8, j in 0:k]) # !works

```

The last line gives: UndefVarError: k not defined.  
Why?

---

<div class="post-metadata">

**Author:** ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)\
**Post date:** [July 19, 2017, 5:12pm UTC](https://discourse.julialang.org/t/inconsistency-in-comprehension-syntax/4949/2 "2017-07-19T17:12:46Z")

</div>

> [@leiteiro](#):
>
> println([binomial(k, j) for k in 0:8, j in 0:k])

Try

```julia
julia> println([binomial(k, j) for k in 0:8, j in 0:8 if j≤k])
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 1, 3, 6, 10, 15, 21, 28, 1, 4, 10, 20, 35, 56, 1, 5, 15, 35, 70, 1, 6, 21, 56, 1, 7, 28, 1, 8, 1]

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 19, 2017, 5:14pm UTC](https://discourse.julialang.org/t/inconsistency-in-comprehension-syntax/4949/3 "2017-07-19T17:14:20Z")

</div>

Writing `[binomial(k, j) for k in 0:8, j in 0:k]` only describes what happens in the lower triangular portion of the array. Comprehensions define rectangular arrays — what should happen in the upper triangular part?

Another option: explicitly state what you want to have in the upper triangular part:

```julia
julia> [j > k ? 0 : binomial(k, j) for k in 0:8, j in 0:8]
9×9 Array{Int64,2}:
 1 0 0 0 0 0 0 0 0
 1 1 0 0 0 0 0 0 0
 1 2 1 0 0 0 0 0 0
 1 3 3 1 0 0 0 0 0
 1 4 6 4 1 0 0 0 0
 1 5 10 10 5 1 0 0 0
 1 6 15 20 15 6 1 0 0
 1 7 21 35 35 21 7 1 0
 1 8 28 56 70 56 28 8 1

```

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [July 19, 2017, 5:41pm UTC](https://discourse.julialang.org/t/inconsistency-in-comprehension-syntax/4949/4 "2017-07-19T17:41:32Z")

</div>

Another expression that works is

```julia
[binomial(k, j) for k in 0:8 for j in 0:k]

```

(replacing the comma with another `for`) but it yields a 1-d array since the iteration space is no longer a product space.
