0

In the following prime sieve:

primes :: [Integer]
primes = sieve [2..]
  where
    sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0]

What do x | x <- xs and x `mod` p > 0 mean?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
carrotzoe
  • 127
  • 1
  • 1
  • 8
  • 1
    Please search for List Comprehensions. Also, mod is the Modulo function. – shree.pat18 Nov 21 '15 at 06:36
  • the list-comprehension syntax is where to close to the set-syntax you would use in math - so it means *the list of all `x` in `xs` where ``x `mod` p > 0``* – Random Dev Nov 21 '15 at 09:29
  • I recommend reading through all of http://learnxinyminutes.com/docs/haskell/ for a quick overview of the syntax. – Chris Martin Nov 21 '15 at 11:50

1 Answers1

2

[ x | x <- xs, x `mod` p > 0] is a list of xs made of elements from xs, but only those elements that satisfy the x `mod` p > 0 condition (mod returns the remainder after the first number is divided by the second number, so you're asking for elements of xs that aren't divisible by p).

Random Dev
  • 51,810
  • 9
  • 92
  • 119