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.
-
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
-
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
-
Imagine a function f that has the type Ord a => a -> a -> Bool, and then we partially apply it to one numeric value.
-
What do you think the type signature of the partially applied function is?
-
Provide your own example of such a function f, and also a function g that is the partially applied f.
-
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.)
-
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.)
-
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
-
Using any conditional branching strategy you like, implement your own version of the built-in function takeWhile. Call it myTakeWhile.
-
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started