But since a human can prove that the sequence terminates at 4, might there be a way to get the interpreter to do it?
In this simple case, yes. But there cannot exist a general algorithm to determine if an expression is true or false for all natural numbers >n
for some n
, because Haskell is Turing-complete, so it's impossible to prove that an expression even represents a terminating program for all natural numbers.
Even if your expression were limited to basic integer arithmetic, its truth would still be undecidable in the general case.
Could this be rewritten in such a way that it does terminate?
As Mog wrote in the comment, it's takeWhile (< 5) [1..]
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…