## Introduction to Haskell applicative

In Haskell, we have applicative, which is normally present inside the control package of Haskell. If we want to refer to it then we may write Control.Applicative in general. As we know that Haskell is a functional programming language, and we have an applicative, which is also known as the applicative functor, this applicative is used to show an intermediate relation, or we can say structure between the moaned and functor. Applicative in Haskell was introduced in the year 2008; applicative in general is the container of data, and it mainly provides us two methods in general one is pure, and the other one is represented by using symbol ‘<*>’ this. When applicative was first released as the Haskell library function, but later on, it was introduced for other languages as well, such as Ocaml, Agada, Scala, etc. In the coming section of this tutorial, we will see the internal working and how we can use this while programming in Haskell, we will see a detailed implementation of this as well.

**Syntax**

As we discussed it ins the library in Haskell, which is used for computation, below we will see the default implementation of the Applicative module, which is present inside the control module in Haskell; we will see an official implementation for this class, which is given by the Haskell official documentation see below;

`class (Functor fun) => Applicative fun where`

pure :: a -> fun a

(<*>) :: fun (a -> b) -> fun a -> fun b

This is given by the Haskell official documentation on their website. As we can see, we have two methods here named pure and <*>; for both these methods, we can give the implementation because Applicative does not give any implementation for both the methods. In the coming section of the tutorial, we will see the internal working and how to include the Applicative class and their methods to use this while programming for beginners for better understanding.

### How to use applicative in Haskell?

As we already know, that applicative is also known as an applicative functor in Haskell, which is used to show a structure between the monad and functor. It basically provides us two methods and does provide their default implementation as well. These methods are named ‘pure’ and ‘<*>’. In Haskell, this applicative is considered as the parameterized type, with to methods available. Now in this section, we will closely look at the class definition of the applicative functor in Haskell. We will also see what are the parameter and what does it mean in actuality. Let’s get started with the class description; see below;

The class definition of Applicative in Haskell see below;

`class (Functor fun) => Applicative fun where`

pure :: a -> fun a

(<*>) :: fun (a -> b) -> fun a -> fun b

4.8 (11,959 ratings)

View Course

As you can see in the above lines of code, this is a class definition for Applicative in Haskell, which is given by the Haskell official website. Let’s take a look at the understanding of each of the parameters and method available in detail; let gets started;

1) class: This keyword is issued to define the Applicative class, and this keyword is introduced as the constraint as well.

2) This basically means that if we try to create the constructor for class Applicative type, then i have to be in Functor first, then only it will create else no.

3) After this, it has defined the two methods, which are mainly named as pure and ‘<*>’. Let’s first see the basic definition for Pure methods given by Applicative type in Haskell see below;

a) Pure : pure :: a -> f a

As you can see in the above line of code, we are using the pure method here. It has the parameterized type f a here, and this we are a method of the applicative has the type f.

b) Now we will see the basic definition for the second method of Applicative, which is available named as ‘<*>’, let’s have a closer look at its definition see below;

`(<*>) :: f (a -> b) -> f a -> f b`

This is a basic method definition given by the Haskell official website, or you can find this on any tutorial which is written for Applicative in Haskell. This method also has an applicative type of f type like a pure method of Applicative.

4) So both these functions will take some value as the parameter and will produce the result as the output from the function. First, we will talk about the pure method of Applicative, which can take an argument which of any type there is no restriction for this, but it will always return us the value of type applicative functor in return. So short, it will take a value which of any type, and it will wrap the result inside the applicative function, so here it is again using a boxing analogy here.

5) Now, we will have a closer look at the second method of applicative, which is <*> this, this has the similar definition as fmap in Haskell, in which we take a functor function as the argument and applies this inside the function; also this method works he same want like fmap in Haskell.

So n this way, these two methods work in Applicative in Haskell now; in the coming section, we will see some more examples to start using these applicative methods inside the Haskell program efficiently for beginners for more understanding and clarity.

### Examples

1) In this example, we are using the methods from the applicative in Haskell, which we are using to perform computation. This is a sample example for beginners to understand and start using it while programming in Haskell.

**Example:**

`main = do`

print("Demo to show applicative in Haskell !!")

print("Using its methods for computation purpose")

let result1 = pure (+100) <*> Just 300

let result2 = pure (*10) <*> Just 50

let result3 = pure (/2) <*> Just 50

let result4 = pure (+3) <*> Just 350

let result5 = pure (+45) <*> Just 60

let result6 = pure (*2) <*> Just 90

let result7 = pure (/5) <*> Just 1000

print("Printing the result after computation !!")

print(result1)

print(result2)

print(result3)

print(result4)

print(result5)

print(result6)

print(result7)

**Output:**

### Conclusion

By using this, we can perform computation; we have two methods available inside this Applicative, but this is a library inside the Haskell functional programming language; also, it is used to show structure between monad and factor in Haskell. This method works the same as fmap in Haskell.

### Recommended Articles

This is a guide to Haskell applicative. Here we discuss the internal working and how we can use this while programming in Haskell. You may also have a look at the following articles to learn more –