Haskell/Solutions/Type basics

← Back to Type basics

Exercises
  1. Try using :type on the literal value "H" (notice the double quotes). What happens? Why?
  2. Try using :type on the literal value 'Hello World' (notice the single quotes). What happens? Why?
  1. "H" is a String with just one element.
  2. 'Hello World' is invalid because Chars consist of only one character.
Exercises

What are the types of the following functions? For any functions involving numbers, you can just pretend the numbers are Ints.

  1. The negate function, which takes an Int and returns that Int with its sign swapped. For example, negate 4 = -4, and negate (-2) = 2
  2. The (||) function, pronounced 'or', that takes two Bools and returns a third Bool which is True if either of the arguments were, and False otherwise.
  3. A monthLength function which takes a Bool which is True if we are considering a leap year and False otherwise, and an Int which is the number of a month; and returns another Int which is the number of days in that month.
  4. f x y = not x && y
  5. g x = (2*x - 1)^2
  1. negate :: Int -> Int
  2. || :: Bool -> Bool -> Bool
  3. monthLength :: Bool -> Int -> Int

And in the second round:

  1. f :: Bool -> Bool -> Bool
  2. g :: Int -> Int