#!/usr/bin/perl -w use strict; use mapcar; sub neighbors { my @coords= @_; my @digits= ( 0, -1, 1 ); my @offset= (0) x @coords; return sub { my $i= 0; while( 2 < ++$offset[$i] ) { $offset[$i]= 0; return if $#offset < ++$i; } return mapcar { $_[0] + $digits[$_[1]] } \@coords, \@offset; }; } my $next= neighbors( 6, 2, -4 ); my @coords; while( @coords= $next->() ) { print "( @coords )\n"; }