1

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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
m b
  • 73
  • 6
  • 1
    Technically 0 is not a prime. In fact the [Goldbach's conjecture](http://en.wikipedia.org/wiki/Goldbach%27s_conjecture "Wikipedia article for Goldbach's conjecture") states that every even *greater than 2*, can be written as the sum of two primes. – Bakuriu Jan 26 '15 at 20:30

1 Answers1

2

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!).

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380