There seems to be two implementations of the constant functor:
Const
in Control.Applicative
Constant
in Data.Functor.Constant
Why do both of them exist, and which one should I use?
A constant is a data type that substitutes a literal. Constants are useful in situations where a specific, unchanging value is to be used at various times during the software program A variable in a program can change its value during the course of execution of the program. A constant retains the same value throughout the program.
What is a Constant? These are values that do not change during experiments. For example, in an experiment where one wants to test how the growth of plants is affected by the amount of water, factors like type of soil, temperature, type of plant and sunlight all stay the same in the course of the experiment.
Difference between #define and const in C? #define is a preprocessor directive. Data defined by the #define macro definition are preprocessed, so that your entire code can use it. This can free up space and increase compilation times.
This means we can typecast, move addresses, and everything else you’d be able to do with a regular variable besides change the data itself, since the data assigned to that variable is constant. In general, const is a better option if we have a choice and it can successfully apply to the code.
They do the same thing indeed. As far as I'm aware of, most people use Const
because it is older (as Bakuriu pointed out), is in base
and has a shorter name. Case in point: lens
uses Const
. There was at least one discussion about unifying them in the libraries mailing list back in 2012, but it didn't went through, seemingly because of differences of opinion about the name and the most appropriate module for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With