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 Characters, 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 Strings.
- Complete the function
removeUpperthat 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
noIdentthat removes any non-letter character of its String argument to lower. A letter is one of the charactersa..zorA..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 ysthat turnsTrueif and only ifxsis prefix ofys. For exampleisPrefixOf "Haskell" "I like Haskell" = FalseandisPrefixOf "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 nthat 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 nthat returnsTrueif and only ifnis prime. For exampleisPrime 12 = FalseandisPrime 13 = True.
> isPrime :: Int -> Bool
> isPrime n = error "Define me!"- Optimize the
factors nfunction to only call themodfunction at most √ n times. Note that factors appear in pairs. If you foundito be a factor ofn, thendiv n iis also a factor ofn. Hint: define a helper recursive functionfactorsRec n iand recurse on increasingi.
> factorsOpt :: Int -> [Int]
> factorsOpt n = error "Define me!"Test your optimization. The below function
sameElems xs yschecks 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
xsare elements of the listys.
> sameElems :: [Int] -> [Int] -> Bool
> sameElems xs ys = length xs == length ys
> && all (`elem` ys) xsUse 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 coloringthat returnsTrueif and only if thecoloringlist is good with respect to the inputadjacency list.
> isGoodColoring :: [(Balkan, Balkan)] -> [(Balkan,Color)] -> Bool
> isGoodColoring adj coloring = error "Define me!"- Define
coloringsto return all the good and complete colorings of the adjacency listadjacencies.
> colorings :: [[(Balkan, Color)]]
> colorings = error "Define me!"