Eq and Ord
Eq is used for types that support equality testing. The functions its members implement are == and /=.
- ghci> 5 == 5
- True
- ghci> 5 /= 5
- False
- ghci> 'a' == 'a'
- True
- ghci> "Ho Ho" == "Ho Ho"
- True
- ghci> 3.432 == 3.432
- True
Ord is for types that have an ordering. Ord covers all the standard comparing functions such as >, <, >= and <=.
- ghci> :t (>)
- (>) :: (Ord a) => a -> a -> Bool
The compare function takes two Ord members of the same type and returns an ordering. Ordering is a type that can be GT, LT or EQ, meaning greater than, lesser than and equal, respectively.
- ghci> "Abrakadabra" < "Zebra"
- True
- ghci> "Abrakadabra" `compare` "Zebra"
- LT
- ghci> 5 >= 2
- True
- ghci> 5 `compare` 3
- GT
Show, Read and type annotations
Members of Show can be presented as strings. The show function takes a value whose type is a member of Show and presents it to us as a string.
- ghci> show 3
- "3"
- ghci> show 5.334
- "5.334"
- ghci> show True
- "True"
Read is sort of the opposite typeclass of Show. The read function takes a string and returns a type which is a member of Read.
- ghci> read "True" || False
- True
- ghci> read "8.2" + 3.8
- 12.0
- ghci> read "5" - 2
- 3
- ghci> read "[1,2,3,4]" ++ [3]
- [1,2,3,4,3]
Type signature of read
It returns a type that's part of Read but it has no way of knowing which type. That's why we can use explicit
- ghci> :t read
- read :: (Read a) => String -> a
Type annotations
Type annotations are a way of explicitly saying what the type of an expression should be. We do that by adding :: at the end of the expression and then specifying a type.
- ghci> read "5" :: Int
- 5
- ghci> read "5" :: Float
- 5.0
- ghci> (read "5" :: Float) * 4
- 20.0
- ghci> read "[1,2,3,4]" :: [Int]
- [1,2,3,4]
- ghci> read "(3, 'a')" :: (Int, Char)
- (3, 'a')
Enum, Bounded and Num
Enum members are sequentially ordered types — they can be enumerated. The main advantage of the Enum typeclass is that we can use its types in list ranges. They also have defined successors and predecesors, which you can get with the succ and pred functions.
Types in this class: (), Bool, Char, Ordering, Int, Integer, Float and Double.
- ghci> ['a'..'e']
- "abcde"
- ghci> [LT .. GT]
- [LT,EQ,GT]
- ghci> [3 .. 5]
- [3,4,5]
- ghci> succ 'B'
- 'C'
Bounded members have an upper and a lower bound. minBound and maxBound have a type of (Bounded a) => a. In a sense they are polymorphic constants.
- ghci> minBound :: Int
- -2147483648
- ghci> maxBound :: Char
- '\1114111'
- ghci> maxBound :: Bool
- True
- ghci> minBound :: Bool
- False
All tuples are also part of Bounded if the components are also in it.
- ghci> maxBound :: (Bool, Int, Char)
- (True,2147483647,'\1114111')
Num is a numeric typeclass. It appears that whole numbers are also polymorphic constants. They can act like any type that's a member of the Num typeclass.
- ghci> :t 20
- 20 :: (Num t) => t
- ghci> 20 :: Int
- 20
- ghci> 20 :: Integer
- 20
- ghci> 20 :: Float
- 20.0
- ghci> 20 :: Double
- 20.0
Type of *
- ghci> :t (*)
- (*) :: (Num a) => a -> a -> a
It takes two numbers of the same type and returns a number of that type. That's why (5 :: Int) * (6 :: Integer) will result in a type error whereas 5 * (6 :: Integer) will work just fine.
Integral includes only integral (whole) numbers. In this typeclass are Int and Integer.
Floating includes only floating point numbers, so Float and Double.
fromIntegral
It has a type declaration of fromIntegral :: (Num b, Integral a) => a -> b. It takes an integral number and turns it into a more general number. If we try to get a length of a list and then add it to 3.2, we'll get an error because we tried to add together an Int and a floating point number. So to get around this, we do fromIntegral (length [1,2,3,4]) + 3.2 and it all works out.
본 카테고리의 내용은 Learn You a Haskell for Great Good! 의 내용을 학습을 위해 요약한 것입니다.
'프로그래밍 Programming' 카테고리의 다른 글
Haskell_006 Guards (0) | 2018.03.17 |
---|---|
Haskell_005 Pattern matching (0) | 2018.03.17 |
Haskell Type, Typeclass, and Type variables (0) | 2018.03.16 |
Haskell_ 003 Typeclass (0) | 2018.03.15 |
Haskell_ 002 Type variables (0) | 2018.03.09 |