Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

What is the meaning of the term opportunity recognition?

Answered: 1 week ago

Question

=+ 4. Why should policymakers think about incentives?

Answered: 1 week ago

Question

=+ 2. What is the opportunity cost of seeing a movie?

Answered: 1 week ago