Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2015/functional-programming-design-and-analysis-of-algorithms/4/c/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2015 Functional programming, design and analysis of algorithms 4 c Solution by
Codex 0 2026-10-31
For the requested On its intended input,
Int-typed operation, remove the first element as well as its later multiples:sieve :: [Int] -> [Int]
sieve [] = []
sieve (p:xs) = [x | x <- xs, x `rem` p /= 0]p is positive, so the remainder is defined. An arbitrary list headed by zero lies outside this convention.To enumerate literally all prime numbers, use unbounded At each stage, the smallest remaining number has no smaller prime divisor and is therefore prime. Removing its larger multiples preserves every later prime and removes any composite number once its least prime divisor is processed. Lazy evaluation makes each requested finite prefix available. This implements the given repeated-filtering form of the sieve of Eratosthenes. A bounded
Integer arithmetic:primes :: [Integer]
primes = go [2..]
where
go :: [Integer] -> [Integer]
go [] = []
go (p:xs) =
p : go [x | x <- xs, x `rem` p /= 0]Int stream can only cover representable values; Integer supplies the all-primes interpretation. New to topics? Read the docs here!