Let's say I want to get a sorted endless list of all forwards to exponent n .
I have a function to combine two sorted lists and a function that gives me prime numbers.
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 returns the correct result, and the other does not. The only difference is that the first version displays the preprocessors in the same way as [[2,3,5,7, ...],[4,9,25,...]] , and the second version displays the simplest ones [[2,4,8],[3,9,27],[5,25,125], ...] . You see, infinity is on a different level in the list.
Do you have an explanation why the second function does not produce any output?
haskell map fold infinite
Benji wa
source share