Let's say I want to get a sorted infinite list of all primepowers up to exponent n
.
I have got a function to merge two sorted lists and a function that gives me primes.
merge :: Ord t => [t] -> [t] -> [t]
merge (x:xs) (y:ys)
| (x <= y) = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
merge xs [] = xs
merge [] ys = ys
primes :: [Integer]
primes = sieve [2..]
where
sieve [] = []
sieve (p:xs) = p : sieve (filter (\x -> x `mod` p /= 0) xs)
I have two versions of the listOfPrimepowers
function:
primepowers :: Integer -> [Integer]
primepowers n = foldr (merge) [] (listOfPrimepowers n)
-- terminating
listOfPrimepowers' n = map(\x -> (map(\y -> y ^ x) primes)) [1..n]
-- non terminating
listOfPrimepowers'' n = map(\x -> (map(\y -> x ^ y) [1..n])) primes
One delivers the correct result and the other one doesn't. The only difference is, that the first version maps the primepowers in a way like [[2,3,5,7, ...],[4,9,25,...]]
and the second version maps the primepowers like [[2,4,8],[3,9,27],[5,25,125], ...]
. You see, the infinity is at another level in the list.
Do you have an explanation why the 2nd function doesn't produce any output?