Types#
:t value
You can check the type of data. Haskell is a strongly typed language.
The same applies to functions.
For example:
fun :: [Char] -> [Char]
For the function "fun", the parameter must be a character array, which is equivalent to a String.
Type Classes#
Type classes are used to define a common interface.
They provide a set of common characteristics for different types.
Similar to interfaces:
class typeclassname a where
funtionname :: a -> a -> returntype
This can be compared to Java:
interface<T,R> name{
R funtion(T t,T t);
}
Then implement instances of the type class:
instance typeclassname type where
funtionname arg arg = return
For example:
class EqualClass a where
equalFun :: a->a->Bool
instance EqualClass Bool where
equalFun True True = True
equalFun True False = False
equalFun False False = True
equalFun _ _ = False
--XNOR
Comparable to Java:
class interfaceImpl<Bool,Bool> implement interface{
Bool funtion(Bool l, Bool r){
retutn !(l^r);
//XNOR
}
}
Therefore, type classes are more like interfaces that can implement different behaviors for different classes.