We believe that the program examples will be readable without detailed knowledge of Haskell--familiarity with some functional language is hopefully sufficient. However, some recurring patterns are perhaps worth explaining:

- Haskell uses layout (indentation) rather than delimiting character to separate declarations, branches in case expressions, etc.
- Anonymous functions are written using
`\`

and`->`

: for example,`\ x -> x`

is the identity function. - The operator
`.`

is function composition. - The operator
`$`

is just function application, that is

=*f*$*x*

. It is right associative and has low precedence, so it can be used to avoid nested parentheses. We often write expressions like*f**x*

instead off $ g $ h $ \ x -> x + 1

f (g (h (\x -> x + 1)))

- An ordinary alfanumeric identifier can be used as an infix
operator by enclosing it in back quotes. We sometimes write, for
example,

instead off x `ap` y

ap (f x) y

- Infix operators are turned into functions that can be passed as
arguments by enclosing parentheses. For example,
`(*)`

is equal to`\x y -> x * y`

.Operators can be partially applied using sections, again using parentheses. For example,

`(2/)`

is the function`\x -> 2 / x`

, and`(/2)`

is the function`\x -> x / 2`

. - The Haskell syntax for tuples, lists and functions is chosen so
that a type and the values of the type look similar. For example,
the type of the tuple
`(3,False,"fudget")`

is`(Int,Bool,String)`

, the type of the list`[1,2,3]`

is`[Int]`

and the type of the function`\ x -> x`

is`a -> a`

. - In type expressions, names starting with lower case letters are type variables and names starting with upper case letters are type constructors.
- We often use the Haskell standard type
`Either`

for disjoint unions, defined as

and the type**data**Either a b = Left a | Right b`Maybe`

for optional values, defined as**data**Maybe a = Nothing | Just a - The result of a Haskell program is the value bound to the
identifier
`main`

. This value should be a representation of the effect (as discussed in Chapter 3) the execution of the program should have on the outside world. A program can be as simple asmain = print "Hello, world!"

To allows boolean values to be tested for equality with theclassEq awhere(==) :: a -> a -> Bool

` ==`

operator, an instance declaration like the
following can be used:

For some standard type classes, instance declarations can be generated automatically by adding ainstanceEq BoolwhereTrue == True = True False == False = True _ == _ = False

**deriving**

clause to the
type definition:

When an overloaded function is used in a new function definition, the overloading may be inherited by the new function. For example, consider the functiondataBool = False | TruederivingEq

`elem`

that checks if a value occurs
in a list, defined as

The type ofx `elem` [] = False x `elem` (y:ys) = x==y || x `elem` ys

`elem`

is written

where the partelem :: Eq a => a -> [a] -> Bool

`Eq a =>`

is called a `a`

is restricted to range over types that are
instance of the `Eq`

class.
In Haskell 1.3, the class system was generalised to allow classes of
type constructors [Jon93] instead of just classes of base types. Type
variables were extended to range over type constructors. This means
a that type scheme like `a Int`

is allowed. The type variable
`a`

can be instantiated to, for example, `Maybe`

and the
list type constructor, giving the types `Maybe Int`

and ` [Int]`

, respectively.

The well known function `map`

,

which is defined for lists in many functional languages, can now be generalised by introducing the classmap :: (a->b) -> [a] -> [b]

`Functor`

,

Instances for the lists and theclassFunctor fwheremap :: (a->b) -> f a -> f b

`Maybe`

type can be defined as

However, the introduction of constructor classes was motivated by the change to monadic I/O (see Section 41.1.3) and a convenient syntax for monadic programming. The classinstanceFunctor []wheremap f [] = [] map f (x:xs) = f x : map f xsinstanceFunctor Maybewheremap f Nothing = Nothing map f (Just x) = Just (f x)

`Monad`

is defined as

and the specialclassMonad mwherereturn :: a -> m a (>>=) :: m a -> (a -> m b) -> m b

**do**

syntax for monadic expressions,is defined to mean the same asdox<-_{1}m_{1}x<-_{2}m..._{2}m_{n}

m>>= (\_{1}x->_{1}m>>= (\_{2}x-> ..._{2}m))_{n}