Home | About | Partners | Contact Us

SourceForge Logo

Quick Links
Home
News
Status
Building XL
XL Mailing List

Understanding XL
Conceptual overview
XL examples
Inside XL
Concept Programming

In depth
Browse GIT
Bugs
SourceForge Info
Contact

Other projects
GNU Project
The Mozart Project

XLR: Extensible Language and Runtime

The art of turning ideas into code

Computing an average

Prev: Maximum

Up

Next: Complex numbers

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++.

Prev: Maximum

Up

Next: Complex numbers


Copyright 2008 Christophe de Dinechin (Blog)
E-mail: XL Mailing List (polluted by spam, unfortunately)