Lesson 37. Capstone: Building a prime-number library
This capstone covers
- Building a new project by using stack
- Writing basic library functions for working with prime numbers
- Using stack test and QuickCheck to check for bugs as you go
- Refactoring code to fix errors
- Adding new functions and tests to your project as you go
So far in this unit, you’ve focused on one problem: creating a program for working with palindromes. For this capstone, you’ll be reiterating over all the work you’ve done creating modules and learning about stack with a new problem. This time you’ll be working on creating a library to work with prime numbers. You’ll focus on three essential problems:
- Listing out primes less than a specified number
- Determining whether a given number is prime
- Breaking a number into its prime factors
The first thing that you’ll need is a way to create a list of prime numbers. Here’s the type signature for that list:
You’ll achieve this by using a prime sieve, which works by filtering out the prime numbers. In terms of types, this will require you to take an [Int] of possible primes and then return an [Int] of just primes. The function that will perform this work is sieve: