LYAHFGG!まとめ(リファレンス)

前置き

これは、LYAHFGG!まとめ(Part1)同Part2の補足記事です。Miran Lipovača氏によるHaskellのチュートリアルLearn You a Haskell for Great Good!から、リファレンス的な意味合いの情報を整理しました。

当初は関数リファレンスの役割も目指してましたが、挫折して関数名のみ列挙した部分も多数あります。詳しくはHoogleで検索して下さい。

本文

ghciコマンド

:l                                   --load
:r                                   --reload
:set prompt "ghci> "
:t                                   --type
:m + Data.List                       --import
:m + Data.List Data.Map Data.Set
:i                                   --info
:k                                   --kind

用語

name
名前。変数?
function
関数
list
リスト
tuple
タプル
type
typeclass
型クラス
range
レンジ
guard
ガード
kind
種。型の型。
unit
空タプルの別名
typeclass constraint(class constraint)
型クラス制約
infix functions
中置の関数
prefix functions
前置の関数
sections
セクション。中置関数の部分適用。
lambda
ラムダ。無名関数。
type variable
型変数
type parameter
型引数。型コンストラクタの引数。
type annotations
型注釈。readで使う::のこと。
list comprehensions
リスト内包表記
module
モジュール
value constructor
値コンストラクタ、データコンストラクタ
type constructor
型コンストラクタ
record syntax
レコードシンタックス
type synonym
型シノニム
IO action
IOアクション
lazy evaluation
遅延評価
strict
正格
curried functions
カリー化(された)関数
function application
関数適用
partially applied function
部分適用(された)関数
function composition
関数合成
higher order function
高階関数
pholymorphic functions
多態な関数?
imperative programming languages
手続き型言語、命令型言語
infinite list
無限リスト
left-associative
左結合
right-associative
右結合
no side-effects
副作用なし
singleton
シングルトン。要素が1個。
pair
要素が2個。
triple
要素が3個。
point free style(pointless style)
ポイントフリー。引数の無い関数定義。
precedence rule
(演算子の)優先ルール
predicate
述語
recursion
再帰
referential transparency
参照透明性、参照透明性
set comprehensions
集合内包表記
syntactic sugar
糖衣構文。シンタックスシュガー。
type inference
型推論
Paamayim Nekudotayim
ダブルコロン。パアマイム・ネクドタイム(ヘブライ語)。
concrete
具象
associativity
結合性
non-deterministic
非決定性
difference list
差分リスト

Prelude

ghci> id 8                             --引数をそのまま返す
8
ghci> succ 8
9
ghci> min 9 10
9
ghci> max 100 101
101
ghci> head [5,4,3,2,1]
5
ghci> tail [5,4,3,2,1]
[4,3,2,1]
ghci> last [5,4,3,2,1]
1
ghci> init [5,4,3,2,1]
[5,4,3,2]
ghci> length [5,4,3,2,1]
5
ghci> null []
True
ghci> reverse [5,4,3,2,1]
[1,2,3,4,5]
ghci> take 3 [5,4,3,2,1]
[5,4,3]
ghci> drop 3 [8,4,2,1,5,6]
[1,5,6]
ghci> minimum [8,4,2,1,5,6]
1
ghci> maximum [1,9,2,3,4]
9
ghci> sum [5,2,1,6,3,2,5,7]
31
ghci> product [6,2,1,2]
24
ghci> 4 `elem` [3,4,5,6]
True
ghci> take 10 (cycle [1,2,3])
[1,2,3,1,2,3,1,2,3,1]
ghci> take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]
ghci> replicate 3 10
[10,10,10]
ghci> fst (8,11)
8
ghci> snd (8,11)
11
ghci> zip [1,2,3,4,5] [5,5,5,5,5]      --短い方に揃えられる
[(1,5),(2,5),(3,5),(4,5),(5,5)]
ghci> "Abrakadabra" `compare` "Zebra"
LT
ghci> show 3
"3"
ghci> read "5" :: Int                  --型注釈(明らかなら不要)
5
ghci> minBound :: Int
-2147483648
ghci> maxBound :: Char
'\1114111'
ghci> fromIntegral 5                   --IntegralをNumに
5
ghci> error "Can't call head on an empty list, dummy!"
*** Exception: Can't call head on an empty list, dummy!
ghci> subtract 3 5
2
ghci> map (+3) [1,5,3,1,6]
[4,8,6,4,9]
ghci> filter (>3) [1,5,3,2,1,6,4,3,2,1]
[5,6,4]
ghci> takeWhile (/=' ') "elephants know how to party"
"elephants"
head' :: [a] -> a
head' = foldr1 (\x _ -> x)
last' :: [a] -> a
last' = foldl1 (\_ x -> x)
ghci> scanl (+) 0 [3,5,2,1]
[0,3,8,10,11]
ghci> scanr (+) 0 [3,5,2,1]
[11,8,3,1,0]
ghci> scanl1 (\acc x -> if x > acc then x else acc) [3,4,5,3,7,9,2,1]
[3,4,5,5,7,9,9,9]
ghci> scanl (flip (:)) [] [3,2,1]
[[],[3],[2,3],[1,2,3]]
  • putStr
  • putChar
  • print
  • getChar
  • sequence
  • mapM
  • mapM_
  • getContents
  • interact
  • readFile
  • writeFile
  • appendFile

Data.List

ghci> intersperse '.' "MONKEY"
"M.O.N.K.E.Y"
ghci> intercalate " " ["hey","there","guys"]
"hey there guys"
ghci> transpose ["hey","there","guys"]
["htg","ehu","yey","rs","e"]
ghci> concat ["foo","bar","car"]
"foobarcar"
ghci> concatMap (replicate 4) [1..3]
[1,1,1,1,2,2,2,2,3,3,3,3]
ghci> and $ map (>4) [5,6,7,8]
True
ghci> or $ map (==4) [2,3,4,5,6,1]
True
ghci> any (==4) [2,3,5,6,1,4]
True
ghci> take 10 $ iterate (*2) 1
[1,2,4,8,16,32,64,128,256,512]
ghci> splitAt 3 "heyman"
("hey","man")
ghci> takeWhile (>3) [6,5,4,3,2,1,2,3,4,5,4,3,2,1]
[6,5,4]
ghci> dropWhile (/=' ') "This is a sentence"
" is a sentence"
ghci> let (fw, rest) = span (/=' ') "This is a sentence" in "First word:" ++ fw ++ ", the rest:" ++ rest
"First word: This, the rest: is a sentence"
ghci> break (==4) [1,2,3,4,5,6,7]
([1,2,3],[4,5,6,7])
ghci> sort [8,5,3,2,1,6,4,2]
[1,2,2,3,4,5,6,8]
ghci> group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7]
[[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[7]]
ghci> inits "w00t"
["","w","w0","w00","w00t"]
ghci> tails "w00t"
["w00t","00t","0t","t",""]
ghci> "cat" `isInfixOf` "im a cat burglar"
True
ghci> "hey" `isPrefixOf` "hey there!"
True
ghci> "there!" `isSuffixOf` "oh hey there!"
True
ghci> partition (`elem` ['A'..'Z']) "BOBsidneyMORGANeddy"
("BOBMORGAN","sidneyeddy")
ghci> find (>4) [1,2,3,4,5,6]
Just 5
ghci> 4 `elemIndex` [1,2,3,4,5,6]
Just 3
ghci> ' ' `elemIndices` "Where are the spaces?"
[5,9,13]
ghci> findIndex (==7) [5,3,2,1,6,4]
Nothing
ghci> findIndices (`elem` ['A'..'Z']) "Where Are The Caps?"
[0,6,10,14]
ghci> zipWith3 (\x y z -> x + y + z) [1,2,3] [4,5,2,2] [2,2,3]
[7,9,8]
ghci> zip4 [2,3,3] [2,2,2] [5,5,3] [2,2,2]
[(2,2,5,2),(3,2,5,2),(3,2,3,2)]
ghci> lines "first line\nsecond line\nthird line"
["first line","second line","third line"]
ghci> unlines ["first line", "second line", "third line"]
"first line\nsecond line\nthird line\n"
ghci> words "hey these           are    the words in this\nsentence"
["hey","these","are","the","words","in","this","sentence"]
ghci> unwords ["hey","there","mate"]
"hey there mate"
ghci> nub [1,2,3,4,3,2,1,2,3,4,3,2,1]
[1,2,3,4]
ghci> delete 'h' "hey there ghang!"
"ey there ghang!"
ghci> [1..10] \\ [2,5,9]
[1,3,4,6,7,8,10]
ghci> "hey man" `union` "man what's up"
"hey manwt'sup"
ghci> [1..7] `intersect` [5..10]
[5,6,7]
ghci> insert 4 [3,5,1,2,8,2]
[3,4,5,1,2,8,2]
ghci> groupBy ((==) `on` (> 0)) values
[[-4.3,-2.4,-1.2],[0.4,2.3,5.9,10.5,29.1,5.3],[-2.4,-14.5],[2.9,2.3]]
ghci> let xs = [[5,4,5,4,4],[1,2,3],[3,5,4,3],[],[2],[2,2]]
ghci> sortBy (compare `on` length) xs
[[],[2],[2,2],[1,2,3],[3,5,4,3],[5,4,5,4,4]]
  • foldl'
  • foldl1'
  • elem
  • notElem
  • zip3
  • zip4
  • zipWith3
  • zipWith4
  • genericLength
  • genericTake
  • genericDrop
  • genericSplitAt
  • genericIndex
  • genericReplicate
  • nubBy
  • deleteBy
  • unionBy
  • intersectBy
  • groupBy
  • sortBy
  • insertBy
  • maximumBy
  • minimumBy

Data.Char

ghci> intToDigit 15
'f'
ghci> ord 'a'
97
ghci> chr 97
'a'
  • isControl
  • isSpace
  • isLower
  • isUpper
  • isAlpha
  • isAlphaNum
  • isPrint
  • isDigit
  • isOctDigit
  • isHexDigit
  • isLetter
  • isMark
  • isNumber
  • isPunctuation
  • isSymbol
  • isSeparator
  • isAscii
  • isLatin1
  • isAsciiUpper
  • isAsciiLower
  • toUpper
  • toLower
  • toTitle
  • digitToInt
ghci> generalCategory ' '
Space
ghci> generalCategory 'A'
UppercaseLetter

Data.Map

ghci> Map.fromList [("betty","555-2938"),("bonnie","222-2222"),("lucille","205-2928"),("bonnie","452-2928")]
fromList [("betty","555-2938"),("bonnie","452-2928"),("lucille","205-2928")]
ghci> Map.empty
fromList []
ghci> Map.insert 3 100 Map.empty
fromList [(3,100)]
ghci> Map.null Map.empty
True
ghci> Map.size $ Map.fromList [(2,4),(3,3),(4,2),(5,4),(6,4)]
5
ghci> Map.singleton 3 9
fromList [(3,9)]
ghci> Map.member 3 $ Map.fromList [(3,6),(4,3),(6,9)]
True
ghci> Map.map (*100) $ Map.fromList [(1,1),(2,4),(3,9)]
fromList [(1,100),(2,400),(3,900)]
ghci> Map.filter isUpper $ Map.fromList [(1,'a'),(2,'A'),(3,'b'),(4,'B')]
fromList [(2,'A'),(4,'B')]
ghci> Map.toList . Map.insert 9 2 $ Map.singleton 4 3
[(4,3),(9,2)]
ghci> Map.insertWith (+) 3 100 $ Map.fromList [(3,4),(5,103),(6,339)]
fromList [(3,104),(5,103),(6,339)]
  • lookup
  • keys
  • elems
  • fromListWith

Data.Set

text1 = "I just had an anime dream. Anime... Reality... Are they so different?"
text2 = "The old man left his garbage can out and now his trash is all over my lawn!"
ghci> let set1 = Set.fromList text1
ghci> let set2 = Set.fromList text2
ghci> set1
fromList " .?AIRadefhijlmnorstuy"
ghci> set2
fromList " !Tabcdefghilmnorstuvwy"
ghci> Set.intersection set1 set2
fromList " adefhilmnorstuy"
ghci> Set.difference set1 set2
fromList ".?AIRj"
ghci> Set.union set1 set2
fromList " !.?AIRTabcdefghijlmnorstuvwy"
ghci> Set.null Set.empty
True
ghci> Set.null $ Set.fromList [3,4,5,5,4,3]
False
ghci> Set.size $ Set.fromList [3,4,5,3,4,5]
3
ghci> Set.singleton 9
fromList [9]
ghci> Set.insert 4 $ Set.fromList [9,3,8,1]
fromList [1,3,4,8,9]
ghci> Set.insert 8 $ Set.fromList [5..10]
fromList [5,6,7,8,9,10]
ghci> Set.delete 4 $ Set.fromList [3,4,5,4,3,4,5]
fromList [3,5]
ghci> Set.filter odd $ Set.fromList [3,4,5,6,7,2,3,4]
fromList [3,5,7]
ghci> Set.map (+1) $ Set.fromList [3,4,5,6,7,2,3,4]
fromList [3,4,5,6,7,8]
ghci> let setNub xs = Set.toList $ Set.fromList xs
ghci> setNub "HEY WHATS CRACKALACKIN"
" ACEHIKLNRSTWY"

Control.Monad

  • when
  • forever
  • forM

System.IO

  • openFile
  • hClose
  • withFile
  • hGetContents
  • hGetLine
  • hPutStr
  • hPutStrLn
  • hGetChar
  • hFlush
  • openTempFile

System.Directory

  • removeFile
  • renameFile

System.Environment

  • getArgs
  • getProgName

System.Random

  • random
  • mkStdGen
  • randoms
  • randomR
  • randomRs
  • getStdGen
  • pack
  • unpack
  • fromChunks
  • toChunks
  • cons
  • cons'
  • empty
  • head, tail, init, null, length, map, reverse, foldl, foldr, concat, takeWhile, filter
  • readFile
  • doesFileExist
  • catch (System.IO.Error)
  • isDoesNotExistError
  • ioError
  • isAlreadyExistsError
  • isDoesNotExistError
  • isAlreadyInUseError
  • isFullError
  • isEOFError
  • isIllegalOperation
  • isPermissionError
  • isUserError
  • ioeGetFileName
  • path.txt
  • ZipList (Control.Applicative)
  • getZipList
  • liftA2
  • Product
  • Sum
  • Any
  • All
  • Last a
  • Foldable (Data.Foldable)
  • foldMap
Last modified:2012/11/27 16:54:26
Keyword(s):
References:[FP: 関数型プログラミング] [LYAHFGG!まとめ(Part1)] [LYAHFGG!まとめ(Part2)]
This page is frozen.