:t head
:t fst
:t (==)
Note:
()
で囲んで使用する:t elem
5 == 5
5 /= 5
'a' == 'a'
"Ho Ho" == "Ho HO"
3.432 == 3.432
Eq
型クラスのメンバは==
と/=
を実装する:t (>)
>
, <
, >=
, <
などの一般的な比較関数を実装する"Abrakatabra" < "Zebra"
"Abrakatabra" `compare` "Zebra"
5 >= 2
5 `compare` 3
:t compare
show 3
show 5.334
show True
show "abc"
read "True" || False
read "8.2" + 3.8
read "5" - 2
read "[1,2,3,4]" ++ [3]
read "4"
:t read
read関数にて、戻り値の型が推論できない場合はエラーとなる
read "5" :: Int
read "5" :: Float
(read "5" :: Float) * 4
read "[1,2,3,4]" :: [Int]
read "(3, 'a')" :: (Int, Char)
['a' .. 'e']
[LT .. GT]
[3 .. 5]
succ 'B'
pred 'B'
Enum
型クラスのメンバ型は、連続的な順序を持つEnum
型クラスのメンバ型はlist range
やsucc
, pred
関数を使用可能()
, Bool
, Char
, Ordering
, Int
, Integer
, Float
, Double
等がEnum型クラスを実装している(minBound :: Int)
(maxBound :: Char)
(maxBound :: Bool)
(minBound :: Bool)
Bounded
型クラスは上限と下限の制限範囲を持つBounded
型クラスのメンバ型はminBound
とmaxBound
関数で制限範囲を確認できるNote:
:t minBound
minBound
やmaxBound
等の引数を取らない関数を多相定数という(maxBound :: (Bool, Int, Char))
:t 20
20 :: Int
20 :: Integer
20 :: Float
20 :: Double
Num
型クラスに所属しているメンバ型はShow
型クラスとEq
型クラスも所属していることになる[補足]
Eq
と Show
が Num
のスーパークラスとなっている。しかし、最近の GHC ではこれらは Num
のスーパークラスではなくなっているIntegral
も数の型クラス。Num
型クラスは全ての数字(実数と整数)Integral
型クラスは整数のみ。Int
とInteger
がメンバ型Floating
型クラスはFloat
とDouble
などの実数のみ。:t fromIntegral
fromIntegral (length [1, 2, 3, 4]) + 3.2
fromIntegral :: (Num b, Integral a) => a -> b
関数が便利