:t (*)
(*) :: Num a => a -> a -> a
square x = x * x
:t square
square :: Num a => a -> a
square
Num
sort :: Ord a => [a] -> [a]
<=
Num
class Num a where (+), (*), (-) :: a -> a -> a negate, abs, signum :: a -> a fromInteger :: Integer -> a
instance Num Int where -- ... instance Num Integer where -- ... instance Num Double where -- ... instance Num Float where -- ...
class Eq a where (==), (/=) :: a -> a -> Bool a/=b = not (a==b) -- default implementation a==b = not (a/=b) -- default implementation instance Eq Int where -- ... instance Eq Double where -- ... instance Eq Char where -- ... -- There are instances for almost all predefined types
Eq
(==) | Red | Yellow | Green |
---|---|---|---|
Red | True | False | False |
Yellow | False | True | False |
Green | False | False | True |
data TrafficLight = Red | Yellow | Green instance Eq TrafficLight where Red == Red = True Yellow == Yellow = True Green == Green = True _ == _ = False
deriving Eq
)
instance (Eq a,Eq b) => Eq (a,b) where (x1,y1) == (x2,y2) = x1==x2 && y1==y2
instance Eq a => Eq [a] where [] == [] = True x:xs == y:ys = x==y && xs==ys _ == _ = False
instance (Eq a,Eq b) => Eq (a,b) where -- ... instance Eq a => Eq [a] where -- ...
Eq Int
Eq Bool
Eq Char
Eq String
Eq [Int]
Eq [[Int]]
Eq [[[Int]]]
Eq (Int,Bool)
Eq [(Int,Bool)]
Eq ([Int],[Bool])
class Eq a => Ord a where (<), (<=), (>), (>=) :: a -> a -> Bool compare :: a -> a -> Ordering max, min :: a -> a -> a data Ordering = LT | EQ | GT
Ord
Eq
Ord
Eq
x==y
True
compare x y
EQ
class Enum a where
pred, succ :: a -> a
toEnum :: Int -> a
fromEnum :: a -> Int
enumFrom :: a -> [a]
enumFromTo :: a -> a -> [a]
-- ...
fromEnum
toEnum
Bool
Int
Integer
Float
Double
Enum
[1..5] == [1,2,3,4,5]
[1..] == [1,2,3,4,5,…]
['a'..'g'] == "abcdefg"
[0.5 .. 3] == [0.5,1.5,2.5,3.5]
Enum
[x..y] == enumFromTo x y
[x..] == enumFrom x
class Bounded a where minBound, maxBound :: a
Bool
Char
Int
Integer
enumAll :: (Bounded a, Enum a) => [a] enumAll = [minBound .. maxBound]
class Show a where
show :: a -> String
-- some more functions...
class Read a where
read :: String -> a
-- some more functions...
read
Eq
Ord
Show
Read
Enum
Bounded
data Suit = Spades | Hearts | Diamonds | Clubs deriving (Eq,Ord,Show,Read,Enum,Bounded)
data Suit = Spades | Hearts | Diamonds | Clubs deriving (Eq,Ord,Enum,Bounded) instance Show Suit where show Spades = "♠" show Hearts = "♥" show Diamonds = "♦" show Clubs = "♣"
data Rank = Numeric Int | Jack | Queen | King | Ace deriving (Eq,Ord) instance Show Rank where show (Numeric n) = show n show Jack = "J" show Queen = "Q" show King = "K" show Ace = "A"
data Card = Card {rank::Rank, suit::Suit} instance Show Card where show (Card r s) = show r++show s
data Hand = Empty | Add Card Hand instance Show Hand where show Empty = "." show (Add c h) = show c ++" "++show h
deriving Show
:
example_hand_2
Add (Card {rank = Ace, suit = Spades}) (Add (Card {rank = King, suit = Clubs}) Empty)
Show
example_hand_2
A♠ K♣
class Functor f where fmap :: (a->b) -> f a -> f b f <$> d = fmap f d
map
map :: (a->b) -> [a] -> [b] fmap :: Functor f => (a->b) -> f a -> f b
[]
Maybe
IO
deriving Functor
is
supported as an extension in GHC.
{-# LANGUAGE DeriveFunctor #-}
in the beginning of your module to enable this extension.
class Foldable t where
foldr :: (a->b->b) -> b -> t a -> b
-- ...
foldr
[]
Maybe
deriving Foldable
is
supported as an extension in GHC.
{-# LANGUAGE DeriveFoldable #-}
in the beginning of your module to enable this extension
class Small a where values :: [a]
instance Small Bool where values = [False,True] instance Small Suit where values = [maxBound .. minBound] instance Small Rank where values = (...) instance Small Card where values = [Card r s | s<-values, r<-values]
smallCheck :: Small a => (a->Bool) -> Bool smallCheck p = and [p x | x<-values]
smallCheck
quickCheck
class SmallCheck prop where smallCheck :: prop -> Bool instance SmallCheck Bool where smallCheck b = b instance (Small a,SmallCheck prop) => SmallCheck (a->prop) where smallCheck f = and [smallCheck (f x)|x<-values]
f :: String -> String f s = show (read s)
square :: Num a => a -> a square x = x * x
answer = 6*7
answer :: Num a => a
answer
answer
should have,
defaulting rules specific for the Num
answer :: Integer
(==) :: ''a -> ''a -> Bool
(*) :: Int -> Int -> Int (*) :: Double -> Double -> Double