TITLE: If $X$ is a compact set, when does $f'(0)$ exist?
QUESTION [6 upvotes]: Let $X$ be a compact set in $\mathbb R$ and for $t\geq 0$ define $f(t)$ as $$f(t) = m(\{x\in \mathbb R| \exists y \in X : |x-y| \leq t\})$$ Under what conditions for $X$ does $f'(0)$ exist? ($m$ is the Lebesgue measure)
If $X$ is finite, then $f'(0)$ exist. Also, if $X$ contains an interval, f'(0) does not exist. I also showed that if $X=\{\frac{1}{2^n}, n\in \mathbb N\} \cup \{0\}$, then $f'(0)$ does not exist. If I am correct, this seems very strange to me, because $X$ has measure $0$ and I would think that for the derivative at zero to exist, the nominator $f(t)-f(0) = f(t)-m(X)$ has to go to $0$ faster than the denominator $t$ and the fastest way for this to happen would be if $X$ had measure $0$, so that $f$ doesn't "measure" a lot of points. 
So, now I am at a loss and can't find what other properties $X$ must have. Thanks for any help and I hope I am clear enough in my explanation!

UPDATE: With the help of the comments from Niels Diepeveen, I managed to do some work on this, but I am still stuck on a point. My work: 
Let $\cal{C}$ be the collection of the connected components of $X$. The claim is that $f'(0)$ exists if and only if $\cal{C}$ is finite.
$(\Leftarrow$) If $\cal{C}$ is finite, then $\cal{C}$ $= \{X_1, X_2,..., X_k\}$. Each $X_i$ is closed and they are pairwise disjoint, therefore there is a $t_0$ such that $$\forall x \text{ with }  d(x,X_i) \leq t_0 \Rightarrow d(x, X_j) > t_o, \forall j \neq i$$
Additionaly each $X_i$ is measurable as a closed and bounded set. Hence, for $t \leq t_o$, $f(t) = \sum_{i=1}^{k} m(X_i) + 2kt$ which means that $f'(0) = 2k$.
$(\Rightarrow)$ Suppose that $f'(0)$ exists but $\cal{C}$ is infinite. Let $M$ be given. Then there is a $k \in \mathbb{N}$ such that $2k>M$. Then, as before, we find a $t_0$ for $k$ of the sets in $\cal{C}$, let them be $\{X_1,...X_k\}$ such that $$\forall x \text{ with }  d(x,X_i) \leq t_0 \Rightarrow d(x, X_j) > t_o, \forall j \neq i, i,j \in \{0,...,k\}$$
Now the problem is, that if I take $t \leq t_o$ I would like to have $$\frac{f(t)-f(0)}{t-0} \geq \frac{f(t_0) - m(X)}{t_0} \geq \frac{m(X) + 2kt_0 -m(X)}{t_0} = 2k > M$$ and then the problem would be solved. But I don't see how I can prove that first inequality (the others are easy). It actually means that the function $\frac{f(t)}{t}$ is decrasing, which seems logical to me, but I can't prove it.

REPLY [2 votes]: For every compact set $X\in\mathbb R$ define $$d(X)=\liminf_{t\to 0^+}\, t^{-1}\left( m(\{x\in \mathbb R  \, |\, \exists y \in X : |x-y| \leq t\})-m(X)\right)$$
(this quantity is always defined, though may  be $+\infty$). Then

$d(X)\ge 2$ for every nonempty $X$, because you get two intervals of length $t$ next to $\min X$ and $\max X$.
$d(X\cup Y)\ge d(X)+d(Y)$ when $X$ and $Y$ are disjoint. Indeed, when $2t<\operatorname{dist}(X,Y)$, the $t$-neighborhoods are disjoint. So the  measures add up, and $\liminf $ of a sum is at least the sum of $\liminf$s.

Now suppose that $X$ has infinitely many connected components. Since it's not connected, there is $a\in \mathbb R\setminus X$ such that $\min X<a<\max X$. The sets $X_1=X\cap (-\infty,a]$ and $X_2=X\cap  [a,\infty)$ are nonempty and compact. By 2, $d(X)\ge d(X_1)+d(X_2)$. But at least one of $X_1$ and $X_2$ has infinitely many components, say $X_1$. So, $d(X_1)\ge d(X_{11})+d(X_{12})$, and this can continue indefinitely. Thus, $d(X)=\infty$.