25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Operators<br />

In <strong>Haskell</strong>, infix operators are simply functions, and quite often they are used in place of alphanumerical<br />

names when the functions involved combine in common ways and are subject to algebraic laws.<br />

infixl 6 +<br />

infixl 6 -<br />

infixl 7 /<br />

infixl 7 *<br />

infixr 5 ++<br />

infixr 9 .<br />

Operators can be written in section form:<br />

(x+) = \y -> x+y<br />

(+y) = \x -> x+y<br />

(+) = \x y -> x+y<br />

Any binary function can be written in infix form by surrounding the name in backticks.<br />

(+1) ‘fmap‘ [1,2,3] -- [2,3,4]<br />

Monads<br />

A monad is a typeclass with two functions: bind and return.<br />

class Monad m where<br />

bind :: m a -> (a -> m b) -> m b<br />

return :: a -> m a<br />

e bind function is usually written as an infix operator.<br />

infixl 1 >>=<br />

class Monad m where<br />

(>>=) :: m a -> (a -> m b) -> m b<br />

return :: a -> m a<br />

is defines the structure, but the monad itself also requires three laws that all monad instances must<br />

satisfy.<br />

Law 1<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!