[Haskell-beginners] List operations

Alex Rozenshteyn rpglover64 at gmail.com
Thu May 19 19:02:58 CEST 2011


Damn it... Forget my last email. I confused iterate with replicate for a
second...

On Thu, May 19, 2011 at 1:01 PM, Alex Rozenshteyn <rpglover64 at gmail.com>wrote:

> You're not applying the operation 1000000 times, you're traversing to the
> 1000000th node in the list and applying the operation once. Laziness never
> evaluates the other thunks.
>
>
> On Thu, May 19, 2011 at 12:15 PM, Ertugrul Soeylemez <es at ertes.de> wrote:
>
>> Daniel Fischer <daniel.is.fischer at googlemail.com> wrote:
>>
>> > However, since you're asking about cost, which indicates that you care
>> > for performance, the above would be better written as
>> >
>> > [x*x*x | x <- list]
>> >
>> > unless you depend on the small differences in the outcome [I'm not
>> > quite sure how many bits may be affected, not many, typically none or
>> > one].  Functions like (**), exp, log, sin, cos, ... are slow, very
>> > slow.  If the exponent is a small (positive) integer, specifically
>> > giving a sequence of multiplication steps is much faster, also using
>> > (^) instead of (**) is faster for small exponents (but slower than an
>> > explicit multiplication sequence).
>>
>> Neither does this really match my intuition, nor can I confirm it with
>> an experiment.  Applying (** 3) a million times to a Double takes a
>> second and gives me the expected Infinity.  Applying (^3) or (\x ->
>> x*x*x) a million times to the same value, well, I didn't want to wait
>> for it to finish.
>>
>> The experiment was ran with the following codes in GHCi:
>>
>>    iterate (** 3) 1.000000001 !! 1000000
>>    iterate (^3) 1.000000001 !! 1000000
>>    iterate (\x -> x*x*x) 1.000000001 !! 1000000
>>
>> Note that exponentiation is a cheap operation on Double.
>>
>>
>> Greets
>> Ertugrul
>>
>>
>> --
>> nightmare = unsafePerformIO (getWrongWife >>= sex)
>> http://ertes.de/
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
> --
>           Alex R
>
>


-- 
          Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110519/c4188039/attachment.htm>


More information about the Beginners mailing list