[Haskell-cafe] Re: Wikipedia on first-class object
jules at jellybean.co.uk
Fri Dec 28 06:23:59 EST 2007
Peter Gammie wrote:
> On 28/12/2007, at 5:50 PM, Miguel Mitrofanov wrote:
>>> Right, so when I say to GHCi:
>>> Prelude> take 5 [1..]
>>> and it says:
>>> then it really has computed the entirety of the infinite sequence
>>> [1..], and not some approximation?
>> Of course not! In fact, it doesn't even compute the value "1". It just
>> juggles with the bytes somehow, so that it seems that it computes the
>> whole sequence and then applies "take 5". But does it really matter if
>> it computes 5 integers, or 10, or the whole sequence, at least if we
>> are not concerned about performance?
> I'll bite. So what do you expect:
> take 5 [1,2,3,4,5,undefined]
> to do? How about:
> take 5 (1:2:3:4:undefined)
> So it does seem to matter how much of the list it evaluates...
take evaluates as much as it needs of the spine but not any of the elements.
Even then, take is a good producer, so
take 1 . take 5 $ 1:2:3:4:undefined
But I think you know all this and your questions are rhetorical :)
More information about the Haskell-Cafe