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. Regarding Haskell GSOC projects (Gautam Sewani)
2. Re: Integer factorization (Ertugrul Soeylemez)
3. Re: Integer factorization (Heinrich Apfelmus)
4. Re: Regarding Haskell GSOC projects (Brent Yorgey)
5. Re: folds again -- myCycle (Will Ness)
6. Re: Re: folds again -- myCycle (Bas van Dijk)
7. Re: Re: folds again -- myCycle (Daniel Fischer)
8. Binary.Put and Double (Rick R)
----------------------------------------------------------------------
Message: 1
Date: Sat, 14 Mar 2009 18:19:55 +0530
From: Gautam Sewani <[email protected]>
Subject: [Haskell-Beginners] Regarding Haskell GSOC projects
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I am interested in working on Haskell during GSOC 2009. I wanted to
know which Haskell mailing list to use in order to inquire about the
various project ideas.
Regards
Gautam
------------------------------
Message: 2
Date: Sat, 14 Mar 2009 16:39:24 +0100
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: Integer factorization
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
"Sergey V. Mikhanov" <[email protected]> wrote:
> I am solving a problem of finding the largest prime factor of
> 600851475143 (Project Euler is great for learning new language!),
> [...]
You may find my little auxilliary library [1] for ProjectEuler.net
problems handy. For the specific problem of factoring an integer, it
uses the wheel factoring method, which uses the idea of Eratosthenes
sieving to filter out most non-primes with almost no performance loss
compared to the more naive trial division method.
[1] http://hpaste.org/fastcgi/hpaste.fcgi/view?id=2388
Greets,
Ertugrul.
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/
------------------------------
Message: 3
Date: Sun, 15 Mar 2009 11:24:03 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Integer factorization
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Francesco Bochicchio wrote:
> Heinrich Apfelmus wrote:
>>
>> Abstraction is the one driving force for very short names. For example,
>> take the definition of foldr
>>
>> foldr f z [] = z
>> foldr f z (x:xs) = f x (foldr f z xs)
>>
>> Since this function is polymorphic, so f , z and the xs can be
>> anything, using more "descriptive" variable names is simply not
>> possible; the key point of fold is its generality.
>
> Ok but one could still hint at their structure or purpose:
>
> foldr function value (x:xs) = function x ( foldr function value xs )
>
> I believe this would give a little more information to the casual reader.
Sure, though there is already the convention from mathematics that
functions are named f, g and values are denoted by x, y or z .
> I have some resistance to use nouns for functions. In the imperative world,
> nouns are for variables, verbs are for functions.
> I know that in pure functional programming there is not such a thing
> as variables, but still I would reserve nouns for function parameters and
> bound expressions. Hence if I have a function that
> find factors, I would call it findFactors rather than just factors.
>
> One such example of misnaming - from a beginner point of view - is the
> length function in prelude: if it was called
> count, I believe more beginners would have realized that works by actually
> counting the elements of
> a list and not by accessing to some already available 'property' of the
> list.
IMHO, I think that using nouns like length or factors is a good
idea, for they sounds nice when used in compound expressions. Compare
for instance
take (length xs `div` 2) xs
take (count xs `div` 2) xs
The first can be put into pseudo-english as
"take (the length of xs divided by 2) elements of xs"
while the verb in the second makes it difficult to use the expression in
parenthesis as object to another verb. In contrast, the runtime (O(n)
versus O(1) for say a record field) is rather secondary.
Implicitly adding the preposition "of" makes the nouns flow, i.e.
"factors of n", "length of xs".
>> Convention. Often, an auxiliary function that does basically the same
>> thing as the main function factors but with an extra parameter will be
>> named factors' . The apostrophe has the drawback that it's easy to
>> forget, so some people now name such auxiliary functions go instead.
>>
>>
> I tend to use _ instead of '. Is more visible and keep conveying the idea
> that the auxiliary function is just a slight variation of the main one.
The convention of using an apostrophe for variations comes from
mathematics and it seems that it was put into the Haskell syntax
specifically for this purpose.
In any case, the rationales and style that I presented are how I
interpret the naming conventions from classic papers like
John Hughes. Why functional programming matters.
http://www.cs.chalmers.se/~rjmh/Papers/whyfp.pdf
John Hughes. The Design of a Pretty-Printing Library
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.8777
Most of these conventions root in mathematics, of course. I like them
for they encourage thinking purely functionally, but these are of course
not the only possibilities.
Regards,
apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 4
Date: Sun, 15 Mar 2009 11:27:08 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-Beginners] Regarding Haskell GSOC projects
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sat, Mar 14, 2009 at 06:19:55PM +0530, Gautam Sewani wrote:
> Hi,
> I am interested in working on Haskell during GSOC 2009. I wanted to
> know which Haskell mailing list to use in order to inquire about the
> various project ideas.
> Regards
> Gautam
Hi Guatam, haskell-cafe [1] is probably the most appropriate list.
-Brent
[1] http://haskell.org/mailman/listinfo/haskell-cafe
------------------------------
Message: 5
Date: Sun, 15 Mar 2009 18:35:43 +0000 (UTC)
From: Will Ness <[email protected]>
Subject: [Haskell-beginners] Re: folds again -- myCycle
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Daniel Fischer <daniel.is.fischer <at> web.de> writes:
>
> Am Samstag, 14. März 2009 08:57 schrieb 7stud:
> > I spent a long time working on a solution for an exercise in RWH: if you
> > can, use foldr to mimic haskell's cycle function. At first, I wondered
>
> So variant 1 amounts to
> myCycle xs = let ys = xs ++ ys in ys
i.e.
myCycle xs = ys where ys = xs ++ ys
or
myCycle xs = xs ++ myCycle xs
- "right recursion" - good (kind of) even under strict semantics - will
actually construct an infinite list in memory (although useless under strict,
since it'll never stop).
> and variant 2 to
> myCycle2 xs = let ys = ys ++ xs in ys
i.e.
myCycle2 xs = ys where ys = ys ++ xs
or
myCycle2 xs = myCycle2 xs ++ xs
- "left recursion" - does nothing under both strict and non-strict semantics -
just goes into infinite loop trying to figure out what to do but never gets to
do anything really.
> We have, for all lists ks, ms:
> ks ++ ms = foldr (:) ms ks
which then by direct application yields
-- myCycle xs = xs ++ myCycle xs
myCycle xs = foldr (:) (myCycle xs) xs
>
> So we can write variant 1 as the snappy
>
> myCycle xs = let ys = foldr (:) ys xs in ys
which is then just rewritable as:
myCycle xs = ys where ys = foldr (:) ys xs
or (arriving at the same result)
myCycle xs = foldr (:) (myCycle xs) xs
I find that "where" rewrites are easier to comprehend for me, more often than
not. :)
Cheers,
------------------------------
Message: 6
Date: Sun, 15 Mar 2009 22:14:34 +0100
From: Bas van Dijk <[email protected]>
Subject: Re: [Haskell-beginners] Re: folds again -- myCycle
To: Will Ness <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On Sun, Mar 15, 2009 at 7:35 PM, Will Ness <[email protected]> wrote:
> which is then just rewritable as:
>
> myCycle xs = ys where ys = foldr (:) ys xs
>
> or (arriving at the same result)
>
> myCycle xs = foldr (:) (myCycle xs) xs
Note that, because 'ys' only has to be calculated once, GHC makes the
most efficient code for the former one. In the latter 'myCycle xs' has
to be calculated each time 'xs' runs empty.
Here are some efficiency tests:
(All programs are compiled with GHC with -O2. I excluded the first run
of each program because of possible initialisation overhead.)
------------------------------------------------------------
module Main where
import System.Environment (getArgs)
myCycle1 xs = ys where ys = foldr (:) ys xs
myCycle2 xs = foldr (:) (myCycle2 xs) xs
myCycle3 xs = ys where ys = xs ++ ys
main = do ns:ms:_ <- getArgs
print $ sum $ take (read ns) (myCycle1 [1..read ms])
------------------------------------------------------------
$ for i in 1 2 3 4 5 6; do time ./cycle1 30000000 1000; done;
15015000000
real 0m4.854s
user 0m4.801s
sys 0m0.017s
15015000000
real 0m3.921s
user 0m3.870s
sys 0m0.016s
15015000000
real 0m3.861s
user 0m3.806s
sys 0m0.023s
15015000000
real 0m3.857s
user 0m3.806s
sys 0m0.018s
15015000000
real 0m4.382s
user 0m4.331s
sys 0m0.022s
15015000000
real 0m3.976s
user 0m3.923s
sys 0m0.020s
(3.870 + 3.806 + 3.806 + 4.331 + 3.923) / 5 = 3.9471999999999996
------------------------------------------------------------
$ for i in 1 2 3 4 5 6; do time ./cycle2 30000000 1000; done;
15015000000
real 0m4.675s
user 0m4.629s
sys 0m0.016s
15015000000
real 0m5.076s
user 0m5.024s
sys 0m0.022s
15015000000
real 0m4.735s
user 0m4.687s
sys 0m0.015s
15015000000
real 0m4.727s
user 0m4.676s
sys 0m0.021s
15015000000
real 0m4.677s
user 0m4.632s
sys 0m0.018s
15015000000
real 0m5.023s
user 0m4.971s
sys 0m0.016s
(5.024 + 4.687 + 4.676 + 4.632 + 4.971) / 5 = 4.798
------------------------------------------------------------
$ for i in 1 2 3 4 5 6; do time ./cycle3 30000000 1000; done;
15015000000
real 0m4.150s
user 0m4.102s
sys 0m0.018s
15015000000
real 0m3.823s
user 0m3.784s
sys 0m0.014s
15015000000
real 0m4.918s
user 0m4.863s
sys 0m0.021s
15015000000
real 0m3.807s
user 0m3.769s
sys 0m0.015s
15015000000
real 0m4.129s
user 0m4.082s
sys 0m0.016s
15015000000
real 0m4.420s
user 0m4.366s
sys 0m0.021s
(3.784 + 4.863 + 3.769 + 4.082 + 4.366) / 5 = 4.1728000000000005
------------------------------------------------------------
Summary:
cycle1: 3.94
cycle2: 4.79
cycle3: 4.17
regards,
Bas
------------------------------
Message: 7
Date: Sun, 15 Mar 2009 23:30:47 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: folds again -- myCycle
To: Bas van Dijk <[email protected]>, Will Ness
<[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Sonntag, 15. März 2009 22:14 schrieb Bas van Dijk:
> On Sun, Mar 15, 2009 at 7:35 PM, Will Ness <[email protected]> wrote:
> > which is then just rewritable as:
> >
> > myCycle xs = ys where ys = foldr (:) ys xs
> >
> > or (arriving at the same result)
> >
> > myCycle xs = foldr (:) (myCycle xs) xs
>
> Note that, because 'ys' only has to be calculated once, GHC makes the
> most efficient code for the former one. In the latter 'myCycle xs' has
> to be calculated each time 'xs' runs empty.
>
> Here are some efficiency tests:
>
> (All programs are compiled with GHC with -O2. I excluded the first run
> of each program because of possible initialisation overhead.)
>
> ------------------------------------------------------------
> module Main where
>
> import System.Environment (getArgs)
>
> myCycle1 xs = ys where ys = foldr (:) ys xs
> myCycle2 xs = foldr (:) (myCycle2 xs) xs
> myCycle3 xs = ys where ys = xs ++ ys
>
> main = do ns:ms:_ <- getArgs
> print $ sum $ take (read ns) (myCycle1 [1..read ms])
>
> ------------------------------------------------------------
> Summary:
>
> cycle1: 3.94
> cycle2: 4.79
> cycle3: 4.17
>
> regards,
>
> Bas
Somewhat more stable timings on my box. Since it's slower, I did only take
10000000 instead of 30000000:
myCycle1: myCycle1 xs = ys where ys = foldr (:) ys xs
5005000000
real 0m2.972s
user 0m2.920s
sys 0m0.000s
5005000000
real 0m2.978s
user 0m2.920s
sys 0m0.010s
5005000000
real 0m2.952s
user 0m2.890s
sys 0m0.010s
5005000000
real 0m2.968s
user 0m2.910s
sys 0m0.010s
5005000000
real 0m2.997s
user 0m2.940s
sys 0m0.010s
5005000000
real 0m2.944s
user 0m2.890s
sys 0m0.000s
myCycle2: myCycle2 xs = foldr (:) (myCycle2 xs) xs
5005000000
real 0m4.267s
user 0m4.220s
sys 0m0.000s
5005000000
real 0m4.320s
user 0m4.220s
sys 0m0.000s
5005000000
real 0m4.227s
user 0m4.160s
sys 0m0.020s
5005000000
real 0m4.194s
user 0m4.130s
sys 0m0.010s
5005000000
real 0m4.297s
user 0m4.190s
sys 0m0.010s
5005000000
real 0m4.229s
user 0m4.180s
sys 0m0.000s
myCycle3: myCycle3 xs = ys where ys = xs ++ ys
5005000000
real 0m2.974s
user 0m2.900s
sys 0m0.020s
5005000000
real 0m2.954s
user 0m2.900s
sys 0m0.010s
5005000000
real 0m2.950s
user 0m2.900s
sys 0m0.000s
5005000000
real 0m2.959s
user 0m2.890s
sys 0m0.020s
5005000000
real 0m2.973s
user 0m2.910s
sys 0m0.010s
5005000000
real 0m2.965s
user 0m2.890s
sys 0m0.000s
Summary:
myCycle1: 2.9116s
myCycle2: 4.1833s
myCycle3: 2.8983s
------------------------------
Message: 8
Date: Sun, 15 Mar 2009 19:29:36 -0400
From: Rick R <[email protected]>
Subject: [Haskell-beginners] Binary.Put and Double
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
I need to serialize an ieee compliant 64 bit floating point number, most
often used as a Double in C.
I'm guessing that the value I need to serialize is CDouble, but Put supports
putWord64* .
How do I convert Double to Word64 while ensuring that it is in the format of
CDouble?
Is this the right approach? If so, how is this done?
--
We can't solve problems by using the same kind of thinking we used when we
created them.
- A. Einstein
--
We can't solve problems by using the same kind of thinking we used when we
created them.
- A. Einstein
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090315/e4dda9e6/attachment.htm
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 9, Issue 16
****************************************