In Haskell, operators are an essential part of the language that make it both powerful and expressive. Whether you’re working with basic arithmetic, manipulating lists, or chaining functions, Haskell’s rich set of operators provides flexible ways to interact with different data types. In this guide, we’ll explore the major operators available in Haskell, grouped by category for easier navigation.

In Haskell, to create an operator you must use the following “operator symbols”:

! # $ % * + . / < = > ? \ ^ | : – ~

Conceptually, there is no difference between an operator and a function, and you can use backticks or parens to make one work like the other.

### 1. Arithmetic Operators

Haskell supports a set of common arithmetic operators for performing calculations on numerical types. Here are the main operators:

– Addition`+`

– Subtraction**-**– Multiplication`*`

– Division (for**/**`Fractional`

types)

– Power (for**^**`Integral`

exponents)

– Power (for**^^**`Fractional`

exponents)

– Power (for******`Floating`

types)

#### Arithmetic Functions

– Integer division**div**

– Modulus (remainder after division)**mod**

– Integer quotient**quot**

– Integer remainder**rem**

– Absolute value**abs**

– Sign of a number (**signum**`-1`

,`0`

, or`1`

)

– Square root (for floating-point numbers)**sqrt**

– Takes two values that can be compared**min**

– Is similar to**max**`min`

, but it returns the larger of two comparable values.

– Takes a value and returns its “successor” (the next value)**succ**

– The opposite of**pred**`succ`

, returning the “predecessor” (previous value) of a given value.

These operators make it easy to perform basic mathematical operations, whether you’re working with integers, floating-point numbers, or fractional types.

### 2. Comparison Operators

Comparison operators allow you to compare values and make decisions based on the relationships between them. Haskell provides a full range of comparison operators:

– Equal to**==**

– Not equal to**/=**

– Less than**<**

– Greater than**>**

– Less than or equal to**<=**

– Greater than or equal to**>=**

Comparison operators are essential for controlling program flow, especially when working with conditionals.

### 3. Boolean (Logical) Operators

Haskell offers Boolean operators that are used for combining or negating Boolean values (`True`

or `False`

):

– Logical AND**&&**

– Logical OR**||**

#### Boolean Functions

– Logical NOT**not**

Boolean operators are typically used in expressions that require multiple conditions to be true or false.

### 4. Bitwise Operators (for `Bits`

Type Class)

Bitwise operators operate at the binary level and are mainly used for low-level data manipulation, commonly with integers:

– Bitwise AND**.&.**

– Bitwise OR**.|.**

#### Bitwise Functions

– Bitwise XOR (exclusive OR)**xor**

– Bitwise complement**complement**

– Shift bits (left or right depending on sign)**shift**

– Rotate bits (left or right depending on sign)**rotate**

– Set a specific bit to**setBit**`1`

– Clear a specific bit to**clearBit**`0`

– Toggle a specific bit**complementBit**

– Test if a specific bit is set**testBit**

– Create a number with a single bit set**bit**

– Get the number of bits in a type**bitSize**

– Check if a type is signed**isSigned**

– Shift bits to the left**shiftL**

– Shift bits to the right**shiftR**

– Rotate bits to the left**rotateL**

– Rotate bits to the right**rotateR**

Bitwise operations are less common in day-to-day Haskell programming but are useful for more specialized applications, such as working with binary data.

### 5. List Operators

Lists are fundamental in Haskell, and these operators make list manipulation straightforward:

– List concatenation, combines two lists into one.**++**

– List indexing (zero-based), retrieves an element at a specific position in the list.**!!**

#### List Functions

– Returns the first element of a list.`head`

– Returns the list without its first element.`tail`

– Returns the length of a list.`length`

– Checks if a list is empty.`null`

– Reverses a list.`reverse`

– Returns the first`take`

*n*elements of a list.– Takes elements from the beginning of a list while a condition is`takeWhile`

`True`

.– Drops the first`drop`

*n*elements of a list.– Drops elements from the beginning of a list while a condition is`dropWhile`

`True`

.– Returns the sum of elements in a list.`sum`

– Returns the product of elements in a list.`product`

– Checks if an element is in a list.`elem`

– Is the opposite of**notElem**`elem`

. It returns`True`

if the value is**not**in the list, otherwise it returns`False`

.

– Takes a list and returns everything except its last element.**init**

– Takes a list and cycles it into an infinite list. If you just try to display the result, it will go on forever so you have to slice it off somewhere.**cycle**

– Takes an element and produces an infinite list of just that element. It’s like cycling a list with only one element.**repeat**

– Takes two lists and then zips them together into one list by joining the matching elements into pairs.**zip**– Applies a function to corresponding elements of two lists and returns a new list.`zipWith`

– Transforms a list of pairs into a pair of lists.`unzip`

– Takes a list of items that can be sorted. Their type is an instance of the Ord typeclass. Quicksort orders them.**quicksort**– Reduces a list from the left by applying a function. Takes an accumulator, a function, and a list.`foldl`

– Reduces a list from the right by applying a function. Takes an accumulator, a function, and a list.`foldr`

– Like`scanl`

`foldl`

, but returns a list of successive reductions from the left.– Like`scanr`

`foldr`

, but returns a list of successive reductions from the right.– Splits a list into two parts: elements that satisfy a condition and the rest.`span`

– Similar to`break`

`span`

, but stops as soon as the condition is`True`

.– Applies a function to each element in a list.`map`

– Returns a list of elements that satisfy a predicate.`filter`

– Concatenates a list of lists into a single list.`concat`

– Maps a function over a list and concatenates the results.`concatMap`

– Finds the largest element in a list.`maximum`

– Finds the smallest element in a list.`minimum`

– Checks if all elements in a list are`and`

`True`

.– Checks if any element in a list is`or`

`True`

.– Checks if all elements satisfy a condition.`all`

– Checks if any element satisfies a condition.`any`

These operators simplify common list tasks, like joining lists or accessing elements by index.

### 6. Function Operators

Function operators in Haskell provide shortcuts for chaining functions together and managing function application:

– Function application (low precedence), allows you to omit parentheses in nested expressions.**$**

– Function composition, combines two functions so that the output of one function becomes the input of the next.**.**

These operators make it easier to write clean, readable function chains.

### 7. Monadic Operators

Monadic operators help work with values wrapped in monadic contexts, a common pattern in Haskell for handling side effects or chaining computations:

– Sequentially compose two actions, discarding the result of the first.**>>**

– Bind operator: passes the result of a monadic computation to the next function.**>>=**

– Reverse bind: passes a monadic value to a function in reverse order.**=<<**

– Left-to-right function composition in monads (Kleisli composition).**>=>**

– Right-to-left function composition in monads (Kleisli composition).**<=<**

Monadic operators are especially useful in structuring code that involves chaining actions, like I/O or state transformations.

### 8. Applicative and Functor Operators

Applicative and Functor operators allow for working with functions and values inside contexts, like `Maybe`

, `Either`

, or lists:

– Functor map (infix version of**<$>**`fmap`

), applies a function to the contents of a functor.

– Applicative apply, applies a function in a context to a value in a context.**<*>**

– Sequence actions, discarding the result of the second action.**<***

– Sequence actions, discarding the result of the first action.***>**

– Replace all elements in a Functor with the same value.**<$**

– Alternative choice (used with**<|>**`Alternative`

type class).

– Apply in reverse order.**<**>**

These operators come in handy for handling operations on values that are wrapped in specific contexts, keeping code concise and expressive.

### 9. Miscellaneous Operators

These operators don’t fit neatly into the previous categories but are often used in Haskell for various purposes:

– List indexing (zero-based), retrieves an element at a specific position in the list.**!!**

– Cons operator, adds an element to the front of a list.**:**

– List difference, removes elements from the first list that appear in the second list.**\\**

– Strict function application, forces evaluation of the argument before applying the function.**$!**

### Conclusion

Haskell’s operators are versatile tools that enable you to write concise, expressive, and powerful code. By understanding the purpose of each operator and the contexts in which they’re most useful, you can make the most of Haskell’s capabilities. This comprehensive guide should serve as a quick reference to navigate Haskell’s operators and understand their applications.

## Leave a Reply