Chapter 3 Reading Guide

We learn about types and typeclasses.

You should use valid Haskell types in all of your answers. Unless needed, I stick with Int for the type for integers.

Basics

  1. How do you find the type of an expression (when using the interpreter)?
  2. What is the type of a string, written in correct Haskell type syntax?
  3. What is the type of the expression (5 == 5)?
  4. What is the type signature for a function that takes in an integer and puts out a string?
  5. What is the type signature for a function that takes in two integers and multiplies them?
  6. What is the type of (factorial 50) from the book? How is that different from an Int?
  7. Write the signature of a function prob3 that takes in a number and puts out a string.
  8. What is the correct way to write the type signature of a function prob4 that take single number and a list of numbers and puts out a list of numbers?

  9. Find out the difference between Float and Double. Summarize it here.

  10. How can you tell whether the text “string” in a program refers to a variable or a type?

  11. Write the type signature for:

    1. snd
    2. repeat
    3. init

Class Constraints

  1. Where can you find a class constraint?
  2. What special symbol separate a class constraint from a function’s type?
  3. What is the best type to give the function:

     mystery a b = if ( a < b ) then "A small" else "A large"
  4. What familiar typeclass indicates that something can be written in string form?

The Read Typeclass

  1. What does being in the Read typeclass indicate?
  2. How can you convert the string “10230” to an integer?

Miscellaneous Typeclasses

  1. What is the main use of the Enum typeclass?
  2. Aside from integers, give two other examples of things in the Enum typeclass.
  3. What typeclass tells you that something has a largest and smallest value?
  4. How would you find the smallest possible Int in Haskell?

Math

  1. What is the difference between 5 and (5 :: Int)?
  2. Can you do this math?
    1. 3.2 * (5 :: Int)
    2. 5.63 * 20
  3. What will happen when you use the following function? listBigSize aList = 3 * (length aList)

Integral vs Floating

  1. The last function discussed in the chapter is the most important by far.

    1. Write its name and signature here.
    2. What is it used for?
  2. Programming exercise: the quadratic formula. Write and test a function that takes in a,b, and c, and produces the larger root of ax^2+bx+c. (Assume there is at least one real root.)