CSCE 314: Programming Languages
Assignment 1
Objective
The aim of this assignment is to give you practice writing basic Haskell functions, including recursive functions.
Instructions
- Write a piece of code for each function.
- Download the testcase for each function. Then test it.
- This assignment asks you to implement a set of Haskell functions. Each of those functions can be defined with just a few lines of code. If your implementations start to get much longer, it may be a sign that there are less complicated solutions that you may be missing.
- A piece of advice: Try to complete the assignment before downloading and using the tests (described below) to check your answers. That'll make sure you're thinking about solving as much of the problem as you can see without just `coding to the test'.
Basic Haskell Drills
For each of the following problems, write: (a) A Haskell function type definition and (b) an actual Haskell function of the given name. For example, if the problem was:
double
: double a given number
the answer could be:
double :: Num a => a -> a double x = x + xIf the preceding code is in a file called
ans.hs
then, once I'm ready to test it, I would import
the file TestDouble
in my session.
Within GHCI, the following shows how to test (the user's commands are in
black):
$ ghci GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help Prelude> :l ans.hs [1 of 1] Compiling Main ( ans.hs, interpreted ) *Main> :a TestDouble.hs [2 of 2] Compiling TestDouble ( TestDouble.hs, interpreted ) Ok, modules loaded: Main, TestDouble. *TestDouble> :main Cases: 3 Tried: 3 Errors: 0 Failures: 0 Counts {cases = 3, tried = 3, errors = 0, failures = 0} *** Exception: ExitSuccess *TestDouble> :quit
If the problem notes that it is recursive, use a recursive implementation (other questions could be recursive, also, and there could be non-recursive solutions).
increaseTen
: increase a number by 10circleArea
: compute the area of a circle of a given radiusmidList
: given a list, return a new list that does not include the first or last element of the original listcountdownList
: given two numbers where the second is larger than the first, get a list of integers from the second down to the first (inclusive)isRight
: given the lengths of three sides of a triangle in order from smallest to largest, determine whether the triangle is a right trianglemultComplex
: given two complex numbers (i.e. of the form a+bi), compute their product. Each complex number should be represented as a tuple with a real part and an imaginary part.countChar
: given a character and a string, count how many times that character appears in the string (recursive)getFirsts
: from a list of 2-tuples, return a list of just the first elements from the tuples (recursive)halfList
: create a new list that contains every other element of an input list (i.e., retain the first element then drop the next, retain, drop, etc.) (recursive)uppercaseList
: given a string, create a list of tuples, where the tuple contains three Boolean variables: one indicating if it is an upper case letter, one indicating if it is lower case, and one indicating if it is a number.- For this, you must first include the line "
import Data.Char
" at the top of your source file.
> uppercaseList "Ab1." [(True, False, False), (False, True, False), (False, False, True), (False, False, False)]
- For this, you must first include the line "
Alternating series
An alternating series is a summation with a sign change between terms:
Write a Haskell function called altSeries
that computes an alternating series of an input list.
altSeries :: Num a => [a] -> a
Check your answers
The collection of tests for this assignment are here.
Note that to use these tests, you need to have HUnit
installed in your haskell environment.