m b
m b

Reputation: 73

Generating tuples of primes with a list comprehension, each tuple having higher sum

I searched, but I didn't find something that helped, so I post a new question. I am learning Haskell, and this is an exercise I don't understand.

I want to create an infinite list of tuples of prime numbers, each tuple's sum being a higher even number, beginning with 2. So the output should be e.g. : [(0,2), (2,2), (3,3), (3,5)...

I want to do it with a list comprehension only, no recursion. The problem is that each tuple's sum must be higher. I defined my own primetest function prim, and started like:

goldbachList :: [(Int, Int)]
goldbachList = [(a,b) | b <- [1 ..], a <- [0 .. b], prim a || a == 0, prim b, a+b >= 2, even (a+b)]

but obviously this creates a infinite list with much more tuples than what I want to have. Is there a possibility to include the condition, that every tuple must sum the next even number compared to its predecessor inside the list comprehension?

Upvotes: 1

Views: 277

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152682

Since this sounds like a fun programming exercise, I will give a hint rather than a complete solution. Here's a template to get you started: [findPair n | n <- [2,4..]]. You can also implement findPair with a list comprehension (plus head -- which is not recursive -- if you believe the conjecture to be true!).

Upvotes: 2

Related Questions