#!/usr/bin/perl

# manhattan_distance( @p )
#   Computes the Manhattan distance between
#   two d-dimensional points, given 2*d coordinates.  For example,
#   a pair of 3-D points should be provided as @p of
#   ( $x0, $y0, $z0, $x1, $y1, $z1 ).

sub manhattan_distance {
    my @p = @_;                 # The coordinates of the points.
    my $d = @p / 2;             # The number of dimensions.

    my $S = 0;                  # The sum of the squares.
    my @p0 = splice @p, 0, $d;  # Extract the starting point.

    for ( my $i = 0; $i < $d; $i++ ) {
        my $di = $p0[ $i ] - $p[ $i ];  # Difference...
        $S += abs $di;                  # ...absolute value summed.
    }

    return $S;
}

print manhattan_distance( 3, 4, 10, 12 ); 
