Like for maximum, computing an average is a good example
of concept where having variadics is a great help. We
can make use of the generic number type used in the
factorial example as well.
function Sum(X : number) return number is
return X
function Sum(X : number; other) return number is
Y : number := Sum(other)
return X + Y
function Count(X : number) return integer is
return 1
function Count(X : number; other) return integer is
return Count(other) + 1
function Average(X : number; other) return number is
return Sum(X, other) / Count(X, other)
The Average function itself is defined in such a way that it
cannot take zero argument (the average of zero arguments would not be
defined). Preventing incorrect use by construction is a good way to
reduce semantic noise.
An average function is an example of variadic function which cannot
easily be represented using currying, because you need to
be able to count the number of elements in the list. So you cannot
easily implement average using, for instance, an
operator<< in C++.
