YOUR NAME: [Fill in here!]
----------------------------------------------------------------------
CS457/557 Functional Languages, Spring 2017 Homework 2
----------------------------------------------------------------------
This is a literate Haskell script that can be loaded directly into ghci.
> module HW2 where
----------------------------------------------------------------------
For Credit Exercises: Your solutions to the following questions are
due by email to cs457acc@pdx.edu at the start of class (2pm) on April 18.
Mailing instructions: Your mail should have the subject line "HW2".
The body of the mail message is unimportant. Your mail should have
the following attachments:
a copy of this literate Haskell file, with answers and missing
definitions filled in.
Some of the problems below will be worked during class; your
submission script can include answers for these, but they will not be
graded.
----------------------------------------------------------------------
Question 1 (Jones):
-------------------
Explain what each of the following functions does. (Note that
your answers should reflect the behavior of each function at a
high-level and should not just be a restatement of the Haskell
syntax. For example, you could say that (sum . concat) is a
function that "adds up all of the values in a list of list of
numbers", but you wouldn't get credit for saying that it is
"the composition of the sum and concat functions".)
a) odd . (1+)
b) odd . (2*)
c) ([1..]!!)
d) (!!0)
e) reverse . reverse
f) reverse . tail . reverse
g) map reverse . reverse . map reverse
----------------------------------------------------------------------
Question 2 (Hutton):
--------------------
Do textbook Ch. 5, questions 6 and 9.
Define factors:
>
>
Define perfects:
>
>
Define scalarproduct:
>
>
----------------------------------------------------------------------
Question 3:
-----------
The function
> isPrefixOf :: Eq a => [a] -> [a] -> Bool
answers whether the first argument is a prefix
of the second. Equivalently, isPrefixOf xs ys
returns true iff there exists a list zs such
that xs++zs = ys.
(a) Define isPrefixOf using a list comprehension.
(Hint: use the inits function defined in class,
which can be imported from the Data.List module.)
> isPrefixOf xs ys = error "fill in here"
(b) Give an alternative definition of isPrefixOf
(call it isPrefixOf') using explicit recursion.
----------------------------------------------------------------------
Question 4 (Hutton):
--------------------
Do textbook Ch. 6 questions 7 and 8:
Define merge:
>
>
Define msort (and any auxiliary functions you need):
>
>
----------------------------------------------------------------------
Question 5:
-----------
(a) Define a function
> inserts :: a -> [a] -> [[a]]
> inserts = error "fill in"
such that inserts x xs is the list of lists produced
by inserting x into every possible position in xs.
For example, insert 0 [1,2,3] should produce the list
[[0,1,2,3],[1,0,2,3],[1,2,0,3],[1,2,3,0]].
Hint: look at the definitions of inits and subsets
given in class.
(b) Use inserts to define a function
> perms :: [a] -> [[a]]
> perms = error "fill in"
that returns all permutations of its input list.
Note: this function is defined in Data.List, but
you should not look at that definition (which
in any case you may find quite mysterious!)
----------------------------------------------------------------------
Question 6:
-----------
In this question, "using foldr" means writing a definition
that invokes foldr in an essential way to perform all
the recursion required.
(a) Define the list concatenation function (which is already in the Prelude) using a foldr:
> concat' :: [[a]] -> [a]
> concat' = error "fill in"
Also, write down a law, similar to those shown in class,
that relates concat and ++
[ANSWER HERE!]
(b) It is easy to write an arbitrary map as a foldr, for example like this:
map f = foldr ((:).f) []
Give an argument based on types to show why
it is _not_ possible to write an arbitrary foldr as a map.
That is, explain why we cannot complete the following definition
(for arbitrary c and n):
foldr c n = map ...
[ANSWER HERE!]
(c) Define the tail function using a foldr. (This is a little challenging!)
> tail' :: [a] -> [a]
> tail' = error "define me!"
----------------------------------------------------------------------
Question 7 (Hutton):
--------------------
Do textbook Ch. 7 question 6.
> unfold :: (a -> Bool) -> (a -> b) -> (a -> a) -> a -> [b]
> unfold p h t x | p x = []
> | otherwise = h x : unfold p h t (t x)
Define chop8, map, iterate using unfold.
> chop8 :: [a] -> [[a]]
> chop8 = error "define me!"
> map' :: (a -> b) -> [a] -> [b]
> map' = error "define me!"
> iterate' :: (a -> a) -> a -> [a]
> iterate' = error "define me!"