Skip to content

Instantly share code, notes, and snippets.

@HiroshiOkada
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HiroshiOkada/e5d86cbb7f194243d2fb to your computer and use it in GitHub Desktop.
Save HiroshiOkada/e5d86cbb7f194243d2fb to your computer and use it in GitHub Desktop.
「すごいHaskellたのしく学ぼう!」を読んでいて出てきた関数のメモ
-- Prelude 
5 + 2                  == 8
5 * 2                  == 10
5 - 2                  == 3
5 / 2                  == 2.5
( True || False )      == True
( True && False )      == False
not True               == False
succ 'c'               == 'd'
min 4 5                == 4
max 'a' 'z'            == 'z'
div 92 3               == 30
mod 14 5               == 4
[1,2,3] ++ [4,5]       == [1,2,3,4,5]
'a':" long time"       == "a long time"  
"abcdef" !! 2          == 'c'
( [1,2,3] < [1,2,4] )  == True
( "bbc" > "abc" )      == True
( [1,2,3] <= [1,2,4] ) == True
( "bbc" >= "abc" )     == True
( "abc" == "abc" )     == True
(1.0 /= 1.5)           == True 
head [1..]             == 1
tail [1,2,3]           == [2,3]
init [1,2,3,4]         == [1,2,3]
length ['a'..'z']      == 26
null []                == True
reverse [1,2,3]        == [3,2,1]
take 3 [1..]           == [1,2,3]
drop 7 [1..10]         == [8,9,10]
maximum [1,10,2]       == 10
minimum [3,2,4]        == 2
sum [1,10,100]         == 10
product [2,3,4]        == 24
elem 4 [1,2,3,4,5]     == True
take 10 ( cycle "abc") == "abcabcabca"
take 5 ( repeat 'x')   == "xxxxx"
replicate 5 'x'        == "xxxxx"
odd 5                  == True
even 8                 == True
fst (1,'A')            == 1
snd (1,'A')            == 'A'
zip [1..] "ABC"        == [(1,'A'),(2,'B'),(3,'C')]
show 4.25              == "4.25"
(read "4" :: Int)      == 4 
(reads "4kg" :: [(Int,String)])  == [(4,"kg")]
(minBound :: (Bool, Int, Char))  == (False,-9223372036854775808,'\NUL') 
(maxBound :: (Bool, Int, Char))  == (True,9223372036854775807,'\1114111')
fromIntegral (div 5 3) + 1.5     == 2.5 
compare 1 2                      == LT
zipWith (*) [1..3] [10,20,30]    == [10,40,90]
flip (++) "DEF" "ABC"            == "ABCDEF"
map (10-) [1..3]           == [9,8,7]
filter (<3) [1..10]        == [1,2]
takeWhile (/= 'd') "abracadabra" == "abraca"
foldl (-) 10 [1,2]         == 7
foldr (-) 10 [1,2]         == 9
foldl1 (-) [10,1,2]        == 7
foldr1 (-) [1,2,10]        == 9
and [True, False, True]    == False
or  [True, False, True]    == True
scanl (+) 0 [1,2,3]        == [0,1,3,6]
scanr (+) 0 [1,2,3]        == [6,5,3,0]
scanl1 (+) [1,2,3]         == [1,3,6]
scanr1 (+) [1,2,3]         == [6,5,3]
abs (-9)                   == 9
sqrt 9                     == 3.0
negate 3.0                 == -3.0
negate $ sqrt $ abs (-9)   == -3.0
(negate . sqrt . abs) (-9) == -3.0
id "itself"                == "itself"
fmap (+1) [1,2,3]          == [2,3,4]
fmap (+1) (Just 100)       == Just 101
words "I don't like it!"   == ["I","don't","like","it!"]
unwords ["A","big","dog"]  == "A big dog"
lines "\naa\nbb\n"         == ["","aa","bb"]
unlines unlines ["x","y"]  == "x\ny\n"

-- import qualified Data.List as List
List.nub "abracadabra"          == "abrcd"
List.sort [9,8..0]              == [0,1,2,3,4,5,6,7,8,9]
List.group "aaabbcca"           == ["aaa","bb","cc","a"]
List.tails [1,2,3]              == [[1,2,3],[2,3],[3],[]]
List.isPrefixOf "Hi" "Hiroshi!" == True
List.any (== 'c') "abcdef"      == True
List.isInfixOf "abc" "xxabcyy"  == True 
List.foldl'                     -- foldl の正格(Strict evaluation)版
List.foldr'                     -- foldr の正格(Strict evaluation)版
List.find (== 0) [1,2,3]        == Nothing
List.delete 'c' "abcabc"        == "ababc"

-- import qualified Data.Char as Char
Char.ord 'A'                    == 65
Char.chr 97                     == 'a'
Char.digitToInt '2'             == 2
Char.toUpper 'a'                == 'A'

-- import qualified Data.Map as Map
Map.fromList [(1,"One"), (2,"Two")]    == fromList [(1,"One"),(2,"Two")]
Map.lookup 2 $ Map.fromList [(1,"A"), (2,"B")] == Just "B"
Map.fromListWith (+) [("A",1),("B",2),("A",3)] == fromList [("A",4),("B",2)]

-- import qualified Data.List as List
Directory.removeFile ファイル名
Directory.renameFile 元の名前 新しい名前

-- import qualified System.Random as Rand
Rand.random 乱数ジェネレーター
   乱数と新しい乱数ジェネレータを返す
Rand.mkStdGen 乱数の種となるInt型の数
   StdGen型の乱数ジェネレータを返す
Rand.randoms 乱数ジェネレータ
   無限長の乱数のリスト返す
Rand.randomR (下限値,上限値) 乱数ジェネレータ
   下限値と上限値の間の乱数と新しい乱数ジェネレータを返す
Rand.randomRs (下限値,上限値) 乱数ジェネレータ
   下限値と上限値の間の無限長の乱数のリストを返す
stdGen <- Rand.getStdGen
   グローバル乱数ジェネレータを初期化する
stdGen' <- Rand.newStdGen
   新しい乱数ジェネレータを返しグローバル乱数ジェネレータを更新する

-- データ型
data Bool = True | Flase
data Maybe a = Nothing | Just a
data Either a b = Left a | Right a

-- IO
-- import qualified Control.Monad as Monad
-- import qualified System.IO as IO
-- import qualified Control.Exception as Ex
-- import qualified System.Environment as Env
putStrLn 文字列            文字列出力+改行
putStr 文字列              文字列出力
putChar 文字               一文字出力
s <- getLine               一行読み込み改行は削除
print 値                   値を文字列化(show)して出力
sequence IOアクションリスト   IOアクションのリストを順に実行結果のリストを返す
mapM IOを返す関数 リスト      mapIO を返す関数版
mapM_ IOを返す関数 リスト     mapM の結果を廃棄する版
Monad.forever IOアクション   IOアクションを永遠に繰り返す
Monad.forM リスト IOアクション  mapM と同じだが引数が逆順
s <- getContents            全部読み込み(遅延評価)
interact 文字列変換関数
         文字列を変換する関数を受け取って標準入力の入力に適用し標準出力に出力する
fh <- IO.openFile ファイルパス IOモード
         指定されたファイルを指定されたIOモードで開くIOアクションを返す
IO.hGetContents fh
         ファイルに含まれる内容を結果として返す IO String を返す
IO.hClose fh
         ファイルを閉じる IOアクションを返す
IO.withFile ファイルパス IOモード ファイルをハンドルを受け取って何かする関数
     ファイルを開いて何かする関数で処理しそのあとファイルを閉じる
IO.hPutStr fh 文字列
         ファイルに文字列を改行をつけて出力
IO.hPutChar fh 文字
         ファイルに文字を出力
IO.hGetChar fh
         ファイルから一文字入力
IO.hGetLine fh 
         ファイルから一行入力改行文字は捨てられる
Io.readFile ファイル名
         ファイルの内容を文字列として読み込む
IO.writeFile ファイル名 文字列
         ファイルに文字列を書き込む(上書き)
IO.appendFile ファイル名 文字列
         ファイルに文字列を書き込む(追記)
IO.openTempFile ディレクトリパス テンポラリファイルの基準名
         テンポラリファイルを開きその名前とハンドルを返す
Ex.bracket リソースの確保を行う リソースを開放する リソースを受け取り何かする
         失敗した時に確実にリソースを開放する(例外が起こらなくても開放する)         
Ex.bracketOnError リソースの確保を行う リソースを開放する リソースを受け取り何かする
         失敗した時にリソースを開放する(例外が起こった場合にのみ開放する)
progName <- Env.getProgName
         コマンド名を取得
args <- Env.getArgs
         コマンドライン引数を取得(文字列の配列)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment