Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 4: Defining functions

Chapter 4: Defining functions

pp. 38-46

Authors

, University of Nottingham
Resources available Unlock the full potential of this textbook with additional resources. There are Instructor restricted resources available for this textbook. Explore resources
  • Add bookmark
  • Cite
  • Share

Summary

In this chapter we introduce a range of mechanisms for defining functions in Haskell. We start with conditional expressions and guarded equations, then introduce the simple but powerful idea of pattern matching, and conclude with the concepts of lambda expressions and operator sections.

New from old

Perhaps the most straightforward way to define new functions is simply by combining one or more existing functions. For example, a few library functions that can be defined in this way are shown below.

• Decide if an integer is even:

even :: Integral a => a -> Bool

even n = n ‘mod’ 2 == 0

• Split a list at the nth element:

splitAt :: Int -> [a] -> ([a],[a])

splitAt n xs = (take n xs, drop n xs)

• Reciprocation:

recip :: Fractional a => a -> a

recip n = 1/n

Note the use of the class constraints in the types for even and recip above, which make precise the idea that these functions can be applied to numbers of any integral and fractional types, respectively.

Conditional expressions

Haskell provides a range of different ways to define functions that choose between a number of possible results. The simplest are conditional expressions, which use a logical expression called a condition to choose between two results of the same type. If the condition is True, then the first result is chosen, and if it is False, then the second result is chosen. For example, the library function abs that returns the absolute value of an integer can be defined as follows:

abs :: Int -> Int

abs n = if n ≥ 0 then n else –n

Conditional expressions may be nested, in the sense that they can contain other conditional expressions. For example, the library function signum that returns the sign of an integer can be defined as follows:

signum :: Int -> Int

signum n = if n < 0 then -1 else

if n == 0 then 0 else 1

Note that unlike in some programming languages, conditional expressions in Haskell must always have an else branch, which avoids the well-known dangling else problem.

About the book

Access options

Review the options below to login to check your access.

Purchase options

eTextbook
US$47.00
Paperback
US$47.00

Have an access code?

To redeem an access code, please log in with your personal login.

If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.

Also available to purchase from these educational ebook suppliers