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

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

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

# 集合操作

```hs
>>> [3,1,4,1,5,9] \\ [1,9]
[3,4,1,5]
```

```hs
>>> "dog" `union` "cow"
"dogcw"
```

```hs
>>> [1,2,3,4] `intersect` [2,4,6,8]
[2,4]
```

# 部分リスト

```hs
>>> inits [1,2,3,4]
[[],[1],[1,2],[1,2,3],[1,2,3,4]]
```

```hs
>>> 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],[]]
```

```hs
>>> 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]]
```

```hs
>>> isSubsequenceOf [3,4,9] [3,1,4,1,5,9]
True
```

```hs
>>> isInfixOf [1,4,1] [3,1,4,1,5,9]
True
```

```hs
>>> isPrefixOf [3,1,4,1] [3,1,4,1,5,9]
True
```

```hs
>>> isSuffixOf [1,5,9] [3,1,4,1,5,9]
True
```

```hs
>>> permutations [1,2,3]
[[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]]
```

# 条件を満たす部分リスト

```hs
>>> takeWhile (< 3) [1,2,3,4,1,2,3,4]
[1,2]
```


```hs
>>> dropWhile odd [3,1,4,1,5,9]
[4,1,5,9]
```

```hs
>>> dropWhileEnd odd [3,1,4,1,5,9]
[3,1,4]
```

```hs
>>> span odd [3, 1, 4, 1, 5, 9]
([3,1],[4,1,5,9])
```

```hs
>>> break even [3, 1, 4, 1, 5, 9]
([3,1],[4,1,5,9])
```

# グループ化

```hs
>>> group [3,3,1,1,1,4,1,5,9]
[[3,3],[1,1,1],[4],[1],[5],[9]]
```

```hs
>>> partition even [1,2,3,4,5]
([2,4],[1,3,5])
```



# 挿入

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

# 削除

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

# 結合

```hs
>>> intercalate "," ["hoge", "moge", "sage"]
"hoge,moge,sage"
```

```hs
>>> intersperse 5 [1,2,3]
[1,5,2,5,3]
```

```hs
>>> intersperse "," ["hoge", "moge", "sage"]
["hoge",",","moge",",","sage"]
```

```hs
>>> concat $ intersperse "," ["hoge", "moge", "sage"]
"hoge,moge,sage"
```

`Control.Monad.join`も`concat`に等しい。

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

# 繰り返し

```hs
>>> take 10 $ iterate (++ "x") ""
["","x","xx","xxx","xxxx","xxxxx","xxxxxx","xxxxxxx","xxxxxxxx","xxxxxxxxx"]
```

```hs
>>> take 10 $ repeat "hoge"
["hoge","hoge","hoge","hoge","hoge","hoge","hoge","hoge","hoge","hoge"]
```

```hs
>>> replicate 3 "hoge"
["hoge","hoge","hoge"]
```

```hs
>>> take 10 $ cycle [3,1,4]
[3,1,4,3,1,4,3,1,4,3]
```

# 最大・最小

```hs
>>> maximum [3,1,4,1,5,9]
9
```

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

```hs
>>> minimum [3,1,4,1,5,9]
1
```


# ソート

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

# 行列

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

# zipとunzip

```hs
>>> zip [3,1,4,1,5,9] [0..]
[(3,0),(1,1),(4,2),(1,3),(5,4),(9,5)]
```

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