beuke.org

A personal blog about computer science topics.

Pronounceable Haskell Operators
Pronounceable Names for Common Haskell Operators
Posted on Aug 14 2018 ~ 2 min read
#haskell  #functional programming  #operators 

(Just a) small collection of Haskell operators with some funny names for them. Haskell tutorials and discussions can sometimes be very dry, so lets “Put the fun back into computing” (distrowatch).


TIE fighter <*>


Package Module Description
base Control.Applicative Sequential application.

Definition:
(<*>) :: f (a -> b) -> f a -> f b
(<*>) = liftA2 id

Example:

> [(*2)] <*> [1,2,3]
[2,4,6]

Other names: ap, apply



Wedge-shaped Imperial Star Destroyer <|>


Package Module Description
base Control.Applicative Associative binary operation.

Definition:
(<|>) :: f a -> f a -> f a
(<|>) = (++)

Example:

> [1,2,3] <|> [4,5]
[1,2,3,4,5]

Other names: or, alternative



TIE bomber <**>


Package Module Description
base Control.Applicative A variant of <*> with arguments reversed.

Definition:
(<**>) :: f a -> f (a -> b) -> f b
(<**>) = liftA2 (\a f -> f a)

Example:

> [1,2,3] <**> [(*2)]
[2,4,6]



Right fish >=>

Package Module Description
base Control.Monad Left-to-right Kleisli composition of monads.

Definition:
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
f >=> g = \x -> f x >>= g

Example:

> (pure . (+)) >=> (pure . (*)) $ 2
12



Left fish

Package Module Description
base Control.Monad (>=>), with arguments flipped.

(<=<) :: Monad m=> (b -> m c) -> (a -> m b) -> (a -> m c)
(<=<)=flip (>=>)

Example:

> (pure . (+)) <=< (pure . (*)) $ 2
8



Right pipe >>>

Package Module Description
base Control.Category Left-to-right composition.

(>>>) :: cat a b -> cat b c -> cat a c
f >>> g = g . f

Example:

> ((*2) >>> (+3)) $ 2
7



Left pipe .

Package Module Description
base Prelude Right-to-left composition.

Definition:
(.) :: (b -> c) -> (a -> b) -> a -> c
(.) f g = \x -> f (g x)

Example:

> ((*2) . (+3)) $ 2
10



Operators and shenanigans without funny names, yet

Notation Signature Description
>>= (>>=) :: Monad m => m a -> (a -> m b) -> m b bind
>> (>>) :: Monad m => m a -> m b -> m b then
-> to
<-< /td> bind
<$> (<$>) :: Functor f => (a -> b) -> f a -> f b fmap
<$< /td> (<$) :: Functor f=> a -> f b -> f a map-replace by
!! (!!) :: [a] -> Int -> a index
! strict
++ (++) :: [a] -> [a] -> [a] concat
[] ([]) :: [a] empty list
: (😃 :: a -> [a] -> [a] cons
:: of type
| lambda
@ as
~ lazy