
Hi, When I run this code in ghci: reverse [1..] I get: <interactive>: out of memory (requested 2097152 bytes) Can anyone explain this behaviour? Emanuel

Hi,
When I run this code in ghci:
reverse [1..]
I get:
<interactive>: out of memory (requested 2097152 bytes)
Can anyone explain this behaviour?
It is an infinite list you are trying to reverse. This cannot terminate. Which is the first element you would expect to see in the result? Those two will finish: reverse $ take 100 [1..] reverse [1..100] Emmanuel

Hello,
Here you try to reverse infinite list. What do you expect to get?
2012/12/18 Emanuel Koczwara
Hi,
When I run this code in ghci:
reverse [1..]
I get:
<interactive>: out of memory (requested 2097152 bytes)
Can anyone explain this behaviour?
Emanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

If you don't care about the order, you may use:
allNumbers = 0 : merge [1,2..] [-1,-2..]
where merge (x:xs) ys = x : merge ys xs
Cheers,
On Tue, Dec 18, 2012 at 12:35 PM, Emanuel Koczwara
Hi,
Dnia 2012-12-18, wto o godzinie 20:30 +0600, Alexander _ pisze:
Hello,
Here you try to reverse infinite list. What do you expect to get?
I was trying to build infinite list from -inf to +inf.
Emanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Felipe.

On 18 Dec 2012, at 14:35, Emanuel Koczwara
Hi,
Dnia 2012-12-18, wto o godzinie 20:30 +0600, Alexander _ pisze:
Hello,
Here you try to reverse infinite list. What do you expect to get?
I was trying to build infinite list from -inf to +inf.
What would you expect the number after -inf to be? Bob

Emanuel: I was trying to build infinite list from -inf to +inf.
Bob: What would you expect the number after -inf to be?
Wait a minute, let's just talk about the head of the list. What type is the
list element? Int? Integer? If Int, there's minBound :: Bounded Int => Int.
If Integer, no such instance exists!
So perhaps you want [minBound .. maxBound] :: [Int]?
-- Kim-Ee
On Tue, Dec 18, 2012 at 10:16 PM, Tom Davie
On 18 Dec 2012, at 14:35, Emanuel Koczwara
wrote: Hi,
Dnia 2012-12-18, wto o godzinie 20:30 +0600, Alexander _ pisze:
Hello,
Here you try to reverse infinite list. What do you expect to get?
I was trying to build infinite list from -inf to +inf.
What would you expect the number after -inf to be?
Bob
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hello Emanuel,
Here's the excerpt of the "reverse" function in GHC-List (
-- | 'reverse' @xs@ returns the elements of @xs@ in reverse order.--
@xs@ must be finite.reverse :: [a] -> [a]#ifdef
USE_REPORT_PRELUDEreverse = foldl (flip (:))
[]#elsereverse l = rev l [] where rev [] a = a rev (x:xs)
a = rev xs (x:a)#endif
Reversing an infinite list, Haskell will try to reach the end of the
list until it runs out of memory.
I don't thing it's doable lazily.
On Tue, Dec 18, 2012 at 9:24 AM, Emanuel Koczwara wrote: Hi, When I run this code in ghci: reverse [1..] I get: <interactive>: out of memory (requested 2097152 bytes) Can anyone explain this behaviour? Emanuel _______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
participants (7)
-
Alexander _
-
Emanuel Koczwara
-
Emmanuel Touzery
-
Felipe Almeida Lessa
-
Jonathan Rioux
-
Kim-Ee Yeoh
-
Tom Davie