Endowing an abelian group with a metric. I solved the following exercise, which is not hard:

Let $G$ be an additive abelian group, such that exists $f: G \to \mathbb{R}$ satisfying:

*

*$f(0) = 0$ and $f(x) > 0$ for all $x \neq 0$.


*$f(x) = f(-x)$.


*$f(x + y) \leq f(x) + f(y)$, for all $x,y \in G$.
Give an example of such a group and function, and prove that $d: G \times G \to \mathbb{R}$ given by $d(x,y) = f(x-y)$ is a metric on $G$.

Since I'm not a creative person, $\mathbb{Z}_2$ and $f: \mathbb{Z}_2 \to \mathbb{R}$ defined by $f(0) = 0 $ and $f(1) = 1 $ works as an example. And proving that $d$ is a metric is straightforward.
My question is: is it always possible to define such a function $f$ satisfying these conditions? Also, I noticed that the function in my example induced the discrete metric (maybe because it is a sort of identity?). If it is not possible, is there a nice way to find conditions on the group that makes it possible?

In the comments, showed up another question, more interesting: if we consider a topological group, can we find a continuous function in the above conditions?
 A: Without a topology given, you can always choose $f = 1 - \chi_{\{0\}}$ as mentioned in the comment by Travis, and get the discrete metric.
Note that neither this, nor any of the following, requires commutativity. By changing the notation, the construction works identically for non-abelian groups.
If we have a topological group, things are more interesting. A necessary condition for the existence of a continuous $f\colon G \to [0,\infty)$ with $f(0) = 0$ and $f(x) > 0$ for all $x \neq 0$ is that $\{0\}$ is a $G_\delta$-set in $G$, since zero-sets of continuous functions are $G_\delta$-sets.
If $\{0\}$ is a $G_\delta$-set, then such an $f$ exists. If $\{0\} = \bigcap\limits_{n=1}^\infty V_n$ with open sets $V_n$, then we can construct a sequence of symmetric ($U = -U$) open neighbourhoods $U_n$ of $0$ with $U_n \subset V_n$ and $U_{n+1} + U_{n+1} + U_{n+1} \subset U_n$ for all $n$. Now define
$$g(x) := \begin{cases}\qquad 1 &, x \notin U_1\\ \inf \{2^{-k} : x\in U_k \} &, x\in U_1\end{cases}$$
and
$$f(x) = \inf \left\{\sum_{i=1}^n g(x_i) : n\in \mathbb{Z}^+, \bigl(\forall 1 \leqslant i \leqslant n\bigr)\bigl(x_i\in G\bigr), \sum_{i=1}^n x_i = x \right\}.$$
By construction, $g\colon G \to [0,1]$ has $g(0) = 0$ and $g(-x) = g(x) > 0$ for all $x\in G\setminus\{0\}$, so $f\colon G\to [0,1]$ is symmetric and satisfies $f(0) = 0$. The triangle inequality $f(x+y) \leqslant f(x) + f(y)$ is easy to show. Further, we have
$$\frac{1}{2} g(x) \leqslant f(x) \leqslant g(x) \tag{$\ast$}$$
for all $x\in G$. The right inequality is immediate. For the left, we note that it is also clear for $x = 0$, and for $x\neq 0$ show that for any decomposition
$$x = \sum_{i=1}^n x_i$$
we have
$$\frac{1}{2}g(x) \leqslant \sum_{i=1}^n g(x_i)$$
by induction on the number $n$ of terms in the decomposition. The base case $n = 1$ is clear. So let $x = \sum\limits_{i=1}^{n+1} x_i$ and $s := \sum_{i=1}^{n+1} g(x_i)$. Since not all $x_i$ can be $0$, we have $s > 0$. If $s \geqslant \frac{1}{2}$, the left inequality in $(\ast)$ holds since $g(x) \leqslant 1$, so we need only consider $0 < s < \frac{1}{2}$. Let
$$m = \max \left\{ k : 1 \leqslant k \leqslant n+1,\, \sum_{i=1}^{k-1} g(x_i) \leqslant \frac{s}{2}\right\}.$$
Then $\sum\limits_{i=1}^m g(x_i) > \frac{s}{2}$, and hence $\sum\limits_{i=m+1}^{n+1} g(x_i) < \frac{s}{2}$. By the induction hypothesis we have
$$\frac{1}{2} g\left(\sum_{i=1}^{m-1} x_i\right) \leqslant \sum_{i=1}^{m-1} g(x_i) \leqslant \frac{s}{2},\quad \frac{1}{2} g\left(\sum_{i=m+1}^{n+1} x_i\right) \leqslant \sum_{i=m+1}^{n+1} g(x_i) < \frac{s}{2},$$
and $\frac{1}{2} g(x_m) \leqslant \frac{s}{2}$ by definition of $s$. Let $k\in \mathbb{Z}^+$ be minimal with $2^{-k} \leqslant s$ [since $s < \frac{1}{2}$, we have $k \geqslant 2$]. Then by definition of $g$, we have
$$\sum_{i=1}^{m-1} x_i,\, x_m,\, \sum_{i=m+1}^{n+1} x_i \in U_k$$
and thus
$$x = \sum_{i=1}^{n+1} x_i \in U_k + U_k + U_k \subset U_{k-1},$$
whence
$$\frac{1}{2}g(x) \leqslant \frac{1}{2} 2^{-(k-1)} = 2^{-k} \leqslant s.$$
From the right inequality in $(\ast)$, we obtain $f(x) = 0 \iff x = 0$, and $f^{-1}([0,2^{-k}]) \supset U_{k}$ shows that $f$ is continuous at $0$. From the triangle inequality we obtain
$$\lvert f(x) - f(y)\rvert \leqslant f(x-y),$$
which then shows the global continuity of $f$.
From the left inequality in $(\ast)$ we obtain $f^{-1}([0,2^{-k})) \subset U_{k}$, so the uniform structure induced by the metric $d(x,y) = f(x-y)$ coincides with the uniform structure generated by the sequence $(U_n)_{n\in\mathbb{Z}^+}$, and the topology induced by the metric is coarser than the given topology [in general, strictly coarser, the constructed metric induces the given topology if $\{ U_n : n \in \mathbb{Z}^+\}$ is a neighbourhood basis of $0$].
As a corollary, we note that a topological group is metrisable if and only if it is Hausdorff and first countable.
