List of the Basic Functions in Haskell

Haskell is a functional programming language known for its high-level abstractions, immutability, and strong type system. While its elegance and power can seem intimidating to beginners, it comes with a rich set of basic functions that make it easier to get started with. Whether you’re performing simple arithmetic, manipulating lists, or exploring higher-order functions, understanding these foundational functions is essential for mastering Haskell.

In this article, we’ll walk through a list of some of the most commonly used basic functions in Haskell. From mathematical operations to list handling, tuples, and I/O functions, these tools will help you become more proficient and comfortable in the Haskell environment.

Type Signatures in Haskell

Before diving into basic functions, it helps to understand Type Signatures & Type Classes. In Haskell, type signatures define the types of inputs and outputs for functions. They provide a way to specify what types a function operates on and what type it will return. Type signatures are optional in most cases because Haskell can infer types, but adding them explicitly can help improve code clarity and prevent certain errors.

A type signature is usually written above the function definition and follows this structure:

functionName :: inputType -> outputType

For example, the type signature of a function that adds two integers is:

add :: Int -> Int -> Int

This tells us that add takes two arguments of type Int and returns a result of type Int.

Basic Structure of a Type Signature:

  • ->: Separates input types and output types. In a -> b, a is the input type, and b is the return type.
  • Parentheses: Used when a function takes another function as an argument or for clarity in complex types.

Type Signatures (Explain Like I’m 5)

Think of a type signature as a label that tells you what kind of input a function expects and what kind of result it will give you. It’s like a recipe: if you follow the instructions and use the right ingredients, you’ll get the right dish!

For example:

add :: Int -> Int -> Int

This type signature says:

  • add is a function that takes two numbers (Int and Int) and gives back another number (also an Int).

So, the type signature is like the ingredients and result list in a recipe: “Give me two integers, and I’ll give you one integer back!”

Type Classes in Haskell

A type class in Haskell is a way of defining a set of behaviors (functions) that different types can implement. Type classes provide a form of polymorphism, allowing a function to work with different types that implement the same set of operations.

For example, the Eq type class defines types that support equality comparison. If a type is an instance of Eq, you can use == or /= to compare values of that type.

Common Type Classes:

Eq: Provides equality (==) and inequality (/=) comparison.

(==) :: Eq a => a -> a -> Bool
(/=) :: Eq a => a -> a -> Bool

Ord: For types that have an ordering. Provides operators like >, <, >=, and <=.

(>) :: Ord a => a -> a -> Bool
(<) :: Ord a => a -> a -> Bool

Show: For types that can be converted to strings.

show :: Show a => a -> String

Num: For numeric types that support operations like +, -, and *.

(+) :: Num a => a -> a -> a
(*) :: Num a => a -> a -> a

Type Classes (Explain Like I’m 5)

A type class is like a set of rules or skills that certain types (things like numbers or text) can follow or have. Think of it like being part of a club: if something (a number or a list) follows the rules of the club, it gets to use certain abilities.

For example:

  • Eq Club: Members of this club can be compared for equality (==). If a type is in the Eq club, you can ask, “Are these two things equal?”
  • Ord Club: Members of this club know how to be ordered, so you can say, “Is this bigger or smaller than that?”
  • Num Club: This club is for numbers. If a type is in the Num club, you can add, subtract, or multiply it.

Here’s an example:

(==) :: Eq a => a -> a -> Bool

This means that the function == works for any type a, as long as a is part of the Eq club (it can be compared for equality). If a type isn’t in the club, you can’t use == on it.

1. Mathematical Functions

Haskell offers a range of mathematical functions that allow you to perform arithmetic and work with numbers efficiently. You’ll use these frequently, whether it’s simple addition or more advanced operations like exponentiation.

Haskell provides many built-in functions to work with numbers:

+: Addition.

2 + 3  -- Result: 5

-: Subtraction.

5 - 2  -- Result: 3

*: Multiplication.

4 * 3  -- Result: 12

/: Division (for floating-point numbers).

10 / 2  -- Result: 5.0

div: Integer division.

10 `div` 3  -- Result: 3

mod: Modulus (remainder after division).

10 `mod` 3  -- Result: 1

abs: Absolute value.

abs (-5)  -- Result: 5

signum: Sign of a number (-1, 0, or 1).

In Haskell, the signum function is used to determine the sign of a number. It returns an indicator of whether the number is negative, zero, or positive. The function is defined in the Num type class, so it works with any numeric type that belongs to this class (like Int, Integer, Float, Double, etc.).

Behavior of signum
  • If the number is negative, signum returns -1.
  • If the number is zero, signum returns 0.
  • If the number is positive, signum returns 1.
signum (-5)  -- Result: -1
signum 0     -- Result: 0
signum 5     -- Result: 1

^: Exponentiation (for integers).

2 ^ 3  -- Result: 8

sqrt: Square root (for floating-point numbers).

sqrt 16  -- Result: 4.0

min: Takes two values that can be compared (types that belong to the Ord type class) and returns the smaller of the two.

min 3 5     -- Result: 3
min 'a' 'z' -- Result: 'a'

max: Is similar to min, but it returns the larger of two comparable values.

max 3 5     -- Result: 5
max 'a' 'z' -- Result: 'z'

succ: Takes a value and returns its “successor” (the next value). It works with any type that is an instance of the Enum type class, such as numbers or characters.

succ 5     -- Result: 6
succ 'a'   -- Result: 'b'

pred: Is the opposite of succ, returning the “predecessor” (previous value) of a given value.

pred 5     -- Result: 4
pred 'b'   -- Result: 'a'

2. Boolean Functions

When working with conditions and logic, Haskell’s boolean functions are essential. Whether you’re checking equality, combining conditions, or negating expressions, these functions simplify the logic in your programs.

Functions that operate on or return Bool values (True or False):

&&: Logical AND.

True && False  -- Result: False

||: Logical OR.

True || False  -- Result: True

not: Logical NOT.

not True  -- Result: False

==: Equality check.

5 == 5  -- Result: True

/=: Inequality check.

5 /= 3  -- Result: True

>: Greater than.

5 > 3  -- Result: True

<: Less than.

3 < 5  -- Result: True

>=: Greater than or equal to.

5 >= 5  -- Result: True

<=: Less than or equal to.

3 <= 5  -- Result: True

3. List Functions

Lists are one of the most common data structures in Haskell. The language provides a robust set of functions for manipulating lists, such as getting the head or tail of a list, reversing it, or filtering elements based on a condition.

Haskell provides many functions for working with lists, one of the most commonly used data structures:

head: Returns the first element of a list.

head [1, 2, 3]  -- Result: 1

tail: Returns the list without its first element.

tail [1, 2, 3]  -- Result: [2, 3]

length: Returns the length of a list.

length [1, 2, 3]  -- Result: 3

null: Checks if a list is empty.

null []       -- Result: True
null [1, 2]  -- Result: False

reverse: Reverses a list.

reverse [1, 2, 3]  -- Result: [3, 2, 1]

take: Returns the first n elements of a list.

take 2 [1, 2, 3, 4]  -- Result: [1, 2]

drop: Drops the first n elements of a list.

drop 2 [1, 2, 3, 4]  -- Result: [3, 4]

sum: Returns the sum of elements in a list.

sum [1, 2, 3]  -- Result: 6

product: Returns the product of elements in a list.

product [1, 2, 3]  -- Result: 6

elem: Checks if an element is in a list.

elem 3 [1, 2, 3, 4]  -- Result: True
elem 5 [1, 2, 3, 4]  -- Result: False

notElem: is the opposite of elem. It returns True if the value is not in the list, otherwise it returns False.

notElem 3 [1, 2, 4]  -- Result: True

4. Tuple Functions

Haskell’s tuples allow you to group values of different types together. Functions like fst and snd help you extract the individual components of a tuple.

Functions that operate on pairs of values (tuples):

fst: Returns the first element of a pair.

fst (1, 2)  -- Result: 1

snd: Returns the second element of a pair.

snd (1, 2)  -- Result: 2

5. Higher-Order Functions

Higher-order functions, which can take other functions as arguments or return them as results, are a hallmark of functional programming. Haskell provides powerful higher-order functions like map, filter, and foldl to process lists in a functional style.

Functions that take other functions as arguments or return them:

map: Applies a function to each element of a list.

map (+1) [1, 2, 3]  -- Result: [2, 3, 4]

filter: Filters elements of a list based on a predicate.

filter (>2) [1, 2, 3, 4]  -- Result: [3, 4]

foldl: Reduces a list from the left using a binary function.

foldl (+) 0 [1, 2, 3]  -- Result: 6

foldr: Reduces a list from the right using a binary function.

foldr (+) 0 [1, 2, 3]  -- Result: 6

6. Input/Output Functions

Although Haskell emphasizes pure functions, it also supports basic input and output (I/O) operations. These functions allow you to interact with users or external systems by reading inputs or printing results.

Functions for basic I/O in Haskell:

print: Outputs a value to the console.

print 5  -- Prints: 5

putStrLn: Outputs a string followed by a newline.

putStrLn "Hello, World!"  -- Prints: Hello, World!

getLine: Reads a line of input from the user.

name <- getLine  -- Reads user input and stores it in `name`

7. Function Composition

Function composition is a key concept in Haskell, allowing you to combine multiple functions into a single function. This results in cleaner, more expressive code by chaining operations together.

You can use . to compose functions. This means combining multiple functions into one.

.: Function composition operator.

(map (*2) . filter (>3)) [1, 2, 3, 4, 5]  -- Result: [8, 10]

Conclusion

Understanding the basic functions of Haskell is the first step toward harnessing the full power of this functional programming language. With a solid grasp of mathematical operations, list manipulations, boolean logic, and higher-order functions, you can write more efficient, elegant, and maintainable code. The power of Haskell lies in its expressiveness and ability to abstract complex ideas into simple functions, making it a unique and rewarding language to learn.

By mastering these basic functions, you’ll be well on your way to exploring more advanced topics in Haskell, such as type classes, monads, and pure functional programming principles. So dive into these fundamentals, experiment with them in the GHCI, and see how they can simplify and improve your approach to problem-solving!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *