LoginSignup
20
8

More than 5 years have passed since last update.

All about Prelude #Haskell

Last updated at Posted at 2017-08-10

はじめに

HaskellのPreludeの全関数と型と解説を書いていきます(全254種).
GHC8.2.1を用いています.

GHCiとPrelude (Hackage), GHCのソースコードを参考に書いていきます.

また一部ソースコードの表記を見易さの為にインデントを変えている点がありますが, 意味は全く変えていません.

間違いやより良い表現等がありましたら, コメントか編集リクエストで教えて頂けると幸いです.

順次追加していきます.

標準の型とクラス, およびそれらに関連する関数

基本データ型

Bool型

所謂真偽値型です.
以下のように定義されています.

data Bool :: *
data Bool =  False
          |  True

値コンストラクタはFalseとTrueの2つがあります.
Falseが偽, Trueが真です.

Bool型の型クラス

Bool型は以下の型クラスのインスタンスになっています.

instance Eq Bool
instance Ord Bool
instance Show Bool
instance Read Bool
instance Enum Bool
instance Bounded Bool
Bool型に関連する関数

Bool型に関連する関数は以下のものがあります.

(&&)
(&&) :: Bool -> Bool -> Bool
infixr 3 &&

AND(論理積)演算を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> True && True
True

> True && False
False

> False && False
False
(||)
(||) :: Bool -> Bool -> Bool
infixr 2 ||

OR(論理和)演算を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> True || True
True

> True || False
True

> False || False
False
not
not :: Bool -> Bool

NOT(否定)演算を行った結果を返します.

実行例.hs
> not True
False

> not False
True
otherwise
otherwise :: Bool

otherwiseTrueと同値(エイリアス?)です.
以下のように定義されています.

otherwise = True

以下のように, ガードの最下部に用いる事で, 全ての条件に当てはまらなかった場合の条件として、CやJavaのcaseのdefaultと似たような形で使います.

f x | x < 0     = "foo"
    | otherwise = "bar"

最下部にotherwiseを書くことによって部分関数にならず, 全てのパターンに当てはまる関数にする事ができます.

Maybe a型

値があるか(Just a), 値が無い(Nothing)という状態を持った値です.
以下のように定義されています.

data Maybe a =  Nothing
             |  Just a
Maybe型の型クラス

Maybe型は以下の型クラスのインスタンスになっています.

instance Applicative Maybe
instance Eq a => Eq (Maybe a)
instance Functor Maybe
instance Monad Maybe
instance MonadPlus Maybe
instance Monoid a => Monoid (Maybe a)
instance Ord a => Ord (Maybe a)
instance Show a => Show (Maybe a)
instance Read a => Read (Maybe a)
instance Foldable Maybe
instance Traversable Maybe
Maybe型に関連する関数
maybe
maybe :: b -> (a -> b) -> Maybe a -> b

デフォルト値, 1引数関数, Maybeの値を取り, Just aならば1引数関数に適用した結果を, Nothingならデフォルト値を返す関数.

Nothingに対して適切な値があるなら, Maybeを剥がす事ができる.

実行例.hs
> maybe 0 (+ 1) (Just 3)
4

> maybe 0 (+ 1) Nothing
0

Either a b型

Eitherは異なる2つの型abに対し, Left a, Right bという2種類の状態を持った値を持つ型です.
以下のように定義されています.

data Either a b =  Left  a
                |  Right b
Either型の型クラス

Either型は以下の型クラスのインスタンスになっています.

instance (Eq b, Eq a) => Eq (Either a b)
instance Monad (Either e)
instance Functor (Either a)
instance (Ord b, Ord a) => Ord (Either a b)
instance (Read b, Read a) => Read (Either a b)
instance (Show b, Show a) => Show (Either a b)
instance Applicative (Either e)
instance Foldable (Either a)
instance Traversable (Either a)
Either型に関連する関数
either
either :: (a -> c) -> (b -> c) -> Either a b -> c

型aを取り型cを返す1引数関数, 型bを取り型cを返す1引数関数, Either a bの値を取り, Left aならば型aを取る1引数関数に適用した結果を, Right bならば型bを取る1引数関数に適用した結果を返す関数.

返す型が同じであればEitherを剥がす事ができる.

実行例.hs
> either length (+ 1) $ Left "Hello"
5

> either length (+ 1) $ Right 1
2

Ordering型

Ordering型はOrd型クラスのメソッドcompareで使われます.
LT, EQ, GTはそれぞれLess than, Equal, Greater thanの意味です.

data Ordering :: *
data Ordering =  LT
              |  EQ
              |  GT
Ordering型の型クラス

Ordering型は以下の型クラスのインスタンスになっています.

instance Bounded Ordering
instance Enum Ordering
instance Eq Ordering
instance Ord Ordering
instance Read Ordering
instance Show Ordering
instance Monoid Ordering

Char型

Char型は文字を表します.
以下のように定義されています.

data Char :: *
data Char =  GHC.Types.C# GHC.Prim.Char#
Char型の型クラス

Char型は以下の型クラスのインスタンスになっています.

instance Bounded Char
instance Enum Char
instance Eq Char
instance Ord Char
instance Read Char
instance Show Char

String型

String型は文字列を表します.
以下のように, Char型のリストのエイリアスとして定義されています.

type String =  [Char]

タプル

複数の値の組です.
型は違う種類でも構いません.

fst
fst :: (a, b) -> a

要素数2のタプルを取り, タプルの1要素目を返す関数.

実行例.hs
> fst (1, "Hello")
1
snd
snd :: (a, b) -> b

要素数2のタプルを取り, タプルの2要素目を返す関数.

実行例.hs
> snd (1, "Hello")
"Hello"
curry
curry :: ((a, b) -> c) -> a -> b -> c

(a, b) -> cは非カリー化関数で, a -> b -> cはカリー化された関数.
curry関数は, 非カリー化関数を引数に取り, カリー化された関数にして返す関数.

実行例.hs
> :t curry fst
curry fst :: c -> b -> c

> (curry fst) 1 2
1
uncurry
uncurry :: (a -> b -> c) -> (a, b) -> c

(a, b) -> cは非カリー化関数で, a -> b -> cはカリー化された関数.
uncurry関数は, カリー化された関数を引数に取り, 非カリー化関数にして返す関数.

実行例.hs
> :t uncurry (+)
uncurry (+) :: Num c => (c, c) -> c

> (uncurry (+)) (1, 2)
3

基本型クラス

Eq型クラス

class Eq a where

メソッド

最小の定義
(==) | (/=)

(==)
(==) :: Eq a => a -> a -> Bool
infix 4 ==

等値(Equal)演算を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> "Hello" == "Hello"
True

> 1 == 2
False
(/=)
(/=) :: Eq a => a -> a -> Bool
infix 4 /=

不等(Not Equal)演算を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> "Hello" /= "Hello"
False

> 1 /= 2
True
属するインスタンス

Ord型クラス

class Eq a => Ord a where

メソッド

最小の定義
compare | (<=)

compare
compare :: Ord a => a -> a -> Ordering

2つの同じ型の値を取り, 比較を行った結果をOrdering型で返します.
LT, EQ, GTはそれぞれLess than, Equal, Greater thanの意味で, 第2引数に対して第1引数が"小さい"か, "等しい"か, "大きい"かを表します.

実行例.hs
> compare 1 2
LT

> compare 1 1
EQ

> compare 2 1
GT

> compare True False
GT
(<)
(<) :: Ord a => a -> a -> Bool
infix 4 <

小なり(Less than)比較を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> 1 < 2
True

> 2 < 1
False

> 2 < 2
False

> "a" < "b"
True
(<=)
(<=) :: Ord a => a -> a -> Bool
infix 4 <=

小なりイコール(Less than or Equal to)比較を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> 1 <= 2
True

> 2 <= 1
False

> 2 <= 2
True

> "a" <= "b"
True
(>)
(>) :: Ord a => a -> a -> Bool
infix 4 >

大なり(Greater than)比較を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> 1 > 2
False

> 2 > 1
True

> 2 > 2
False

> "a" > "b"
False
(>=)
(>=) :: Ord a => a -> a -> Bool
infix 4 >=

大なりイコール(Greater than or Equal to)比較を行った結果を返します.
記号のみで定義されているので中置記法が使えます.

実行例.hs
1 >= 2
False

> 2 >= 1
True

> 2 >= 2
True

> "a" >= "b"
False
max
max :: Ord a => a -> a -> a

2つの同じ型の引数を取り, 大きい方を返す関数.

実行例.hs
> max 1 2
2

> max 2 1
2

> max 2 2
2
min
min :: Ord a => a -> a -> a

2つの同じ型の引数を取り, 小さい方を返す関数.

実行例.hs
> min 1 2
1

> min 2 1
1

> min 2 2
2
属するインスタンス

Enum型クラス

class Enum a where

メソッド

最小の定義

toEnum, fromEnum

succ
succ :: a -> a

引数の値の次の値を返す関数です.

実行例.hs
> succ 1
2

> succ 'a'
'b'
pred
pred :: a -> a

引数の値の前の値を返す関数です.

実行例.hs
> pred 0
-1

> pred 'a'
'`'
toEnum
toEnum :: Int -> a
実行例.hs

fromEnum
fromEnum :: a -> Int
実行例.hs

enumFrom
enumFrom :: a -> [a]
実行例.hs

enumFromThen
enumFromThen :: a -> a -> [a]
実行例.hs

enumFromTo
enumFromTo :: a -> a -> [a]
実行例.hs

enumFromThenTo
enumFromThenTo :: a -> a -> a -> [a]
実行例.hs

属するインスタンス

Bounded型クラス

class Bounded a where

メソッド

最小の定義

minBound, maxBound

minBound
minBound :: a
実行例.hs

maxBound
maxBound :: a
実行例.hs

属するインスタンス

数値

数値型

Int型

Integer型

Float型

Double型

Rational型

Word型

数値型クラス

Num型クラス

メソッド
(+)
(+) :: Num a => a -> a -> a
infixl 6 +

加算演算子です.
記号のみで定義されているので中置記法が使えます.

実行例.hs
> 1 + 1
2

> 1.0 + 1
2.0

> a = 1 :: Int
> 1.0 + a

<interactive> error:
     No instance for (Fractional Int) arising from the literal 1.0
     In the first argument of (+), namely 1.0
      In the expression: 1.0 + a
      In an equation for it: it = 1.0 + a
-- 1.0 + 1が正しく動作したのは, 型推論によって1がDouble型であると推論された為です.
-- 暗黙的な型変換が行われている訳ではありません.
(-)
(-) :: Num a => a -> a -> a
infixl 6 -

減算演算子です.
記号のみで定義されているので中置記法が使えます.

部分適用をしようとすると, 単項演算子のminus扱いをされるため期待する動作が得られません.
減算の部分適用をしたい場合は, 後述のsubtractを利用して下さい.

実行例.hs
> 1 - 1
0
(*)
(*) :: Num a => a -> a -> a
infixl 7 *

乗算演算子です.
2項演算子化されているため, 中置記法が使えます.

実行例.hs
> 1 * 1
1
negate
negate :: Num a => a -> a

引数の値の符号を反転させて返す関数です.

実行例.hs
> negate 1
-1

> negate (-1)
1
abs
abs :: Num a => a -> a

引数の値の絶対値を返す関数です.

実行例.hs
> abs 1
1

> abs (-1)
1
signum
signum :: Num a => a -> a

引数の値の符号を返します.

実行例.hs
> signum 1
1

> signum (-1)
-1

absと併用して, 以下の事が言えます.

abs x * signum x == x
属するインスタンス

Real型クラス

メソッド
toRational
toRational :: a -> Rational
実行例.hs

属するインスタンス

Integral型クラス

メソッド
quot
quot :: Integral a => a -> a -> a

整数の除算を行います.
整数で割り切れない場合は0の方に丸めます

実行例.hs
> quot 1 2
0 -- 0.5を0の方に丸めている. 商が正の値になる場合はdivとquotの結果に差はない

> quot 4 2
2

> quot (-5) 2
-2 -- 2.5を0の方に丸めている
rem
rem :: Integral a => a -> a -> a

整数の剰余演算を行います.
rem x y == x - (quot x y) * yとなるように求めます.

実行例.hs
> rem 1 2
1 -- quot 1 2は0なので, rem 1 2 == 1 - 0 * 2 == 1

> rem 4 2
0

> rem (-5) 2
-1 -- quot (-5) 2は(-2)なのでrem (-5) 2 == (-5) - (-2) * 2 == (-1)
div
div :: Integral a => a -> a -> a

整数の除算を行います.
整数で割り切れない場合は, 負の無限大の方に丸めます.

実行例.hs
> div 1 2
0  -- 0.5を負の無限大の方に丸めている. 商が正の値になる場合はdivとquotの結果に差はない

> div 4 2
2

> div (-5) 2
-3 -- 2.5を負の無限大の方に丸めている
mod
mod :: Integral a => a -> a -> a

整数の剰余演算を行います.
mod x y == x - (div x y) * yとなるように求めます.

実行例.hs
> mod 1 2
1 -- div 1 2は0なので, mod 1 2 == 1 - 0 * 2 == 1

> mod 4 2
0

> mod (-5) 2
1 -- div (-5) 2は(-3)なので, mod (-5) 2 == (-5) - (-3) * 2 == 1
quotRem
quotRem :: Integral a => a -> a -> (a, a)

quotremの結果をタプルにして返します.

実行例.hs
> quotRem 1 2
(0,1)

> quotRem 4 2
(2,0)

> quotRem (-5) 2
(-2,-1)
divMod
divMod :: Integral a => a -> a -> (a, a)

divmodの結果をタプルにして返します.

実行例.hs
> divMod 1 2
(0,1)

> divMod 4 2
(2,0)

> divMod (-5) 2
(-3,1)
toInteger
toInteger :: Integral a => a -> Integer

Integral型クラスのインスタンスの型のリテラルをInteger型に変換します.

実行例.hs
> a = 1 :: Int
> toInteger a
1
> b = toInteger a
> :t b
b :: Integer
属するインスタンス

Fractional型クラス

メソッド
(/)
(/) :: a -> a -> a
infixl 7 /
実行例.hs

recip
recip :: a -> a
実行例.hs

fromRational
fromRational :: Rational -> a
実行例.hs

属するインスタンス

Floating型クラス

メソッド
pi
pi :: Floating a => a

円周率πです.

実行例.hs
> pi
3.141592653589793
exp
log
sqrt
(**)
logBase
sin
cos
tan
asin
acos
atan
sinh
cosh
tanh
asinh
acosh
atanh
属するインスタンス

RealFrac型クラス

メソッド
properFraction
truncate
round
ceiling
floor
属するインスタンス

RealFloat型クラス

メソッド
floatRadix
floatDigits
floatRange
decodeFloat
encodeFloat
exponent
significant
scaleFloat
isNaN
isInfinite
isDenormalized
isNegativeZero
isIEEE
atan2
属するインスタンス

数値に関する関数

subtract
subtract :: Num a => a -> a -> a

(-)と同じですが, 部分適用が出来ます.
(-)だと, (- n)は単項演算子の(-)扱いされて、負数のマイナスnになります.
subtractは部分適用が出来るので, 高階関数に渡す時にはこちらを利用して下さい.

実行例.hs
> map (subtract 1) [1, 2, 3]
[0,1,2]
even
even :: Integral a => a -> Bool

引数が奇数であればFalse, 偶数であればTrueを返します.

実行例.hs
> even 1
False

> even 2
True
odd
odd :: Integral a => a -> Bool

引数が奇数であればTrue, 偶数であればFalseを返します.

実行例.hs
> odd 1
True

> odd 2
False
gcd
gcd :: Integral a => a -> a -> a

第1引数と第2引数の最大公約数を返します.

実行例.hs
> gcd 8 12
4

> gcd 11 23
1
lcm
lcm :: Integral a => a -> a -> a

第1引数と第2引数の最小公倍数を返します.

実行例.hs
> lcm 4 5
20

> lcm 8 12
24
(^)
(^^)
fromIntegral
realToFrac

実行例.hs

モノイド

Monoid型クラス

メソッド
mempty
mappend
mconcat
属するインスタンス

MonadとFunctor

Functor型クラス

メソッド
fmap
(<$)
属するインスタンス
関連する関数
(<$>)

Applicative型クラス

メソッド
pure
(<*>)
(*>)
(<*)
属するインスタンス

Monad型クラス

メソッド
(>>=)
(>>)
return
fail
属するインスタンス
関連する関数
mapM_
sequence_
(=<<)

畳み込みと走査

Foldable型クラス

メソッド
foldMap
foldr
foldl
foldr1
foldl1
null
length
elem
maximum
minimum
sum
product
属するインスタンス

Traversable型クラス

メソッド
traverse
sequenceA
mapM
sequence
属するインスタンス

雑多な関数

id
const
(.)
flip
($)
until
asTypeOf
error
errorWithoutStackTrace
undefined
seq
($!)

リスト操作

特別な畳み込み

リスト作成

スキャン

無限リスト

部分リスト

探索

リストのZipとUnzip

文字列に関する関数

文字列との相互変換

文字列への変換

文字列からの変換

基本入出力

単純なI/O操作

出力関数

入力関数

ファイル

I/Oモナドの例外処理

20
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
8