Show that $f$ is a Borel measurable function Let $f: \mathbb{R} \to \mathbb{R} $ be a function with
$$ x \mapsto \begin{cases}
x, \quad &\text{if} \quad x \in \mathbb{Q} \cap [0,1]  \\
1-x, \quad &\text{if} \quad x \in (\mathbb{R} \backslash \mathbb{Q}) \cap [0,1] \\
0, \quad &\text{otherwise.} 
\end{cases} $$
One can write $f = \mathbb{1}_{\mathbb{Q} \cap [0,1]}(x) \cdot x + \mathbb{1}_{(\mathbb{R} \backslash \mathbb{Q}) \cap [0,1]}(x) \cdot (1-x) $ and clearly this function is measurable since it is a sum of measurable functions (this way it is just fine). But, would it be also ok, if I would show that the preimages are in $\mathfrak{B}(\mathbb{R})$, i.e.
$$ 
f^{-1}(x) = \mathbb{Q} \cap [0,1] \in \mathfrak{B}(\mathbb{R}) 
$$
and in the same fashion also for $f^{-1}(x)$ and $f^{-1}(\{0\})$? Obviously those are again all contained in the Borel $\sigma$-Algebra, but I think that is not the correct way to do it, since one has to show, that
$$ \forall B \in \mathfrak{B}(\mathbb{R}): f^{-1}(B) \in \mathfrak{B}(\mathbb{R}). $$ In my opinion there could be several cases for $B \in \mathfrak{B}$ arbitary: $x \in B $, $1-x \in B$, $\{0 \} \in B$, $\emptyset = B$, $\mathbb{R} \in B$, such that for all this $B$ I have to show, that the preimages are in $\mathfrak{B}(\mathbb{R})$. Please let me know, if I am on the right path!
 A: $\newcommand{\d}{\mathfrak{B}(\mathbb R)}$
By definition, we should be checking that for every $B \in \d$, we have that $f^{-1}(B) \in \d$.
But some niceties make things easy for us, namely the following identities :

*

*For any $f$ and arbitrary collection of disjoint sets $U_i$ we have $f^{-1}(\cup_{i \in I} U_i) = \cup_{i \in I} f^{-1}(U_i)$.


*For any $f$ and set $U$ we have $f^{-1}(U^c) = [f^{-1}(U)]^c$.
Both are easily proved by definition.

A generating set for $\d$ is given by half intervals, which are the subsets of $\mathbb R$ given by $H = \{(-\infty,x] : x \in \mathbb R\}$. So elements of $H$, for example are $(-\infty,0]$, $(-\infty,3.54]$, $(-\infty,-\pi]$ and so on.
The point is, that $H$ generates $\d$ in the sense that the smallest sigma-algebra containing $H$ is $\d$. Furthermore, $H$ is what we call as a $\pi$-family : if I intersect any two members of $H$, the intersection is also in $H$. For example , the intersection of $(-\infty,1]$ and $(-\infty,2]$ is $(-\infty,1]$ which remains in $H$.
Let's take an element in $H$, say $(-\infty,x]$. Then, the set $f^{-1}((-\infty,x])$ is nothing but the set $\{y : f(y) \leq x\}$.


Lemma : Let $f$ be a function such that for every $x \in \mathbb R$, the set $\{y : f(y) \leq x\}$ is Borel. Then $f$ itself is Borel i.e. for any $B \in \d$, $f^{-1}(B)$ is Borel.

Proof : Consider the sigma-algebra $\Sigma = \{B' \in \d : f^{-1}(B') \in \d\}$. This is the set of Borel sets, which we know satisfy the conditions for now. Note that $H \subset \Sigma$.
But we also know something else about $\Sigma$ : it is closed under complements and under infinite disjoint union, thanks to the two properties we mentioned at the outset. Also it is clear that $\mathbb R \in \Sigma$.
Now, this makes $\Sigma$ what we call as a $\lambda$-system : not quite a sigma-algebra as yet, but close. Now, we invoke the Dynkin $\pi-\lambda$ theorem :

Let $S$ be a $\lambda$ system , containing a $\pi$-system $T$. Then $S$ contains $\sigma(T)$, the smallest sigma-algebra containing $T$.

That tells us, that $\Sigma$ contains $\d$, the smallest sigma-algebra containing $H$, but then $\Sigma$ is anyway contained in $\d$ as well, so we are done.

The result of all this, is that $f$ is Borel. Why? Let's prove it using the lemma. Pick $x\in \mathbb R$. Then we have some cases :

*

*If $x < 0$ then $f(y) < x$ for all $y$, so $\{y : f(y) \leq x\}$ is empty, hence Borel.


*If $x > 1$ then $f(y) > x$ for all $y$ so $\{y : f(y) \leq x\} = \mathbb R$ is Borel.


*If $x \in [0,1]$, then of course $\{y : f(y) \leq x\}$ contains $[0,1]^c$, which is Borel. Apart from this, we break into cases : if $y \in [0,1] \cap \mathbb Q$ with $f(y) \leq x$ then $y \leq x$ i.e. $y \in \mathbb Q \cap [0,x]$. Similarly, if $f(y) \leq x$ with $y \in [0,1] \cap \mathbb Q^c$ then $y \geq 1-x$. This makes up $[1-x,1] \cap \mathbb Q^c $. Thus :
$$
\{y : f(y) \leq x\} = [0,1]^c \cup \left(\mathbb Q \cap [0,x]\right) \cup \left([1-x,1] \cap \mathbb Q^c\right)
$$
a union of Borel sets, hence Borel.
Thus, we get that $f$ is Borel.

Now, a common misconception is that if for every $x$, if $\color{red}{\{y : f(y) = x\}}$ is Borel, then $f$ is Borel. This is false.
The reason? Well, the family of singleton sets $\{x\}$ don't generate $\d$, for starters. Then it doesn't even form a $\pi$-family. Finally, a Borel measurable set has more regularity, in the sense that sets that usually are indexed (or decided) by infinite sequences (like the real numbers, if treated with the decimal expansion as an infinite sequence of digits) aren't Borel because they behave far too irregularly. (More on that, but in little more detail, here).
Edit : The family $H$ may look like it too is parametrized by the real numbers, but using density of rationals, you can see that a function being Borel measurable must be a result of "countable" decision i.e. decide it at the rationals and you get it everywhere.
For a counterexample, we use the Cantor set, the most fundamental set when it comes to Lebesgue measurability, along with a standard set called the non-measurable set. The Cantor set itself is closed, hence Borel, but we can play with it nicely.
In this page, there is a good explanation on what's happening :essentially, the Cantor function is a bijection, so every set of the form $\{x : f(x) = y\}$ is a singleton hence Borel. But the problem is that the non-measurable set which sits inside $[0,1]$, that causes problems.
Unfortunately, I cannot find a simpler example, because looking for something non-Borel yet Lebesgue measurable is in itself a topic that is difficult to discuss.
