As a layman of Haskell
, I find being cautious of upper/lower case letters help me a lot to get understanding of functions:
(1) Function name doesn’t begin upper case: it can be lower case (e.g., sqrt
) or special characters (e.g., +
).
(2) Function has type. Let’s define a new function, incInt
:
incInt :: Integer -> Integer
incInt a = a + 1
The above identifies incInt
‘s type is “Integer -> Integer
“: Integer
is type in Haskell
, and types begin with upper case. Check another built-in functionsqrt
:
Prelude> :t sqrt
sqrt :: Floating a => a -> a
The “Floating a
” which is in the left of =>
is called type constraint: Floating
is typeclass, and its values are types which satisfy this typeclass (Floating
typeclass contains both Float
and Double
types); a
is a type variable which can be any type which belongs to Floating
typeclass.
Takeaway:
a) Function name can’t begin with upper case letters.
b) Type, typeclass, type variable occur in function type definition. Type and typeclass begin with upper case letters, and type variable need to begin with lower case letters.