Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. List operations ([email protected])
2. Re: List operations (Felipe Almeida Lessa)
3. Re: List operations ([email protected])
4. Re: List operations (KC)
5. Re: List operations (Daniel Fischer)
6. Re: List operations (Ertugrul Soeylemez)
7. Re: List operations (KC)
8. Re: List operations (Alex Rozenshteyn)
----------------------------------------------------------------------
Message: 1
Date: Thu, 19 May 2011 14:05:29 +0000
From: [email protected]
Subject: [Haskell-beginners] List operations
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed";
DelSp="yes"
Hi,
I have a question about the cost of list comprehension and map function.
For example, I have a list of Double and I want to do lots of calculation
on it. There are two ways to do that:
1. [x ** 3 | x <- list]
2. map (flip (**) 3) list
I am wondering if the cost of using them could be very different and why.
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110519/7274e6c1/attachment-0001.htm>
------------------------------
Message: 2
Date: Thu, 19 May 2011 11:16:42 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] List operations
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, May 19, 2011 at 11:05 AM, <[email protected]> wrote:
> I have a question about the cost of list comprehension and map function.
>
> For example, I have a list of Double and I want to do lots of calculation on
> it. There are two ways to do that:
> 1. [x ** 3 | x <- list]
> 2. map (flip (**) 3) list
> I am wondering if the cost of using them could be very different and why.
List comprehensions are just syntatic sugar. You should expect both
(1) and (2) to be exactly the same when compiling.
Cheers, =)
--
Felipe.
------------------------------
Message: 3
Date: Thu, 19 May 2011 14:30:24 +0000
From: [email protected]
Subject: Re: [Haskell-beginners] List operations
To: Felipe Almeida Lessa <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="gb2312"; Format="flowed";
DelSp="yes"
OK, I see. Thanks a lot.
Cheers, =)
?Felipe Almeida Lessa <[email protected]>???
> On Thu, May 19, 2011 at 11:05 AM, [email protected]> wrote:
> > I have a question about the cost of list comprehension and map function.
> >
> > For example, I have a list of Double and I want to do lots of
> calculation on
> > it. There are two ways to do that:
> > 1. [x ** 3 | x
> > 2. map (flip (**) 3) list
> > I am wondering if the cost of using them could be very different and
> why.
> List comprehensions are just syntatic sugar. You should expect both
> (1) and (2) to be exactly the same when compiling.
> Cheers, =)
> --
> Felipe.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110519/06fd5928/attachment-0001.htm>
------------------------------
Message: 4
Date: Thu, 19 May 2011 08:52:05 -0700
From: KC <[email protected]>
Subject: Re: [Haskell-beginners] List operations
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=GB2312
By the way, you don't need the "flip" function.
map (** 3) [0,1..10]
[ x ** 3 | x <- [0,1..10]]
The section (** 3) will figure out on which side to put the argument/operand.
See http://www.haskell.org/haskellwiki/Section_of_an_infix_operator
2011/5/19 <[email protected]>:
> OK, I see. Thanks a lot.
>
> Cheers, =)
>
> ?Felipe Almeida Lessa <[email protected]>???
>> On Thu, May 19, 2011 at 11:05 AM, [email protected]> wrote:
>>
>> > I have a question about the cost of list comprehension and map function.
>>
>> >
>>
>> > 1. [x ** 3 | x
>> > 2. map (flip (**) 3) list
>>
--
--
Regards,
KC
------------------------------
Message: 5
Date: Thu, 19 May 2011 17:57:05 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] List operations
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Thursday 19 May 2011 16:05:29, [email protected] wrote:
> Hi,
>
> I have a question about the cost of list comprehension and map function.
>
> For example, I have a list of Double and I want to do lots of
> calculation on it. There are two ways to do that:
> 1. [x ** 3 | x <- list]
> 2. map (flip (**) 3) list
> I am wondering if the cost of using them could be very different and
> why.
As Felipe said, they should compile to the same code.
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).
Of course, if the exponent may be a non-integer, you have to use (**), if
it is an integer but not a fixed one, use (**) if the integer may be
large?, (^) or (^^) if it is sure to be small (in absolute modulus).
[?] (**) doesn't care how large the exponent is, (^) resp (^^) do.
------------------------------
Message: 6
Date: Thu, 19 May 2011 18:15:31 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] List operations
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Daniel Fischer <[email protected]> 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/
------------------------------
Message: 7
Date: Thu, 19 May 2011 10:01:29 -0700
From: KC <[email protected]>
Subject: Re: [Haskell-beginners] List operations
To: Ertugrul Soeylemez <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
x ** y = 10 ** (y * logBase 10 x)
Much easier to do base 10 exponents.
*Euler025> 25.0 ** 3.5
78125.0
*Euler025> 10 ** (3.5 * logBase 10 25.0)
78125.00000000009
On Thu, May 19, 2011 at 9:15 AM, Ertugrul Soeylemez <[email protected]> wrote:
> Daniel Fischer <[email protected]> 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/
>
--
--
Regards,
KC
------------------------------
Message: 8
Date: Thu, 19 May 2011 13:01:38 -0400
From: Alex Rozenshteyn <[email protected]>
Subject: Re: [Haskell-beginners] List operations
To: Ertugrul Soeylemez <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> wrote:
> Daniel Fischer <[email protected]> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110519/90033702/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 38
*****************************************