# Comprehension syntax

**URL:** <https://forum.elixirforum.com/t/comprehension-syntax/43075>\
**Category:** Questions\
**Tags:** comprehensions, syntax, list-comprehension\
**Created:** [October 13, 2021, 8:36pm UTC](https://forum.elixirforum.com/t/comprehension-syntax/43075 "2021-10-13T20:36:27Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [October 14, 2021, 8:52am UTC](https://forum.elixirforum.com/t/comprehension-syntax/43075/7 "2021-10-14T08:52:08Z")

</div>

Yes they are the same. A `do` block after a call adds the `[do: ...]` argument to the function/macro call on the left.

So if you call this (notice the parentheses for the `if` call)

```elixir
if(a == 1) do
  :ok
end

```

The compiler kind of rewrites it to `if(a == 1, [do: :ok])`

This works for functions too:

```elixir
defmodule Test do
  def myfun(value, block) do
    value |> IO.inspect(label: "value")
    block |> IO.inspect(label: "block")
  end
end

Test.myfun :some_value do
  a = 1
  b = 2
  a + b
end

```

This will output the following:

```elixir
value: :some_value
block: [do: 3]

```

But as `myfun/2` is a function and not a macro, the block is evaluated _before_ being passed to the function, hence you see `[do: 3]`, whereas with macros the quoted block is passed. This syntactic sugar is then less useful for functions.

The `for` macro is special, because if you pass `into: %{}, do: stuff()`, the `do` and `into` are in the same keyword list ; if you pass `into: %{}` and then a `do end` block, the compiler will add another argument to the `for()` call, containing just the `do` block:

```elixir
iex(1)> quote do
...(1)> for a <- 1..2, into: %{} do
...(1)> {a, a*a} 
...(1)> end
...(1)> end
{:for, [],
 [
   {:<-, [],
    [{:a, [], Elixir}, {:.., [context: Elixir, import: Kernel], [1, 2]}]},
   [into: {:%{}, [], []}],
   [
     do: {{:a, [], Elixir},
      {:*, [context: Elixir, import: Kernel],
       [{:a, [], Elixir}, {:a, [], Elixir}]}}
   ]
 ]}
iex(2)> quote do                   
...(2)> for a <- 1..2, into: %{}, do: {a, a*a}
...(2)> end
{:for, [],
 [
   {:<-, [],
    [{:a, [], Elixir}, {:.., [context: Elixir, import: Kernel], [1, 2]}]},
   [
     into: {:%{}, [], []},
     do: {{:a, [], Elixir},
      {:*, [context: Elixir, import: Kernel],
       [{:a, [], Elixir}, {:a, [], Elixir}]}}
   ]
 ]}

```

I guess that is why `for` is defined in `Kernel.SpecialForms` as an [error](https://github.com/elixir-lang/elixir/blob/a64d42f5d3cb6c32752af9d3312897e8cd5bb7ec/lib/elixir/lib/kernel/special_forms.ex#L1475) because it is handled at the compiler level.

---

_[View the full topic](https://forum.elixirforum.com/t/comprehension-syntax/43075)._
