• Home
  • Textbooks
  • Programming in Haskell
  • Types and classes

Programming in Haskell

Graham Hutton

Chapter 3

Types and classes - all with Video Answers

Educators


Chapter Questions

01:36

Problem 1

What are the types of the following values?
[’a’,’b’,’c’]

(’a’,’b’,’c’)

[(False,’O’),(True,’1’)]

([False,True],[’0’,’1’])

Susan Cooper
Susan Cooper
Numerade Educator
01:09

Problem 2

Write down definitions that have the following types; it does not matter what the definitions actually do as long as they are type correct.
bools :: [Bool]

nums :: [[Int]]

add :: Int -> Int -> Int -> Int

copy :: a -> (a,a)

apply :: (a -> b) -> a -> b

Suman Saurav Thakur
Suman Saurav Thakur
Numerade Educator
00:31

Problem 3

What are the types of the following functions?
second xs = head (tail xs)

swap (x,y) = (y,x)

pair x y = (x,y)

double x = x*2

palindrome xs = reverse xs == xs

twice f x = f (f x)

James Kiss
James Kiss
Numerade Educator

Problem 4

Check your answers to the preceding three questions using GHCi.

Check back soon!
03:15

Problem 5

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.

Jennifer Stoner
Jennifer Stoner
Numerade Educator