reverse [1..]

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

reverse [1..]

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




Reply | Threaded
Open this post in threaded view
|

reverse [1..]

Emmanuel Touzery

> 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


Reply | Threaded
Open this post in threaded view
|

reverse [1..]

Alexander _
In reply to this post by Emanuel Koczwara
Hello,

Here you try to reverse infinite list. What do you expect to get?


2012/12/18 Emanuel Koczwara <poczta at emanuelkoczwara.pl>

> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121218/4c005bee/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

reverse [1..]

jonathan.rx
In reply to this post by Emanuel Koczwara
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 <poczta at emanuelkoczwara.pl
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121218/bc4664f7/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

reverse [1..]

Emanuel Koczwara
In reply to this post by Alexander _
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





Reply | Threaded
Open this post in threaded view
|

reverse [1..]

Felipe Lessa
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
<poczta at emanuelkoczwara.pl> 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.
>
> Emanuel
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



--
Felipe.


Reply | Threaded
Open this post in threaded view
|

reverse [1..]

Thomas Davie
In reply to this post by Emanuel Koczwara

On 18 Dec 2012, at 14:35, Emanuel Koczwara <poczta at emanuelkoczwara.pl> 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


Reply | Threaded
Open this post in threaded view
|

reverse [1..]

Kim-Ee Yeoh
Administrator
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 <tom.davie at gmail.com> wrote:

>
> On 18 Dec 2012, at 14:35, Emanuel Koczwara <poczta at emanuelkoczwara.pl>
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121218/81027e45/attachment.htm>