Module 09 Project
Introduction
In this PA we will simply be practicing writing some Haskell functions.
Project starter files: p3.zip
Requirements
The functions you need to write are stubbed out in the P3.hs
file. Here are
descriptions of them (you can also look at the test cases in Test.hs
to see
what they are supposed to do, of course).
factors :: Integral a => a -> [a]
- This function must take a non-negative
whole number n
and return a list of all its factors, including n
itself.
Its result must be the empty list if n
is zero or negative.
isPrime :: Integral a => a -> Bool
- This function must take a non-negative
whole number n
and return True
if it is prime and False
otherwise. Its result
must be False
if n
is zero or negative.
primeFactors :: Integral a => a -> [a]
- This function must take a
non-negative whole number n
and return a list of all its prime factors,
possibly including n
itself. Note that 1 is not a prime number. Its result
must be the empty list if n
is zero or negative.
primesUpTo :: Integral a => a -> [a]
- This functions must take a
non-negative whole number n
and return a list of prime numbers up to and
possibly including n
. Its result must be the empty list if n
is zero or
negative.
isPerfect :: Integral a => a -> Bool
- A perfect number is a number that is
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
must take a non-negative whole
number n
and return True
if n
is a perfect number and False
otherwise.
Its result must be False
if n
is zero or negative.
perfectUpTo :: Integral a => a -> [a]
- This function must take a
non-negative whole number n
and return a list of all perfect numbers up to
and possibly including n
. Its result must be the empty list if n
is zero or
negative.
nextPrime :: Integral a => a -> a
- This function must take a whole number
n
and return the first prime greater than n
. 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 must always return 2 for any number
less than 2.
generatePrimes :: Integral a => a -> [a]
- This function must take a
non-negative whole number n
and return a list of the first n
primes. For
example, the first three primes are 2, 3, and 5. This function must return the
empty list for all n
less than 1.
Some test routines are included in Test.hs
. You can build and run the tests
using the provided makefile (e.g., run make
and then ./Test
). However, your
submission will be graded based on additional tests not provided to you, so you
should write your own test cases as well.
You can also test your functions yourself using ghci
by executing :l P3.hs
to load your solution file (which you will need to do every time you make a
change to it). You could also just run the interpreter with the filename as a
parameter (e.g., ghci P3.hs
).
For this assignment, you may NOT use any classes or methods that are not in the Haskell Prelude. If you are unsure about a particular class or method, you can consult the documentation.
Submission
Your program must contain implementations of the methods described above. You
must name your Haskell script file P3.hs
. You must put your name in a comment
at the top of the file. As usual, decompose your methods to make them more
readable, use good names, indent properly, and so on. You will partly be graded
on the readability of your code but mostly on whether it works.
Submit ONLY P3.hs
to the appropriate assignment on Canvas by the deadline.