Normalizable wavefunction that does not vanish at infinity I was recently reading Griffiths' Introduction to Quantum Mechanics, and I stuck upon a following sentence:

but $\Psi$ must go to zero as $x$ goes to $\pm\infty$ - otherwise the wave function would not be normalizable.

The author also added a footer:
"A good mathematician can supply you with pathological counterexamples, but they do not arise in physics (...)".
Can anybody give such a counterexample?
 A: Take a gaussian (or any function that decays sufficiently quickly), chop it up every unit, and turn all the pieces sideways. 

A: Some simple example that illustrates that the condition 
$$\lim_{|x|\to \infty} f(x) = 0 \quad (1)$$ 
is not necessary. If the condition were necessary $f\in L^2$ would imply that the limit in (1) holds. 
Take in dimension 1 the function
$$
f(x) = \sum_{n=2}^{\infty} \chi_{I_n}(x)
$$
where $\chi_{I_n}$ is the characteristic function of the interval $I_n = [n-\frac{1}{n^2}, n+\frac{1}{n^2}]$ then the integral evaluates to
$$
\int |f(x)|^2 dx = \sum_{n=2}^{\infty} |I_n| = \sum_{n=2}^{\infty} \frac{2}{n^2} < \infty\ .
$$
But the function does not converge to zero for $|x|\to \infty$.
Sorry: Forgot to center the intervals around n. Now corrected.
A: Let
$$ \psi(x)
= \begin{cases} 1 & \exists\, n \in \mathbb N: x \in [n, n+\frac 1 {n^2}]\\ 0 & \text{otherwise.} \end{cases}
= \sum_{n \in \mathbb N} \mathbf 1_{[n,n+\frac 1 {n^2}]}(x) , $$
where $\mathbf 1_A$ is the characteristic function of the set $A$.
Then
$$ \int_{-\infty}^\infty |\psi(x)|^2 dx = \sum_{n=1}^\infty \frac 1 {n^2} < \infty, $$
but $\psi(x)$ doesn't converge to zero as $x \to +\infty$.
Note that $\psi \in L^2(\mathbb R)$, but it is not twice (weakly) differentiable and can therefore not be the solution to Schrödinger's equation with $H = -\Delta + V$. However, that problem can easily be solved by replacing the rectangle function with a smooth pulse with compact support. Alternatively, use
$$ \psi(x) = x^2 \mathrm e^{-x^8 \sin^2 x} ,$$
as discussed in Example 2 of §2.1 in arXiv:quant-ph/9907069 -- this is even analytical.
A: Emilio Pisanty and Eckhard Giere have already given discontinuous, piecewise constant counterexamples in their answers. Here we provide for-the-fun-of-it a smooth infinitely-many-times-differentiable counterexample $f\in C^{\infty}(\mathbb{R})$ of a square integrable function $f:\mathbb{R} \to [0,1]$ that does not satisfy $\lim_{|x|\to \infty}f(x)=0$. Our counterexample is 
$$\tag{1} 
f(x)~:=~ e^{- g(x)} ~\in ~]0,1], \qquad g(x)~:=~x^4 \sin^2 x~\in ~[0,\infty[. $$
Intuitive idea: If we imagine $x$ as a time variable, then the function $f$ returns periodically to its maximum value
$$\tag{2} f(x) =1 \quad\Leftrightarrow\quad g(x) =0 \quad\Leftrightarrow\quad \frac{x}{\pi}\in \mathbb{Z} ,$$
but spends most if its time close to the $x$-axis in order to be square integrable.
Proof: We leave a detailed rigorous epsilon-delta mathematical proof to the reader, but a sketched heuristic proof goes like this. For each very large integer $|n|\gg 1$, define a shifted variable 
$$\tag{3} y~:=~x-\pi n.$$ 
For the fixed integer $n\in\mathbb{Z}$, always assume from now on that the $y$-variable belongs to the interval 
$$\tag{4} |y|~\leq~ \frac{\pi}{2}.$$ 
For $|y|\ll\frac{\pi}{2}$ very small, we may approximate $g(x) \approx (\pi n)^4y^2$, so that in the interval (4), we have 
$$\tag{5} g(x)~\lesssim~ \pi^4 |n| \quad \Leftrightarrow\quad |y| ~\lesssim~ |n|^{-\frac{3}{2}}.$$ 
Thus we may form a square integrable majorant function $h\geq f$ (outside a compact region on the $x$-axis) by defining 
$$\tag{6}  h(x)~:=~\left\{\begin{array}{lcl} 1 &{\rm for}& |y| ~\lesssim~ |n|^{-\frac{3}{2}}, \cr
e^{-\pi^4 |n|}&{\rm for}& |n|^{-\frac{3}{2}}~\lesssim~ |y| ~\leq~ \frac{\pi}{2}, \end{array} \right.  \qquad |n|\gg 1. $$
The function $h\in {\cal L}^2(\mathbb{R})$ is square integrable on the whole $x$-axis, since 
$$\tag{7} \sum_{n\neq 0} |n|^{-\frac{3}{2}} ~<~ \infty$$ 
and 
$$\tag{8} \pi \sum_{n\in\mathbb{Z}}e^{-2\pi^4 |n|}~<~\infty$$  
are convergent series. 
A: Apart from not being sufficient to prove convergence of the integral
$$\int |f(x)|^2\text dx<\infty,$$
having the vanishing limig $\lim_{x\rightarrow\infty}f(x)=0$ is only necessary for the convergence within a suitable class of "nice" functions.
Consider, for example, the function
$$
f(x)=\sum_{n=1}^\infty\chi_{\left[n,n+\frac1{n^2}\right]}(x)=\left\{\begin{array}&1\text{ if }  n\leq x\leq n+1/n^2 \text{ for some }n=1,2,3,\ldots,\\0\text{ otherwise.}\end{array}\right.
$$
(Here $\chi_A$ is the characteristic function of a set $A\subseteq\mathbb R$.) This function has a convergent $L^2$ integral but does not have a well-defined limit at infinity. While this function is not continuous, but using suitable bump functions you can make a similar $C^\infty$ function with the same properties. This is the kind of function you're allowing when you do not impose vanishing limits at infinity - i.e., pretty ugly.
More to the point, say your wavefunction obeys a stationary Schrödinger equation with energy $E$ for some potential $V$ such that $\lim_{x\rightarrow} V(x)>E$ (i.e. a bound state). Then you know that, at infinity, $f''(x)$ has the same sign as $f$, which we can assume to be positive. If $f'(x)$ is ever zero in that region, then you know that it will be positive for all $x$ after that and $f(x)$ will monotonically increase, in which case the $L^2$ integral has no chance of convergence. In this particular setting, then you can restrict yourself to monotonically decreasing functions, and those are nice enough that the vanishing limit at infinity is necessary for $L^2$ convergence.
(To be followed by a more rigorous argument if I find the time.)
