Haskell Exercises

CMSC 331

Fall 2015

REVISED 11/25/2015

Prepare a Haskell program that includes the following function definitions and PutStrLn calls to show that they work as intended. The program needs to run on the GL platforms. As before, use the UNIX script command to list the program and the results. Email the file to me and the TA Rujuta Palande no later than midnight Tuesday/Wednesday December 8, 2015. Work submitted before Thursday December 3 may be eligible for extra credit.

You are free to install Haskell on your own PC, if you wish. Just be able to prove that your program ran as required.

Choose any six of these seven exercises. If you do all seven, that too may be eligible for extra credit.

  1. Given three real numbers representing the lengths of the sides of an ordinary triangle, use Heron's Formula to print the area of that triangle.
  2. Define a function myElem that decides if a value is an element of a list. (Note that elem is already a built-in function in Haskell. Don't use the elem function.) If the value is found, return its position in the list, with the first element being in position one. If the element is not found, return zero. The function memberP has been added to haskellCS2.hs as an exampple, or your use as a helper function. The signature of myElem would be:
    myElem :: Eq a => a -> [a] -> Int
  3. Using a high order function or list comprehension, define a function replicate :: Int -> a -> [a] that produces a list of identical elements. For example,
    > replicate 3 "tsk"
    ["tsk","tsk","tsk"]
  4. Define a function median :: [Int] → Int that finds the median of a list of integers. The median is the element that is in the middle when the list is sorted. If the list is of even length, the median is the arithmetic mean of the two middle elements. The median of the empty list is for you to decide, but zero or the empty list itself are reasonable choices. Your function may use a built-in sort function if you wish, although this isn't necessary. The cheat sheet file haskellCS2.hs has an example of how the built-in sort function is used. The median function would work as shown here:
    > median [1,2,3,4,5,6]
    3.5
    > median [11, 2, 13]
    11
  5. For any positive integer k, we can compute the sum of the divisors of k. For example, sumOfDivisors 8 would be 1+2+4=7. But sumOfDivisors 6 is 6, since 1+2+3=6. A number that equals the sum of its divisors is said to be perfect. Implement the sumOfDivisors function. Compute sumOfDivisors 496.
  6. Define a recursive function merge :: Ord a ⇒ [a] → [a] → [a] that merges two sorted lists to give a single sorted list. The two input lists need not be the same length. One or both may be empty. Your definition should be defined using explicit recursion. For example
    > merge [2,5,6] [1,3,4]
    [1,2,3,4,5,6]
  7. Suppose we wanted to generate an infinite sequence of prime numbers. Recall that an integer is prime if and only if it has no divisiors other than itself and one. If we have a set of primes called P = {p1, p2,..., pn}, note that the product of the pi's plus one will also (probably if not certainly) be a prime. For example. if we start with P={2} we derive P={2,3}, and then P={2,3,7}, then P={2,3,7,43}, and so fortth. This process generates only a small fraction of all the primes, but you can see that an infinite sequence of primes is generated. Print the first eight of these lists P, with the first being P={2} as shown above, in a format of your choice.