:t (*)
(*) :: Num a => a -> a -> a
square x = x * x
:t square
square :: Num a => a -> a
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Â
class Enum a where
pred, succ :: a -> a
toEnum :: Int -> a
fromEnum :: a -> Int
enumFrom :: a -> [a]
enumFromTo :: a -> a -> [a]
-- ...
BoolÂ
IntÂ
IntegerÂ
FloatÂ
DoubleÂ
EnumÂ
EnumÂ
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...
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]
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
should have,
defaulting rules specific for the ÂNumÂ
(==) :: ''a -> ''a -> Bool
(*) :: Int -> Int -> Int (*) :: Double -> Double -> Double