-3

I tried to find the biggest non-floating number in existance. However, the following Haskell Code did not work in GHCi:

head (reverse [1..])

My pc is almost freezing when trying to compute the result. I tried to prove with mathematical induction that by taking the last element of a list of increasing numbers, i get the biggest number of them all.

head (reverse [1]) returns 1, the biggest number of this list head (reverse [1..100]) returns 100, which is also the biggest number.

I'm really confused here. What am i missing? Why is there an seemingly infinite loop? (I waited 5 minutes for a result) Are there any other methods i could try?

Alexander Mills
  • 277
  • 3
  • 18
  • 4
    What do you mean by *biggest non-floating number*? What do you expect, exactly? `[1..]` is an infinite list, and you can't reverse an infinite list... Are you not simply looking for `maxBound :: Int`? – jub0bs Mar 05 '15 at 13:02
  • Nope, im looking for the biggest number in existance. My idea was to use the lazy evaluation of haskell to achive this. Since the list won't be computed completly, i can avoid the problem of the infinite list :-) – Alexander Mills Mar 05 '15 at 13:07
  • 7
    There is no "biggest number in existence". For every number x there is a larger number x+1. – interjay Mar 05 '15 at 13:08
  • Well thats a bummer. And people always talk about Infinities Edge... i thought finding it would be a cool thing – Alexander Mills Mar 05 '15 at 13:13
  • 1
    Prove by contradiction that such a number does not exist and stop looking for it. It takes one application of Peano's induction axiom. – Sassa NF Mar 05 '15 at 14:15
  • I'll admit the question was asked as a joke. Of course such a number does not exist... – Alexander Mills Mar 05 '15 at 14:33
  • 1
    I'm voting to close this question as off-topic because it is not about programming. – dfeuer Mar 06 '15 at 07:27

1 Answers1

4

In theory, Haskell Integers are entirely unbounded, so there is no official way to get a largest one. In practice, GHC uses (usually) the GNU GMP library which allows not quite unbounded, but still enormous numbers, each single number taking up a significant part of your computer memory.

In practice, head (reverse [1..]) has no particular optimizations to avoid gradually constructing the entire list of numbers in memory, which means your computer will start thrashing and run out of memory rather soon.

You might try last [1..] instead, which should avoid keeping the entire list in memory because it can be garbage collected incrementally. But even with this, you could run the evaluation for a longer time than the universe has existed, and still never reach the end.

As suggested in the comments, for many types other than Integer there is an explicit upper bound, which you can often find with the overloaded maxBound, e.g. with a 64-bit GHCi:

Prelude> maxBound :: Int
9223372036854775807

(Even then, using head (reverse [1::Int ..]) is not a good idea, I should know because I just had to reboot my machine when it locked it up.)

Ørjan Johansen
  • 18,119
  • 3
  • 43
  • 53
  • Okay, i didn't think that last would be better than "head reverse". Now all i have to do is eliminate the time problem... How much longer than the universe has existed would i need to run the function? If i split the job into infinite tasks for infinite CPUs, would the code return an answer in O(1)? – Alexander Mills Mar 05 '15 at 13:20
  • 3
    @AlexanderMills If your computer were truly infinite, then it, too, would have no largest number. Otherwise, it will have to give up *somehow*, by running out either of time or memory. – Ørjan Johansen Mar 05 '15 at 13:23
  • Let's assume i upgrade it to have an infinite amount of Memory and a infinite power generator. I would not avoid any expense in pursuit of the Edge of Infinity. – Alexander Mills Mar 05 '15 at 13:27
  • 3
    I think this is a good answer. Just add that this is a list of `Integer` in the absence of other restrictions, and that in other circumstances it may be finite: `last [1..] :: Int16 == 32767` – Sassa NF Mar 05 '15 at 16:18
  • 3
    Something to add is that, to find the Largest `Int`, one can simply write `maxBound :: Int`. – AJF Mar 05 '15 at 16:47