Question: Install the Haskell Platform on your machine to answer some of the questions. The language used is Racket. https://www.haskell.org/platform/ For the following three questions, please

Install the Haskell Platform on your machine to answer some of the questions. The language used is Racket.

https://www.haskell.org/platform/

For the following three questions, please explain your answer and include any citations if you consulted outside resources.

  1. For the function:

func :: [a] -> [a] -> [a]

func x y = x ++ y

Which of the following are true?

a) x and y must be of the same type

b) x and y must both be lists

c) if x is a String, then y must be a String

  1. What do you think the type of q is? First give your guess, then check what the Haskell compiler thinks by running the following lines of code. Explain any difference between your prediction and the inference made by the compiler.

addOne :: Num a => a -> a

addOne x = x + 1

q :: ????

q = head (map addOne [1, 2, 3, 4])

:t q

  1. Imagine a function f that has the type Ord a => a -> a -> Bool, and then we partially apply it to one numeric value.

  1. What do you think the type signature of the partially applied function is?

  2. Provide your own example of such a function f, and also a function g that is the partially applied f.

  3. Then, run :t g in your Jupyter notebook, to find out whether the type signature that Haskell infers is the same as the type signature that you predicted. Explain any differences.

For each of the following functions, write the type signature above each definition. (The type signature will be worth 1 point per problem.)

  1. Using guards, write a function exp that takes two arguments, x and n, and returns xn. Your function should recursively multiply x by itself, n times. (Dont worry about n values that are negative or decimals.)

  1. The following function uses if-then-else to branch. Rewrite it to use pattern matching, instead (and write its type signature, too):

foo x y bool = if bool then x else y

  1. Using any conditional branching strategy you like, implement your own version of the built-in function takeWhile. Call it myTakeWhile.

  1. Recall the repeated function in Racket from Lab 2. One of its valid implementations looked like this:

(define (repeated f count)

(lambda (x)

(if (= count 0)

x

(f ((repeated f (- count 1)) x)))))

Using pattern matching, implement repeated in Haskell. For the type

signature, assume well only ever pass in a function that takes, and returns, an Int.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!