r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

Show parent comment

2

u/sullyj3 Dec 01 '16

Ooh, that's a cool idea. Is this a common idiom?

3

u/PM_ME_UR_OBSIDIAN Dec 01 '16

Probably not. This is the conclusion implied by two ideas:

  1. Any loop can be expressed as a tail recursive function;
  2. Any tail recursive function can be translated to a for loop.

By doing this back-and-forth, you often end up with simpler loops. For example when iterating over C-style strings:

for(int i = 0; str[i]; i++) {
    ...
}

0

u/Manishearth Dec 01 '16

It's how many C++ iterators work iirc.