[Haskell-cafe] Hamming's Problem

Jose Luis Reyes F. jlareyes at cybercia.com
Thu Jan 17 08:00:23 EST 2008


Hi,

 

In exercise 2 of http://www.walenz.org/Dijkstra/page0145.html we need to
write a function that holds

 

(1)    The value 1 is in the sequence

(2)    If x and y are in the sequence, so is f(x,y), where f has the
properties

a.       f(x,y) > x

b.      (y1 > y2) => (f(x,y1)>f(x,y2))

 

This is a solution for this problem, but an inefficient one

 

hammingff :: [Integer]

hammingff = 1 : merge [ h x y | x <- hammingff, y <- hammingff ] [ h x y | y
<- hammingff, x <- hammingff ]

 

h x y = 2*x+3*y

 

merge :: (Ord a) => [a] -> [a] -> [a]

merge [] xs = xs

merge ys [] = ys

merge (x:xs) (y:ys) = case compare x y of

    LT -> x : merge xs (y:ys)

    GT -> y : merge (x:xs) ys

    EQ -> x : merge xs ys

 

 

anybody  has a better solution?.

 

Thanks

Jose

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080117/d7114d31/attachment.htm


More information about the Haskell-Cafe mailing list