Under ¶
The best way to understand any given typeclass is to find its documentation online. This is usually easy with Google, or failing that, Hoogle.
Show¶
A class for converting a type into a String
, which can be displayed. String
is a legacy type, but Show
is widely used, and can be derived by Haskell:
Gotcha
Haskell won't show arbitrary functions, because they don't have a Show
instance. This makes sense, since it is impossible to show all the (infinite) input-output pairs of a function.
Eq¶
Ord¶
Num¶
Semigroup¶
Provides a method to combine two values: (<>) :: a -> a -> a
Any instance should define <>
such that it is associative (i.e. a <> (b <> c) = (a <> b) <> c
)
Text¶
> :set -XOverloadedStrings
> import Data.Text
> text = "hello"
> text2 = "world"
> text <> text2
"helloworld"
Monoid¶
Any¶
All¶
Sum¶
Product¶
Foldable¶
Functor¶
- The kind signature
f :: * -> *
requires the GHC2021 standard extensions.
Hint
Types which are instances of Functor
must have kind * -> *
.
So Int
or Bool
or Either Int Bool
or [Int]
**cannot be instances of Functor
, but Either Int
, or []
can. (See section on partial application of types.)
List¶
The definition of fmap
for []
is just map
> ls = [1 :: Int, 2, 3]
> :t ls
ls :: [Int]
> fmap even ls
[False,True,False]
> :t fmap even ls
fmap even ls :: [Bool]
Maybe¶
> maybeChar = Just 'a'
> :t maybeChar
maybeChar :: Maybe Char
> fmap (=='a') maybeChar
Just True
> :t fmap (=='a') maybeChar
fmap (=='a') maybeChar :: Maybe Bool
Reader r¶
State s¶
Fix f¶
Free f¶
Cont r¶
Applicative¶
Monad¶
Created: January 11, 2023