Haskell/Solutions/Type basics

From Wikibooks, open books for an open world
< Haskell‎ | Solutions
Jump to: navigation, search

← Back to Type basics

  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.

Finding types for functions is a basic Haskell skill that you should become very familiar with. What are the types of the following functions?

  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 are True, 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.

For any functions hereafter involving numbers, you can just assume the numbers are Ints.

  1. f x y = not x && y
  2. 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