Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework 1 This homework assignment is out of 1 0 0 points ( the number of points for each exercise is indicated beside the exercise

Homework 1
This homework assignment is out of 100 points (the number of points for each exercise is indicated beside the exercise). You may submit your solution in the text window in iCollege, as a .txt file, or in some other commonly used format, e.g.,.pdf,.docx, etc.
Note that partial credit is available for explanations which convey the right ideas, even if they may not be totally correct or complete, and so showing or explaining your work or thought process (why the answer is what you think it is) is always a good idea.
1. Consider the function
second xs = head (tail xs)
(a)(10 points). What is the type of second? Note: that type has a formal meaning in Haskell,
that is, there is only one correct (and very precise) answer.
(b)(10 points). Explain why second has the type that it has. That is, why does chaining tail and head together (or composing them) in the function definition above give rise to the type that second has?
2. Consider the functions swap and pair, defined respectively as swap (x,y)=(y,x)
pair x y =(x,y)
(a)(10 points). What is the type of swap?
(b)(10 points). What is the type of pair?
(c)(10 points). Notice that, in the types of swap and pair, that x and y (from the respective function definitions above) correspond to two different polymorphic type variables. Why is this, i.e., why wouldnt they be the same?
3. Consider the functions double and palindrome, defined respectively as double x = x*2
palindrome xs = reverse xs == xs
(a)(10 points). What is the type of double?
(b)(10 points). What is the type of palindrome?
(c)(10 points). Notice that there are certain type class constraints present in both types of double and palindrome. What is the reason for this, in either case?
4.(10 points). Consider the function
twice f x = f (f x)
What constraints does twice place on the type that input (function) f can have? Hint: try seeing
what happens when calling twice head [1,2,3] in GHCi.
5.(10 points). Notice that the list [tail, init, reverse] has a type, however, if we try to construct
[tail, init, reverse, head], it will generate a type error. Why is this?
1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

How is a CFD contract terminated?

Answered: 1 week ago