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.

Appendix B: Standard prelude

Appendix B: Standard prelude

pp. 280-297

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 appendix we present some of the most commonly used definitions from the Haskell standard prelude. For expository purposes, a number of the definitions are presented in simplified form. The full version of the prelude is available from the Haskell home page, http://www.haskell.org.

Basic classes

Equality types:

class Eq a where

(==), (/=) :: a -> a -> Bool

x /= y = not (x == y)

Ordered types:

class Eq a => Ord a where

(<), (<=), (>), (>=) :: a -> a -> Bool

min, max :: a -> a -> a

min x y | x <= y = x

| otherwise = y

max x y | x <= y = y

| otherwise = x

Showable types:

class Show a where

show :: a -> String

Readable types:

class Read a where

read :: String -> a

Numeric types:

class Num a where

(+), (-), (*) :: a -> a -> a

negate, abs, signum :: a -> a

Integral types:

class Num a => Integral a where

div, mod :: a -> a -> a

Fractional types:

class Num a => Fractional a where

(/) :: a -> a -> a

recip :: a -> a

recip n = 1/n

Booleans

Type declaration:

data Bool = False | True

deriving (Eq, Ord, Show, Read)

Logical conjunction:

(&&) :: Bool -> Bool -> Bool

False && _ = False

True && b = b

Logical disjunction:

(||) :: Bool -> Bool -> Bool

False || b = b

True || _ = True

Logical negation:

not :: Bool -> Bool

not False = True

not True = False

Guard that always succeeds:

otherwise :: Bool

otherwise = True

Characters

Type declaration:

data Char = …

deriving (Eq, Ord, Show, Read)

The definitions below are provided in the library Data.Char, which can be loaded by entering the following in GHCi or at the start of a script:

import Data.Char

Decide if a character is a lower-case letter:

isLower :: Char -> Bool

isLower c = c >= ’a’ && c <= ’z’

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