Book creator (disable)

Haskell/Solutions/Type basics

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

← Back to Type basics


Exercises

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 'and', that takes two Bools and returns a third Bool which is True if both the arguments were, and False otherwise.
  3. 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.

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
  3. h x y z = chr (x - 2)
  1. negate :: Int -> Int
  2. && :: Bool -> Bool -> Bool
  3. || :: Bool -> Bool -> Bool

And in the second round:

  1. f :: Bool -> Bool -> Bool
  2. g :: Int -> Int
  3. h :: Int -> a -> b -> Char. The usage of a and b is explained in the next section. In this case the function doesn't use y or z, so the types can be anything.