PA - Primes (And Other Haskell Functions)
Introduction
In this PA, you will practice writing Haskell by completing some Hashell functions. The boilerplate file is provided below. You will need to download it and fill out the stub functions.
If you look towards the bottom of the file, you will see a bunch of test functions. You should not change these functions in any way. If you load this file into ghci as-is and run the main function, you will see that all the tests fail. As you write your code, you can keep running main (or any of the individual test functions) for an indication of whether your code works.
Functions
The functions you need to write are stubbed out at the top of the file. Below are descriptions of them. You can also look at the test cases to see what they are supposed to do.
-
factors :: Integral a => a -> [a]
Takes a non-negative whole number and returns a list of all its factors, including the number itself. Should return the empty list if the argument is zero or negative. -
isPrime :: Integral a => a -> Bool
Takes a non-negative whole number and returns True if it is prime and False otherwise. Returns False if the argument is zero or negative. -
primeFactors :: Integral a => a -> [a]
Takes a non-negative whole number and returns a list of all its prime factors, possibly including the number itself. Note that 1 is not a prime factor of any number. Returns the empty list if the argument is zero or negative. -
primesUpTo :: Integral a => a -> [a]
Takes a non-negative whole number and returns a list of prime numbers up to and possibly including the argument. Returns the empty list if the argument is zero or negative. -
isPerfect :: Integral a => a -> Bool
A perfect number is a number that is exactly half the sum of its factors. For example, the factors of 6 are 1, 2, 3, and 6, whose sum is twice six, or 12.isPerfect
takes a non-negative whole number and returns True if the argument is a perfect number and False otherwise. Returns False if the argument is zero or negative. -
perfectUpTo :: Integral a => a -> [a]
Takes a non-negative whole number and returns a list of all perfect numbers up to and possibly including the argument. Returns the empty list if the argument is zero or negative. -
nextPrime :: Integral a => a -> a
Takes a whole number and returns the first prime greater than the argument. For example, the first prime greater than 6 is 7, and the first prime greater than 7 is 11. Note that since the first prime number is 2, this function always returns 2 for any number less than 2. -
generatePrimes :: Integral a => a -> [a]
Takes a non-negative whole number and returns a list of the first n primes. For example, the first three primes are 2, 3, and 5. Returns the empty list for all n less than 1.
Deliverable Requirements
Submit your modified Haskell script file on Canvas by 11:59 pm on Friday,
February 27, 2015. Please fill in your name at the top of the file. Do not
modify the testing code or the main
method.
Acknowledgement
This PA was originally designed by Dr. Chris Fox; much of the wording on this page was originally his.