Let's start properly with one of (it seems) most talked about languages about at the moment, Ruby. Anyone here using Ruby in any kind of serious way? Me neither.
This first piece of code probably looks pretty sensible to most people. I get the impression that Rubyists frown on it rather though. The for/in is more idiomatic, but that turns out to be syntactic sugar around the each method.
An iterator in Ruby is a method that invokes a block of code on elements in the collection. In Ruby a block is like a closure, or an anonymous method, or a functor. It's a bit of code that you pass into a method, which that method then executes. For an iteration, it calls the block for each element. In fact, it's not unlike the Lisp we were just looking at.
This is described by Pragmatic Dave Thomas in Programming Ruby as "more natural". Perhaps, perhaps not. Lots of Ruby is built on this idea though. We've already said the for/in construct is syntactic sugar around each. "As long as your class defines a sensible each method, you can use a for loop to traverse it." The Ruby library includes a mixin class called
Enumerable that has a whole pile of iterator methods, built around each, find, detect, collect, max, min, partition.
For Ruby, as with Lisp, iteration is something a collection does to itself. It's internal, and not really for general consumption.