I still have trouble getting my head around the difference between the ==
and =
in Haskell. I know the former has something to do with being an overloaded type and the latter 'gives the result' of the function but I just can't seem to get my head around it! Any help would be much appreciated.
Most notably, in Haskell, functions are not in the Eq typeclass (in general). Not any two functions can be compared for equality in Haskell.
The /= operator means "is not equal". It's supposed to be reminiscent of the mathematical "≠" symbol (i.e., an equals sign with a diagonal line through it).
(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.
The ++ operator is the list concatenation operator which takes two lists as operands and "combines" them into a single list.
=
is a special reserved symbol in Haskell meaning "is defined as". It is used to introduce definitions. That is, you use it to create new values and functions which may be referenced in the definitions of other values and functions.
==
is not a reserved symbol but just a run-of-the-mill function of type Eq a => a -> a -> Bool
. It happens to be declared in a type class (Eq
), but there's nothing extraordinary about it. You could hide the built-in declaration of ==
and redefine it to whatever you wanted. But normally it means "is equal to", and because it is part of a type class, you can define (overload) it to mean whatever you want "equality" to mean for your particular type.
For example:
data Foo = Foo Int
instance Eq Foo where
(Foo x) == (Foo y) = x == y
Note that I used =
to define ==
for Foo
!
A pithy way to think of the difference is that =
asserts equality at compile time, whereas ==
checks for equality at runtime.
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