TITLE: "Length" of rationals in an interval
QUESTION [8 upvotes]: For $x \in \mathbb{R}$, define $r(x)$ as follows:
$$
r(x)=
\begin{cases}
1 &\text{if $x$ is rational},\\
0 &\text{if $x$ is irrational}.
\end{cases}
$$

Q. What is $\int_0^1 r(x) dx$ ?

I know the rationals are dense in an interval, but countable, and so "sparse."
My motivation is a desire to average over the rationals, and in some sense
this integral would be the denominator.
If the integral is zero, then I'll have to think of another route. Thanks!

REPLY [6 votes]: Others have focused on the specific value of the integral itself. However, it is clear from your description that you wish to evaluate something along the lines of
$$
\frac{\int_0^1 f(x)r(x)dx}{\int_0^1 r(x)dx}
$$
where $r(x)$ is the rational indicator function.
While the component integrals cannot be evaluated, the overall concept is still meaningful. It is likely best to think of this in terms of a Riemann sum restricted to subsets of the rationals. In effect, you seek
$$
\lim_{n\to\infty} \frac{1}{n!}\sum_{i=1}^{n!} f\left(\frac{i}{n!}\right)
$$
where we restrict $n$ to integer values. Note that I have chosen $n!$ to ensure that subsequent sums will always include every point from the previous term. Also note that this is using the right-side value for each point, which means that f(0) does not affect the sum at any point.
It should be noted that this expression may not be well defined for some functions, and for others it may not be uniquely defined. A more rigorous definition would involve $\limsup$ and $\liminf$, and a requirement that these be equal for the "average" to exist.