갈루아의 반서재

Eq and Ord



Eq is used for types that support equality testing. The functions its members implement are == and /=

  1. ghci> 5 == 5  
  2. True  
  3. ghci> 5 /= 5  
  4. False  
  5. ghci> 'a' == 'a'  
  6. True  
  7. ghci> "Ho Ho" == "Ho Ho"  
  8. True  
  9. ghci> 3.432 == 3.432  
  10. True  

Ord is for types that have an ordering. Ord covers all the standard comparing functions such as ><>= and <=

  1. ghci> :t (>)  
  2. (>) :: (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 GTLT or EQ, meaning greater thanlesser than and equal, respectively.

  1. ghci> "Abrakadabra" < "Zebra"  
  2. True  
  3. ghci> "Abrakadabra" `compare` "Zebra"  
  4. LT  
  5. ghci> 5 >= 2  
  6. True  
  7. ghci> 5 `compare` 3  
  8. 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.

  1. ghci> show 3  
  2. "3"  
  3. ghci> show 5.334  
  4. "5.334"  
  5. ghci> show True  
  6. "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.

  1. ghci> read "True" || False  
  2. True  
  3. ghci> read "8.2" + 3.8  
  4. 12.0  
  5. ghci> read "5" - 2  
  6. 3  
  7. ghci> read "[1,2,3,4]" ++ [3]  
  8. [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

  1. ghci> :t read  
  2. 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.

  1. ghci> read "5" :: Int  
  2. 5  
  3. ghci> read "5" :: Float  
  4. 5.0  
  5. ghci> (read "5" :: Float) * 4  
  6. 20.0  
  7. ghci> read "[1,2,3,4]" :: [Int]  
  8. [1,2,3,4]  
  9. ghci> read "(3, 'a')" :: (IntChar)  
  10. (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: ()BoolCharOrderingIntIntegerFloat and Double.

  1. ghci> ['a'..'e']  
  2. "abcde"  
  3. ghci> [LT .. GT]  
  4. [LT,EQ,GT]  
  5. ghci> [3 .. 5]  
  6. [3,4,5]  
  7. ghci> succ 'B'  
  8. '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. 

  1. ghci> minBound :: Int  
  2. -2147483648  
  3. ghci> maxBound :: Char  
  4. '\1114111'  
  5. ghci> maxBound :: Bool  
  6. True  
  7. ghci> minBound :: Bool  
  8. False  

All tuples are also part of Bounded if the components are also in it.

  1. ghci> maxBound :: (BoolIntChar)  
  2. (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.

  1. ghci> :t 20  
  2. 20 :: (Num t) => t  
  1. ghci> 20 :: Int  
  2. 20  
  3. ghci> 20 :: Integer  
  4. 20  
  5. ghci> 20 :: Float  
  6. 20.0  
  7. ghci> 20 :: Double  
  8. 20.0  

Type of *

  1. ghci> :t (*)  
  2. (*) :: (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