# Stating my opininon

**URL:** <https://discourse.julialang.org/t/stating-my-opininon/15942>\
**Category:** Community\
**Tags:** releases\
**Created:** [October 5, 2018, 7:00pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942 "2018-10-05T19:00:06Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 9, 2018, 12:26pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/21 "2018-10-09T12:26:14Z")

</div>

I think that has nothing to do with scope, but with the way that `for` works in Julia, just iterating over a well-defined collection (which is of finite length). At each step you assign `i` to the next element in the collection. This is way off topic, but if you want to do something like the c code you could perhaps do something similar to this?

```julia
function main(n::Int)
  r = 0
  i = 1
  while (i += 1) < n
    i -= 1
    r += 1
  end
  r
end

```

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 9, 2018, 12:27pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/22 "2018-10-09T12:27:33Z")

</div>

Oops, @kristoffer.carlsson beat me to it! He seems to be everywhere all the time. Thanks for all your help in discourse Kristoffer!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2018, 12:31pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/23 "2018-10-09T12:31:56Z")

</div>

We can of course create a macro that simulates the C for loop style

```julia
macro cfor(loop_preamble, body)
    init, cond, post = loop_preamble.args
    return quote
        let
            $init
            while $cond
                $body
                $post
            end
        end
    end
end

```

```julia
julia> @cfor (i = 1, i<5, i+=1) begin
           println(i)
       end
1
2
3
4

```

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [October 9, 2018, 12:54pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/24 "2018-10-09T12:54:06Z")

</div>

It is not the same.  
Using “for i in 1:n” in Julia, you update i using an iterator, it does not use the previous value, so if you change it inside the block, it does not matter. Using a while the result should be the same than in C.  
It has not relationship with the scope of variable i.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [October 9, 2018, 2:17pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/25 "2018-10-09T14:17:02Z")

</div>

Yes, exactly, good point with the iterator. I tried using all sorts of `global` and `local` declarations to access the `i` but yes, inside the loop I was always dealing with a different variable and I didn’t know how Julia implements that.

@kristoffer.carlsson @pablosanjose thanks for the rewrite, very helpful. In terms of time and space complexity the Julia algorithm accurately maps the C implementation. Nice stuff.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 9, 2018, 4:37pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/26 "2018-10-09T16:37:55Z")

</div>

Split this topic on October 9, 2018, 4:37pm UTC

2 posts were split to a new topic: [Allocations in function timing](https://discourse.julialang.org/t/allocations-in-function-timing/16081)

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 9, 2018, 5:36pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/28 "2018-10-09T17:36:45Z")

</div>

> This has nothing to do with scoping but how a C-loop is defined.

Pretty interesting, this means that for loops in Julia are primitive recursive, no?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2018, 5:45pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/29 "2018-10-09T17:45:16Z")

</div>

Could you elaborate what is interesting?

Remember that `for i in iter; #body; end` is just syntax for

```julia
next = iterate(iter)
while next !== nothing
    (i, state) = next
    # body
    next = iterate(iter, state)
end

```

and that it is trivial to write an iterator that works just like the C-loop.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 9, 2018, 5:51pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/30 "2018-10-09T17:51:25Z")

</div>

Its just interesting from a theoretical point of view since it means for loops are really less powerful in Julia than in C. Note that this is a good thing. If one can avoid while loops and recursion it should be possible showing that a given function halts.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2018, 6:00pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/31 "2018-10-09T18:00:04Z")

</div>

> [@tobias.knopp](#):
>
> If one can avoid while loops and recursion it should be possible showing that a given function halts.

A for loop is lowered to a while loop and as I said, any C for loop can be written as a julia for loop with a suitable iterator.

I think a concrete example would solidify your point.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 9, 2018, 6:20pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/32 "2018-10-09T18:20:16Z")

</div>

The point is that for loops in Julia are not as powerful as while loops (I am talking here from the computability point of view). One can convert a for loop into a while loop but not vice versa. In C they are equally powerful.

I brought this up since @essenciary said

> [@essenciary](#):
>
> Got me thinking a bit that I couldn’t represent the algorithm

and if for loops are primitive recursive in Julia then this can be formally proven.

([Primitive recursive function - Wikipedia](https://en.wikipedia.org/wiki/Primitive_recursive_function) for some background)

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 9, 2018, 6:58pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/33 "2018-10-09T18:58:02Z")

</div>

You cannot mean the following?

```julia
for _ in Iterators.repeated(true)
    cond && break
    body  
end

```

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 9, 2018, 7:16pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/34 "2018-10-09T19:16:19Z")

</div>

This answers my question

```julia
Pretty interesting, this means that for loops in Julia are primitive recursive, no?

```

with no, they are μ-recursive ([General recursive function - Wikipedia](https://en.wikipedia.org/wiki/%CE%9C-recursive_function)). The power comes from the fact that iterators don’t need to iterate over a finite range. Thanks!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2018, 7:17pm UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/35 "2018-10-09T19:17:41Z")

</div>

> [@tobias.knopp](#):
>
> In C they are equally powerful.

They are equally “powerful” in Julia as well.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 10, 2018, 6:30am UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/36 "2018-10-10T06:30:20Z")

</div>

> [@tobias.knopp](#):
>
> The point is that for loops in Julia are not as powerful as while loops (I am talking here from the computability point of view). One can convert a for loop into a while loop but not vice versa. In C they are equally powerful.

“Power” is a misleading concept here. Both languages have general constructs for control flow, Julia’s `for` just does different things (being closely related to the iteration interface, cf [similar C++ solutions](https://www.boost.org/doc/libs/1_68_0/doc/html/foreach.html)). Saying that the C/C++ `for` is more powerful is not unlike arguing that `Base.*` is more “powerful” than C’s `*` because in Julia it also works for strings.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 10, 2018, 7:59am UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/37 "2018-10-10T07:59:58Z")

</div>

> [@Tamas\_Papp](#):
>
> “Power” is a misleading concept here.

The “power” is standard terminology for describing the computability aspects of any programming language (or abstract machines like the Turing machine). Most programming languages that we use are all equally powerful and in particular as powerful as a Turing machine. One essential thing is whether a programming language provides a LOOP-like ([LOOP (programming language) - Wikipedia](https://en.wikipedia.org/wiki/LOOP_(programming_language))) iteration protocol or a WHILE iteration.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 10, 2018, 8:12am UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/38 "2018-10-10T08:12:02Z")

</div>

My point was that it makes little sense to talk about the “power” of a particular language construct in isolation.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 10, 2018, 10:57am UTC](https://discourse.julialang.org/t/stating-my-opininon/15942/39 "2018-10-10T10:57:56Z")

</div>

Languages like LOOP and WHILE have been designed exactly for the purpose of investigating what power a language construct gives to a language. Maybe we just look from different angles on this subject. The term “power” should really not imply anything negative. From a language point of view it would actually be better if things like `Iterators.repeated(true)` could be prevented. Reasoning about a program where `for` is only primitive recursive is much easier (and actually computable) instead of a for loop that is μ-recursive.

[Previous page](https://discourse.julialang.org/t/stating-my-opininon/15942.md?page=1)
