Advanced Functional Programming TDA342/DIT260 (Mocked Online Exam)

Dear students, here you can see a **mocked (fake) exam** so that you get familiar with the way to submit, how to write your answers, and the submission system of Canvas. Please, raise your questions and comments in the "Discussions" section of the course.
The whole exam is in a `.pdf` file in case you have connection problems. Search for the file `mocked_exam.pdf` in the Files tab.
Date Sunday, 15 March 2020
Time Sunday, 15 March 2020, 15:00 - Wednesday, March 18 2020, 18:00
Submission window Wednesday 18 2020, 18:00 - 18:10

Preliminaries

Below you find the information that it is often on the **first page of a paper exam**

Preliminaries about this online exam

Below you find the information that is related to **the online exam**

What and how to submit

Exercise 1 (20 points)

File src/Exam/Ex1.hs

You have seen before the Haskell function

reverse :: [a] -> [a]

In this exercise, we will explore a bit more.

Task 1 (10 pts)

  1. Implement the function

    freverse :: [a] -> [a]
    

    which simply implements reverse using foldr.

    foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
    
  2. Question: Is freverse faster than Haskell reverse? Justify your answer.

Task 2 (10 pts)

Write a Quick Check property that test that your function, freverse behaves the same as Haskell's reverse.

Exercise 2 (20 points)

File src/Exam/Ex2.hs

It is often very useful to take the last element of a list.

Task 1 (20 pts)

Using function freverse from the previous exercise, write a function which takes the last element of a list:

myLastElem :: [a] -> a

Exercise 3 (20 points)

File src/Exam/Ex3.hs

Monad are very cool abstractions.

Given the following data type definition:

data X a = X a

Task 1

  1. Give the instance Monad for X as the identify monad.

  2. Can you give instances for Applicative and Functor using the methods from Monad? Justify your answer.