Homework 1
Instructions
The source code of this homework can be found here. You should fill in the definitions of the required functions but do not change the types of the functions.
How to submit: TBA
> module HW1 where
> import Data.Char
Problem 1: Strings
In Haskell the String
data type is defined to be a list of Char
acters, so String
can be manipulated via list comprehension.
For example, bellow list comprehension is used to combine each possible adjectives with each possible noun.
> [adj ++ " " ++ noun | adj <- ["lazy", "nasty"], noun <- ["cat", "language"] ]
["lazy cat","lazy language","nasty cat","nasty language"]
You are requested to use list comprehension to define the following two functions on String
s.
- Complete the function
removeUpper
that removes all uppercase characters from its String argument. For exampleremoveUpper "Hello World!" = "ello orld!"
. Hint: use the library functionisLower
.
> removeUpper :: String -> String
> removeUpper xs = error "Define me!"
- Complete the function
noIdent
that removes any non-letter character of its String argument to lower. A letter is one of the charactersa..z
orA..Z
. For examplenoIdent "Hello World!" = "HelloWorld"
. Hint: use the library functionelem
.
> noIdent :: String -> String
> noIdent xs = error "Define me!"
- Now use recursion to define the function
isPrefixOf xs ys
that turnsTrue
if and only ifxs
is prefix ofys
. For exampleisPrefixOf "Haskell" "I like Haskell" = False
andisPrefixOf "I like" "I like Haskell" = True
.
> isPrefixOf :: String -> String -> Bool
> isPrefixOf xs ys = error "Define me!"
Problem 2: Factoring
We say that a
is a factor of n
when there exists an integer b
so that n = a * b
.
- Define a function
factors n
that returns all functors ofn
. For example,factors 12 = [1,2,3,4,6,12]
andfactors 13 = [1,13]
.
> factors :: Int -> [Int]
> factors n = error "Define me!"
We say that an integer n
is prime when its only factors are 1
and n
.
- Define a function
isPrime n
that returnsTrue
if and only ifn
is prime. For exampleisPrime 12 = False
andisPrime 13 = True
.
> isPrime :: Int -> Bool
> isPrime n = error "Define me!"
- Optimize the
factors n
function to only call themod
function at most √ n times. Note that factors appear in pairs. If you foundi
to be a factor ofn
, thendiv n i
is also a factor ofn
. Hint: define a helper recursive functionfactorsRec n i
and recurse on increasingi
.
> factorsOpt :: Int -> [Int]
> factorsOpt n = error "Define me!"
Test your optimization. The below function
sameElems xs ys
checks that the two input lists have the same elements, by checking that- the two input lists have same length and
- all elements of the list
xs
are elements of the listys
.
> sameElems :: [Int] -> [Int] -> Bool
> sameElems xs ys = length xs == length ys
> && all (`elem` ys) xs
Use sameElems
to write a function testFactors n
that tests that the two factor functions you wrote above return the same factors for every integer up to n
. Hint: use the library function and
.
> testFactors :: Int -> Bool
> testFactors n = error "Define me!"
Problem 3: Coloring
LetColor
be the red, green, blue or yellow color data type.
> data Color
> = Red | Green | Blue | Yellow
> deriving (Eq, Show)
Note the above deriving
annotation teaches Haskell how to compare colors and how to turn them to strings for printing.
Balkan
data type defines the countries that belong in the Balkan area.
> data Balkan
> = Albania | Bulgaria | BosniaAndHerzegovina
> | Kosovo | Macedonia | Montenegro
> deriving (Eq, Show)
Two countries are adjacent when they share the same border. The below adjacencies
list captures all the balkan adjacencies: x
is adjacent to y
when either elem (x,y) adjacencies
or elem (y,x) adjacencies
.
> adjacencies :: [(Balkan,Balkan)]
> adjacencies =
> [ (Albania, Montenegro), (Albania, Kosovo), (Albania, Macedonia)
> , (Bulgaria,Macedonia)
> , (BosniaAndHerzegovina, Montenegro)
> , (Kosovo, Macedonia), (Kosovo, Montenegro)
> ]
We call coloring a list of type [(Balkan,Color)]
that related each Balkan country with a color. A coloring is good with respect to an adjacency matrix when every two adjacent countries have a different color. A coloring is complete with respect to an adjacency matrix when it colors every country in the matrix. Good colorings may be incomplete.
- Write a function
isGoodColoring adj coloring
that returnsTrue
if and only if thecoloring
list is good with respect to the inputadj
acency list.
> isGoodColoring :: [(Balkan, Balkan)] -> [(Balkan,Color)] -> Bool
> isGoodColoring adj coloring = error "Define me!"
- Define
colorings
to return all the good and complete colorings of the adjacency listadjacencies
.
> colorings :: [[(Balkan, Color)]]
> colorings = error "Define me!"