|
|
|
|
Quick Links
Understanding XL
In depth
Other projects
|
XLR: Extensible Language and Runtime
|
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 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++.
|
Copyright 2008 Christophe de Dinechin (Blog)
E-mail: XL Mailing List (polluted by spam, unfortunately)