My function looks like this:
minus :: (Eq a) => [a] -> [a] -> [a]
minus [] xs = []
minus (y:ys) xs | y `notElem` xs = y : (minus ys xs)
| otherwise = minus ys xs
It can be used like this:
[99,44,55,22,23423] `minus` [55,22]
with output: [99,44,23423]
I wrote this because I’m looking at Project Euler problem 7, and the Sieve of Eratosthenes seems like the right tool, and it was, but I kept reading down the Wikipedia page and got to the part about Euler’s sieve.
I tried to copy/paste the code and run it in GHCi, but my version of GHCi doesn’t have a module called Data.OrdList, and I couldn’t find a function called minus in Hoogle.
This is the code from Wikipedia:
import Data.OrdList (minus)
primes = euler [2..]
euler (p : xs) = p : euler (xs `minus` map (*p) (p : xs))
If I substitute my minus function in there, I get an out of memory error, because my function isn’t lazy.
Is there a way to make a lazy minus function?
Does my minus function do the same as the minus function in the Wikipedia article?
As sepp2k pointed out, the implementation of
minusmust assume ordered lists. Here goes a possible implementation: