HW 1
HW 1
1. Objectives
The main goal of this homework is to solidify your understanding of basic Haskell syntax.
You should strive to master the following items by the time the assignment is due:
+,-,*,/,^,**,mod,div,min,max,abs,exp,sqrt,...
&&,||,not,==,/=,<=,<,>=,>...
• Using recursion instead of loops to define functions on the integers, like factorial,
sum-of-odd-numbers-up-to-n, etc.
• Using list comprehensions and ranges. For example, the set of all numbers from 1
to 50 divisible by 7: [x | x <- [1..50], mod x 7 == 0]
(:) (++) null elem reverse length take drop head tail init last
Make sure you remember and understand the types of these functions!
Also, make sure you can explain the difference between the operators ++ and :
Additional hints
• Most of the material required for these functions is covered in Chapter 2 of the
tutorial. Some harder questions may require you to look in Chapter 4.
• Do not fear compiler errors! An error means the compiler has detected that you
wrote something different than what you meant to write. It will usually try to
explain what exactly the problem is.
1
2. Warm-up/Practice problems
The practice problems are grouped roughly by some shared themes.
4. Implement a function h2 :: Double -> Double -> Double that behaves like:
0
x2 < 10
h2 (x, y) = y 2 x2 ≥ 10, x + y < 100
x
2 − y2 x2 ≥ 10, x + y ≥ 100
1. sumUp :: Integer -> Integer computes the sum of all numbers from 0 to the
given integer, inclusive.
2. sumSquares :: Integer -> Integer computes the sum of the squares of all the
numbers from 0 to the given integer, inclusive.
3. sumOddSquares :: Integer -> Integer computes the sum of squares of all odd
numbers from 0 to the given integer, inclusive.
2
2.3. Testing Collatz conjecture
1. The Collatz function is defined as following:
1
n = 0 or n = 1
f (n) = f (n/2) n > 1 is even
f (3n + 1) n > 1 is odd
2. The Collatz Conjecture states that f (n) always returns 1 when n > 0. No one
knows if this is true. You will be very famous if you solve this problem, see
https://en.wikipedia.org/wiki/Collatz_conjecture.
Verify that the Collatz conjecture is true for 1 ≤ n ≤ 100, by using list compre-
hension to construc a list collatzCheck :: [Integer] consisting of the values of
f (n) in that range.
Hint. You can define a value of any type, including list, just like you would define
a function, except there are no input arguments. For example, you can write
myNum :: Integer
myNum = 100
myList :: [Integer]
myList = [1,4,10,20]
collatzCheck :: [Integer]
collatzCheck = ?
1. getSecond :: [String] -> String returns the second element in a list of strings.
It can crash if such element doesn’t exist.
2. makePalindrome :: String -> String takes a string and appends it to its re-
verse: makePalindrome "hello" = "helloolleh".
Hint. In Haskell, strings are lists of characters: String = [Char]. Therefore, you
can apply the same operations to pure strings that you can apply to arbitrary lists.
3. skip3 : String -> String removes the first three characters from a given string.
It may error if the string has length less than 3.
4. find7 : [Integer] -> Bool returns True if the input list contains the number
7, and returns False otherwise.
3
3. Homework assignment
This final list contains the homework problems. Save your solutions to these in a sepa-
rate file, and save the file as homework1.hs. Do not include any personally identifying
information in the file, such as your name, student id, etc.
1. Write a function radius :: Double -> Double -> Double which takes two floating-
point numbers, x and y, and returns the distance from the point (x, y) to the origin.
2. Write a function sumEvens :: Integer -> Integer which adds up all the even
numbers from 1 to its input argument (inclusive, if applicable).
sumEvens 5 = 6, sumEvens 8 = 20.
3. Using ranges and/or list comprehension, create a list of all numbers 1 ≤ n ≤ 200 di-
visible by 17. Save this as a Haskell term multiplesOfSeventeen :: [Integer].
4. Write a function multiplyEnds :: [Integer] -> Integer which multiplies the
first and the last element of the given list. If the list is empty, it should return 1.
5. Write a function getLengths :: [String] -> [Int] which takes a list of strings
and returns a list containing their lengths:
getLengths ["Hello","World"] = [5,5]
6. Write a function dropLastTwo :: [Integer] -> [Integer] which returns all
but the last two elements. (Assuming they exist.)
7. Write a function findEmpty :: [String] -> Bool that takes a list of strings,
and determines whether it contains an empty string.
8. Write a function checkPalindrome :: String -> Bool that returns True if the
string is the same when traversed in opposite direction. (Hint. You can use
Haskell’s equality operator == to check whether two strings are equal.)
9. Write a function checkSize :: [Integer] -> Bool which takes a list of integers,
and returns True if the number of elements in the list is at least 3, and the first
element in the list is at least 10. (Hint. Use the length function.)
10. Write a function checkAnySize :: Integer -> [Integer] -> Bool, which works
similar to the previous function, but it now takes an additional integer parameter
as the first argument, and checks whether both the length of the list and the first
element in the list are at least as big as this parameter.
checkAnySize 4 [5,2,10] = False, checkAnySize 4 [5,2,10,0,-1] = True.
Submission instructions
Save your file as homework1.hs. Submit it to asulearn by the deadline. Your file must
compile/load correctly. If you can’t get a function to work, provide a dummy value of
the correct type that the function is supposed to return. For example,
dropLastTwo xs = []