计算机代考程序代写 Haskell # Problem Sheet for Week 2 – cscodehelp代写

# Problem Sheet for Week 2

## Polymorphism

1. (Requires Section [Polymorphism](../LectureNotes/Sections/polymorphism.md))
Find out the types of the following functions. Decide if they are polymorphic.
1. `fst`
2. `(++)`
3. `not`
4. `head`
5. `tail`
6. `id`

2. Explain, in your own words, what the function `zip` does. In the expression `zip [‘x’, ‘y’] [False]`, what are the type variables `a` and `b` of `zip :: [a] -> [b] -> [(a, b)]` instantiated by?

3. Find a polymorphic function in the GHC standard library whose type contains 3 type variables or more.

4. Read Section 3.7 of Programming in Haskell. Compare the types of the examples given there with the types `ghci` indicates. (Note: some of the types that `ghci` shows use “type classes” – you will learn about these in the next lesson.)

## Types and Typeclasses

1. (Requires Section [Type classes and instances](../LectureNotes/Sections/typeclasses.md))
Run, and understand, the following examples:
1. `False == ‘c’`
2. `False == True`
3. `False == not`
4. `False == not True`
5. `not == id`
6. `[not] == [ (id :: Bool -> Bool) ]`

2. (Requires Section [Type classes and instances](../LectureNotes/Sections/typeclasses.md))
On type classes
1. Find all the basic instances of the type class `Bounded` that are defined in the GHC Prelude (the libraries that are loaded when starting `ghci`, without importing any additional libraries). Find out what `minBound` and `maxBound` are for each of the instances.
2. What type classes do the type classes `Fractional`, `Floating`, `Integral` extend? What functions to they provide?
3. Another type class:
1. Which type class defines the function `enumFromTo`?
2. Evaluate `enumFromTo` on elements of each instance of that type class.
3. Explain the different output between `:type enumFromTo 4 8` and `:type enumFromTo 4 (8 :: Int)`.

## Functions in exercises are also contained in the lecture notes.
They are collected here for your convenience.

1. Using the functions `removeLast` and `removeElem` from [Handout – Functions](../LectureNotes/Sections/functions.md), write a function that removes both the first and the last element of a list.

2. Using guarded equations, write a function of type `Int -> Int -> Bool` that returns `True` if the first argument is greater than the second and less than twice the second.

3. Write a function to pair each element of a list with its index.

4. Write a function which returns the reverse of a list if its length is greater than 7. Now modify the function so that the cutoff length is a parameter.

5. Write a function `orB :: Bool -> Bool -> Bool` that returns `True` if at least one argument is `True`.

6. Write a function `swap :: (a, b) -> (b, a)` that swaps the elements of a pair.

7. (Adapted and expanded from the book “Programming in Haskell)
Define three variants of a function `third :: [a] -> a` that returns the third element in any list that contains at least this many elements, using
1. `head` and `tail`
2. list indexing `!!`
3. pattern matching

8. (Adapted and expanded from the book “Programming in Haskell)
Define a function `safetail :: [a] -> [a]` that behaves like tail except that it maps `[]` to `[]` (instead of throwing an error). Using `tail` and `isEmpty :: [a] -> Bool`,
define `safetail` using
1. a conditional expression
2. guarded equations
3. pattern matching

Leave a Reply

Your email address will not be published. Required fields are marked *