How to take the derivative of this strange alternative to a devil's slippery staircase? I am trying to construct a function $f(x)$ which


*

*has derivative = $0$ almost everywhere, and

*is strictly increasing. 


I realize one can do this by playing with the Cantor function, but I would like to do it directly. However, I am having difficulty seeing if the beautiful monster has a derivative at all! 
Let $(a_k)$ be a sequence of numbers, such that $\sum |a_k| < \infty$, and $a_k > 0$. Define an ordering on $q_k \in \mathbb{Q}$, which one can do because $\mathbb{Q}$ is countable and totally ordered. 
Define $f: \mathbb{R} \to \mathbb{R}$, $$f(x) = \sum\limits_{\{k \text{ : } q_k < x\}} a_k$$
We see that $f_k$ is strictly increasing (thus is both increasing and non-constant on any open interval), for $\mathbb{Q}$ is dense in $\mathbb{R}$, and any open interval in $\mathbb{R}$ contains an element of $\mathbb{Q}$.
I would think this function would have derivative zero on all but $\mathbb{Q}$, which are totally disconnected and countable, and a disjoint union of countably many points has Lesbegue measure 0. 
The issue is that I don't know how to rigorously show that $|f'| = 0$ a.e., indeed, I don't know how to check that $f$ is differentiable. I don't know how to see that this $$\lim_{x \to x_0} \frac{f(x) - f(x_0)}{ x - x_0}$$
has limit $0$ on $S \subset \mathbb{R}$ such that the Lesbegue measure of $\{ \mathbb{R} - S \}$ is $0$.
Here is my question: How do I take the derivative of this function? Does this function satisfy the desired criterion?
 A: Your construction might work for some choices of the enumeration $(q_k)$ of $ Q$ and some choices of $(a_k)$ but if $a_k=1/k^2$ and if $q_{2k}\in (\pi, \pi+2^{-k})$ for each $k$, then $f(\pi+2^{-k})-f(\pi)\geq \sum_{j\geq k}1/(4j^2)= 1/(4k) + o(1/k)$ as $k\to \infty$, so $f'(\pi)$ does not exist. 
A: Revised:
As noted below in the original version, the fact that $f'$ exists almost everywhere is immediate from standard results, for example in Folland. Those results rely on other results - putting it all together into a proof requires a substantial fraction of the results in that chapter. Here's an entirely self-contained ad hoc proof.
If $I$ is an interval we let $3I$ denote the interval with the same center but three times the length: If $I=(a-r,a+r)$ then $3I=(a-3r,a+3r)$.
Lemma 1 Suppose that $K\subset\Bbb R$ is compact and $C$ is a collection of open intervals covering $K$. Then there exist finitely many pairwise disjoint $I_1,\dots,I_n\in C$ such that $3I_1,\dots,3I_n$ cover $K$.
Proof: We can suppose $C$ is finite. Let $I_1$ be an element of $C$ of maximal length. "Discard" any element of $C$ that intersects $I_1$. Note that if $I$ was discarded just now then $I\subset 3I_1$, since $I$ intersects $I_1$ and $I$ is no longer than $I_1$. 
Now let $I_2$ be one of the "remaining" intervals in $C$ of maximal length. Discard any remaining interval that intersects $I_2$. Note that any interval discarded at this stage is contained in $3I_2$. Also note that $I_1$ and $I_2$ are disjoint, since $I_2$ was not discarded at the first stage.
Etc. QED.
Now for $f:[0,1]\to\Bbb R$ define $$Mf(x)=\sup_{y\ne x}\left|\frac{f(x)-f(y)}{x-y}\right|$$and $$\omega f(x)=\limsup_{y\to x}\left|\frac{f(x)-f(y)}{x-y}\right|.$$Note that $$0\le\omega f(x)\le Mf(x)$$and that $f'(x)=0$ if and only if $\omega f(x)=0$.
Lemma 2 If $f:[0,1]\to\Bbb R$ is nondecreasing then $$m\left(\{x\in[0,1]\,:\,Mf(x)>\lambda\}\right)\le \frac c\lambda(f(1)-f(0)).$$
Proof: Since $m$ is inner regular it is enough to show that $m(K)$ satisfies the same inequality, where $K$ is a compact set with $Mf(x)>\lambda$ for every $x\in K$. 
For each $x\in K$ there exists $y\ne x$ with $|(f(x)-f(y))/(x-y)|>\lambda$. Let $J_x=[x,y]$ or $J_x=[y,x]$, whichever makes sense. Let $I_x$ be an open interval containing $J_x$, with $|I_x|\le 2|J_x|$.
Now the $I_x$ for $x\in K$ form an open cover of $K$. Hence, writing $I_j$ in place of $I_{x_j}$, there exist finitely many disjoint $I_1,\dots I_n$ such that $3I_1,\dots,3I_n$ cover $K$.
Aargh, we need more notation because we didn't know whether $x<y$ or $y<x$ at the start of this. Each $I_j$ is an open interval containing $J_j$, where $|I_j|\le 2|J_j|$. Write $J_j=[a_j,b_j]$. Now
$$m(K)\le\sum m(3I_j)\le 6\sum m(J_j)=6\sum_{j=1}^n(b_j-a_j).$$But $(f(b_j)-f(a_j)/(b_j-a_j)>\lambda$, so that $b_j-a_j\le(f(b_j)-f(a_j))/\lambda$. So we have $$m(K)\le\frac6\lambda\sum_{j=1}^n(f(b_j)-f(a_j)).$$But since $f$ is nondecreasing and the $[a_j,b_j]$ are disjoint, $$\sum(f(b_j)-f(a_j))\le f(1)-f(0).$$QED.
Note of course both lemmas are analogous to results in that chapter in Folland, adapted to the present context.
And now we can show that your $f$ has $f'=0$ almost everywhere. Write $f=\sum f_n$ in the obvious way. Write $$f=s_N+r_N,$$where $$s_N=\sum_{n=1}^Nf_n.$$
For every $N$ we certainly have $s_N'=0$ almost everywhere, so $\omega s_N=0$ almost everywhere. Hence $$\omega f\le\omega s_N+\omega r_N=\omega r_N\le Mr_N$$almost everywhere. Let $\epsilon>0$. Choose $N$ so that $$\frac c\epsilon(r_N(1)-r_N(0))<\epsilon.$$Then the set where $Mr_N>\epsilon$ has measure less than $\epsilon$. So the previous inequality shows that the set where $\omega f>\epsilon$ has measure less than $\epsilon$. So $\omega f=0$ almost everywhere. QED.

Original:
Yes, $f'=0$ almost everywhere. (Assuming $a_k>0$ and $\sum a_k<\infty$.)
This is not quite trivial. I don't know how much real analysis you know; the fact that $f'=0$ almost everywhere is immediate from basic/standard results in reals.
See for example Proposition 3.30 in Folland Real Analysis, as well as related results in that section. Your function $f$ is non-decreasing, hence it has bounded variation. There are fussy details regarding whether or not $f$ lies in what Folland calls NBV, but Theorem 3.23 shows that this doesn't matter; $f$ nondecreasing implies that $f=g$ almost everywhere, where $g$ is in NBV, and also $f'=g'$ almost everywhere. Now in the notation of Proposition 3.30, your $\mu_f$ is a singular measure, being $\sum a_k\delta_{q_k}$ (where $\delta_q$ is a point mass at $q$). So 3.30 says $f'=0$ almost everywhere.
It's not clear to me how difficult a more elementary proof would be (for example a proof accessible to someone taking the course that's called "Advanced Caclulus" here...)
