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:
\
and ->
: for
example, \ x -> x
is the identity function..
is function composition.$
is just function application, that is
f $ x
= f x
. It is right
associative and has low precedence, so it can be used to avoid
nested parentheses. We often write expressions like
instead off $ g $ h $ \ x -> x + 1
f (g (h (\x -> x + 1)))
instead off x `ap` y
ap (f x) y
(*)
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
.
(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
.Either
for
disjoint unions, defined as
and the typedata Either a b = Left a | Right b
Maybe
for optional values, defined as
data Maybe a = Nothing | Just a
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 as
main = print "Hello, world!"
To allows boolean values to be tested for equality with theclass Eq a where (==) :: 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 ainstance Eq Bool where True == 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 functiondata Bool = False | True deriving Eq
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 context. It means that
the type variable 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 theclass Functor f where map :: (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 classinstance Functor [] where map f [] = [] map f (x:xs) = f x : map f xs instance Functor Maybe where map f Nothing = Nothing map f (Just x) = Just (f x)
Monad
is defined as
and the specialclass Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b
do
syntax for monadic expressions,is defined to mean the same asdo x1 <- m1 x2 <- m2 ... mn
m1 >>= (\ x1 -> m2 >>= (\ x2 -> ... mn))