next up previous contents
Next: Polymorphism Up: Functional programming in Haskell Previous: Defining functions in Haskell   Contents

Functions as first class objects

As we have seen, because of currying, a function typically has a return value that is itself a function (i.e., the value returned has a type of the form X -> Y, where X and Y may themselves be further function types). Symmetrically, there is nothing to stop us from functions whose input type is a function type.

Thus, there is no distinction between basic types such as Int, Bool, etc and function types as far as input and output types of functions are concerned. In the literature, this property is often referred to as having ``functions as first class objects''. Contrast this to a language like C or Java where there is no natural way to pass a function as an argument or as a return value.

A function that takes another function as input is sometimes called a ``higher order'' function. Here is a simple higher order function:

  apply :: (Float -> Int) -> Float -> Int
  apply f y = (f y)

Thus, apply takes two arguments and applies its first argument to its second argument. Notice that the types of f and y have to be compatible to permit this.


next up previous contents
Next: Polymorphism Up: Functional programming in Haskell Previous: Defining functions in Haskell   Contents
Madhavan Mukund 2004-04-29