Logo

Programming-Idioms

  • Pascal
  • Scheme
  • Perl
  • Ruby

Idiom #257 Traverse list backwards

Print each index i and value x from the list items, from the last down to the first.

items.each_with_index.reverse_each{|x, i| puts "#{i} #{x}" }
for i := items.count-1 downto 0 do writeln(i,' ',items[i]);
(define (reversed-inspect items)
  (foldl (lambda (x i)
          (display (format "~a ~a\n" i x))
          (sub1 i))
         (sub1 (length items))
        (reverse items)))
for ($i = $#items; $i >= 0; $i--) {
    print "$i $items[$i]\n";
}
for(int i = items.Count - 1; i >= 0; i--)
{
    Console.WriteLine($"Index = {i}, Item = {items[i]}");
}

New implementation...
< >
programming-idioms.org