CS 430: Lecture 10 - Haskell Midterm Review
Dear students:
Today we have a practice exam for the upcoming Haskell midterm. A number of you have submitted candidate questions, and I have collected most of them below. I left out several that would take too much time, were similar to others, or lacked solutions.
Problems
-
Write a function
join
that concatenates a list of strings together. -
Write a function
startEnd
that takes in a list of any type and returns a 2-tuple containing the first and last element of the list. -
Write function
box
that takes a list ofInt
triples, each representing the length, width, and height of a box in inches. The function returns the total volume of all items as a string in the form"42276in^3"
. -
Write function
percentWeight
that takes a list of weights and returns a list of percentages of the total weight of all items as strings. For examplepercentWeight [12, 24, 36, 48]
yields["10.0%","20.0%","30.0%","40.0%"]
. -
Write a function
middle3
that accepts a list and returns a list of just the three middle elements. -
Assume you have a file named
words.txt
containing one word per line. Define amain
function that reads in the file and prints out each word and its length with a sentence of the formWORD has COUNT characters
. For example, suppose the file contains this text:Your program should produce this output:Computer Science Homework
Computer Science Homework
Computer has 8 characters Science has 7 characters Homework has 8 characters
Computer has 8 characters Science has 7 characters Homework has 8 characters
-
Fill in the blanks in the code below. Use
Maybe
to communicate an invalid result when the side lengths are negative.main = do let result = pythagorean (1, 2) case result of ______ -> print c ______ -> putStrLn "Error: Both values have to be greater than or equal to zero." pythagorean :: ______ -> ______ pythagorean (a, b) = if a >= 0 && b >= 0 then ______ $ sqrt $ a ** 2 + b ** 2 else ______
main = do let result = pythagorean (1, 2) case result of ______ -> print c ______ -> putStrLn "Error: Both values have to be greater than or equal to zero." pythagorean :: ______ -> ______ pythagorean (a, b) = if a >= 0 && b >= 0 then ______ $ sqrt $ a ** 2 + b ** 2 else ______
-
Write a record type named
Business
with the one and only constructorBusiness
. Give it aname
field of typeString
, adescription
field of typeString
, and anemployees
field of typeInt
. Have your type derive theShow
typeclass. Create also atoInfo
function that takes in aBusiness
and returns a string of the form:"Business: Ron's Trucking with 500 employees"
. -
Suppose you are given an array of employee salaries at AmaGoogle, Inc. Word just came down from management that these salaries must be tripled right away. Write a function named
tripleSalaries
that accepts a list of salaries and returns the list of salaries with their values tripled. -
Write a
main
function that accepts three command-line arguments: a number, an arithmetic operator, and another number. It prints the value of the expression. -
Write a
main
function that accepts a list of file paths as command-line arguments. It prints the first line of each file. -
Write a function
reach
that accepts a list of numbers as a parameter and returns the distance between its least and greatest values. -
Your boss just sent you a list that contains thousands of integers. Your task is to sort them by value, and then write that sorted list to a file named
sorted.txt.
You only have until 5 PM or you risk being fired, so you aim for a solution that requires as little code as possible. -
Define a
Genre
enum to describe a type of television channel, with variants for news, sports, and cartoons. Define aChannel
data type with a single variant namedChannel
that has fields for the channel's genre and number. -
Define type
Run
to represent a repeating character. Give it two variants.Single
has a field for a character that appears just once.Multiple
has a field for a count and a character that appears a specified number of times. For example,Single '@'
represents the string"@"
, andMultiple 4 '@'
represents"@@@@"
. Write functiondecodeRun
to produce a run's string. Write functiondecodeRuns
to decode a list of runs and join their result into a single string. -
Write a
main
function that accepts three command-line arguments: a path to an input file, an integer , and a path to an output file. It writes to the output file only the lines of the input file that have at least characters. -
Write a
main
function reads in a file of space-separated integers and multiplies each by a number passed as a command-line argument. -
Write an impure function
printBiggersOfTwo
that accepts a list of pairs and prints the larger of each on its own line. In the case of a tie, print the first component. -
Write a function
squareDigits
that accepts aString
and returns a list of the squares of any digits contained in theString
. For example,squareDigits "asd3_ui9d3"
yields[9, 81, 9]
. Use higher- order functions. UseisDigit
fromData.Char
to identify digits. -
Write a function
swapPairs
that accepts a list of tuples and returns a new list of pairs with the first and second elements of each pair swapped. -
Write a
main
function reads in a file of space-separated integers and prints their sum. -
Define a
Robot
data type that has variantsWalking
andTalking
. Both variants have fieldsname
andbattery
of typesString
andInt
, respectively. Walking robots also have a fielddistance
of typeInt
. Talkings robots also have a fieldvolume
of typeInt
. MakeRobot
an instance of theShow
typeclass. -
Suppose you have two lists, one of
(String, String, String)
and one ofDouble
. They are of equal size. You want to replace the third string in each tuple with its correspondingDouble
. Write a functionreplaceThird
that accomplishes this task.
TODO
Here's your list of things to do in the near future:
Wrap up work on project 2. For your final progress report, prepare a 2-3 minute screencast of your project that briefly demonstrates its execution. Give voiceover commentary. Pick three programming language ideas and reflect on how they are exhibited in your source code. Upload the video to a streaming service of your choice and share a link to it in your group channel on Discord. Also commit and push to GitHub. Do all this before noon Monday.
See you next time.
Sincerely,