2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haskell Data.Listのリスト操作関数(サンプル付き)

Last updated at Posted at 2017-02-17

自分用メモ。全関数は網羅していない。

完全なリファレンスはこちら。
http://hackage.haskell.org/package/base-4.9.1.0/docs/Data-List.html

集合操作

>>> [3,1,4,1,5,9] \\ [1,9]
[3,4,1,5]
>>> "dog" `union` "cow"
"dogcw"
>>> [1,2,3,4] `intersect` [2,4,6,8]
[2,4]

部分リスト

>>> inits [1,2,3,4]
[[],[1],[1,2],[1,2,3],[1,2,3,4]]
>>> tails [3, 1, 4, 1, 5, 9]
[[3,1,4,1,5,9],[1,4,1,5,9],[4,1,5,9],[1,5,9],[5,9],[9],[]]
>>> subsequences [3, 1, 4, 1, 5, 9]
[[],[3],[1],[3,1],[4],[3,4],[1,4],[3,1,4],[1],[3,1],[1,1],[3,1,1],[4,1],[3,4,1],[1,4,1],[3,1,4,1],[5],[3,5],[1,5],[3,1,5],[4,5],[3,4,5],[1,4,5],[3,1,4,5],[1,5],[3,1,5],[1,1,5],[3,1,1,5],[4,1,5],[3,4,1,5],[1,4,1,5],[3,1,4,1,5],[9],[3,9],[1,9],[3,1,9],[4,9],[3,4,9],[1,4,9],[3,1,4,9],[1,9],[3,1,9],[1,1,9],[3,1,1,9],[4,1,9],[3,4,1,9],[1,4,1,9],[3,1,4,1,9],[5,9],[3,5,9],[1,5,9],[3,1,5,9],[4,5,9],[3,4,5,9],[1,4,5,9],[3,1,4,5,9],[1,5,9],[3,1,5,9],[1,1,5,9],[3,1,1,5,9],[4,1,5,9],[3,4,1,5,9],[1,4,1,5,9],[3,1,4,1,5,9]]
>>> isSubsequenceOf [3,4,9] [3,1,4,1,5,9]
True
>>> isInfixOf [1,4,1] [3,1,4,1,5,9]
True
>>> isPrefixOf [3,1,4,1] [3,1,4,1,5,9]
True
>>> isSuffixOf [1,5,9] [3,1,4,1,5,9]
True
>>> permutations [1,2,3]
[[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]]

条件を満たす部分リスト

>>> takeWhile (< 3) [1,2,3,4,1,2,3,4]
[1,2]
>>> dropWhile odd [3,1,4,1,5,9]
[4,1,5,9]
>>> dropWhileEnd odd [3,1,4,1,5,9]
[3,1,4]
>>> span odd [3, 1, 4, 1, 5, 9]
([3,1],[4,1,5,9])
>>> break even [3, 1, 4, 1, 5, 9]
([3,1],[4,1,5,9])

グループ化

>>> group [3,3,1,1,1,4,1,5,9]
[[3,3],[1,1,1],[4],[1],[5],[9]]
>>> partition even [1,2,3,4,5]
([2,4],[1,3,5])

挿入

>>> insert 8 [3,1,4,1,5,9]
[3,1,4,1,5,8,9]

削除

>>> stripPrefix "foo" "foobar"
Just "bar"
>>> stripPrefix "foo" "barfoo"
Nothing

結合

>>> intercalate "," ["hoge", "moge", "sage"]
"hoge,moge,sage"
>>> intersperse 5 [1,2,3]
[1,5,2,5,3]
>>> intersperse "," ["hoge", "moge", "sage"]
["hoge",",","moge",",","sage"]
>>> concat $ intersperse "," ["hoge", "moge", "sage"]
"hoge,moge,sage"

Control.Monad.joinconcatに等しい。

>>> import Control.Monad
>>> join ["hoge", "moge"]
"hogemoge"
>>> join [[1,2], [3,4]]
[1,2,3,4]

繰り返し

>>> take 10 $ iterate (++ "x") ""
["","x","xx","xxx","xxxx","xxxxx","xxxxxx","xxxxxxx","xxxxxxxx","xxxxxxxxx"]
>>> take 10 $ repeat "hoge"
["hoge","hoge","hoge","hoge","hoge","hoge","hoge","hoge","hoge","hoge"]
>>> replicate 3 "hoge"
["hoge","hoge","hoge"]
>>> take 10 $ cycle [3,1,4]
[3,1,4,3,1,4,3,1,4,3]

最大・最小

>>> maximum [3,1,4,1,5,9]
9
>>> maximumBy (\a -> \b -> compare (abs a) (abs b)) [3,1,4,1,5,-9]
-9
>>> minimum [3,1,4,1,5,9]
1

ソート

>>> sortOn abs [-3, 1, -4, 1, -5, 9]
[1,1,-3,-4,-5,9]

行列

>>> transpose [[3,1,4], [1,5,9]]
[[3,1],[1,5],[4,9]]

zipとunzip

>>> zip [3,1,4,1,5,9] [0..]
[(3,0),(1,1),(4,2),(1,3),(5,4),(9,5)]
>>> unzip $ zip [3,1,4,1,5,9] [0..]
([3,1,4,1,5,9],[0,1,2,3,4,5])
2
3
0

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?