D.challenge

bearophile bearophileHUGS at lycos.com
Tue Aug 31 02:48:52 PDT 2010


Justin Johansson:
> "challenging D to do this or that" in the
> context of, say, another programming language or some CS
> problem.

In past months I have proposed two little challenges, the second of them was to implement this RosettaCode task in D, to produce a program that is faster, shorter and uses less memory than the current D version:
http://rosettacode.org/wiki/Hamming_numbers#D

If you take a look at the Haskell version (that is much faster and uses much less RAM and has a shorter source code) you see that there is lot of space for improvement:

hamming = 1 : map (2*) hamming `merge` map (3*) hamming `merge` map (5*) hamming
     where merge (x:xs) (y:ys)
            | x < y = x : xs `merge` (y:ys)
            | x > y = y : (x:xs) `merge` ys
            | otherwise = x : xs `merge` ys
 
main = do
    print $ take 20 hamming
    print $ hamming !! 1690
    print $ hamming !! 999999

(I don't remember yet the other older challenge.)

Bye,
bearophile


More information about the Digitalmars-d mailing list