This has been sat on my pad for about a week now.
My marks for Chapter 3 of Programming in Haskell.
Exercise 1 - What are the type of the following values?
Part 1:
[ 'a', 'b', 'c' ]
-- has type
[ Char ]
Correct
Part 2:
( 'a', 'b', 'c' )
-- has type
( Char , Char , Char )
Correct
Part 3:
[ ( False , 'O' ) , ( True , '1' ) ]
-- has type
[ ( Bool , Char ) ]
Correct
Part 4:
( [ False , True ] , [ '0' , '1' ] )
-- has type
( [ Bool ], [ Char ] )
Correct
Part 5:
Here is what I thought:
[ tail , init , reverse ]
-- has type
[ ƒ ]
I was wrong. I was unsure how to denote the type of a function within the context of the type of another function, but seeing the output from Hugs helped me realise how this is done. You denote a functions type by writing out its type, if you follow.
The correct answer:
[ tail , init , reverse ]
-- has type
[ [ a ] -> [ a ] ]
Incorrect
I've learned, or rather realised something from getting part 5 wrong, the way to express the type of a function. This really was a moment of realisation, as I've correctly written the type [o \rrrrrrrrrrrrrrrrrrrrrrrrrrrttttttf cat on the keyboard] of a function in my working for Problem 1 of Project Euler. It was so obvious, I couldn't see the wood for the trees.
Exercise 2 - What are the types of the following functions?
Part 1:
second xs = head (tail xs)
-- has type
second :: [ a ] -> a
Correct
Part 2:
swap ( x , y ) = ( y , x )
-- has type
swap :: ( a , b ) -> ( a , b )
Correct (ish)
I was right in theory but I used x and y to denote the types for the arguments however; it is Haskell convention that type variables are usually a, b, c... I made this same mistake in the rest answers but as I was correct in the theory, if not the notation, I've given myself the mark.
Part 3:
pair = ( x , y )
-- has type
pair :: a -> b -> ( a , b )
Again, I originally used x and y for the type variables.
Correct
Part 4:
double x = x * 2
-- has type
double :: Num a => a -> a
Correct
Part 5:
palindrome xs = reverse xs == xs
-- has type
palindrome :: Eq a => [ a ] -> Bool
Correct
Part 6:
twice ƒ x = ƒ ( ƒ x )
-- has type
twice :: ƒ -> x
(Completely) Incorrect
I got that last one wrong! As mentioned before, not realising how to write the type of a function within the type of another function, I was looking for some way to express "argument blah is a function". Having made the realisation stated earlier, the correct answer makes complete sense.
Exercise 3 requires no formal answers but in working through my answers using Hugs I gained the understanding of how to express a function.
Exercise 4 - Why is it not feasible in general for function types to be instances of the Eq class? When is it feasible? Hint: two functions of the same type are equal if they always return equal results for equal arguments.
Answer: It is not feasible for the majority of functions to be instances of the Eq class, and therefore return the same result given the same arguments because you would rarely (if ever) implement two functions which took the same arguments and did the same thing. It might be feasible if you were writing two different implementations of a software for fail safe reasons. Maybe.
I'm not going to lie, I'm about to learn something in a moment when I flick to the PDF of answers and find out what the correct answer is.
And the winner is...
Incorrect
The correct answer is that it's too complicated and time consuming to check all possible combinations of inputs and output of a function when it has a lot of possible inputs and outputs. It is feasible to check the equality of a function when it has few inputs and few outputs.
This was my first thought (and it's a really obvious one) but I have a tendency to over think things and get too deep into the problem.
Marks for Chapter 3 : 9 out of a possible 12 but some important lessons learned.