🦔

2022/12/22に公開

いちいちfとかgに置き換えてるけど本来はそんなこと不要です。
codewarsとかアルゴ式とか他人様のコードが読めるところが勉強になります。

### ひとつの引数を二項演算子にダブル適用する

``````import Control.Monad (join)
f = join
f (*) 5 --> 25 = 5 * 5
``````

### +1する、-1する

``````f = succ
g = pred

f 1 --> 2
g 2 --> 1
``````

### リストの中身をひとつずつ改行しながら表示する

``````f = mapM_ print
f ['1', '2', '3'] -->
'1'
'2'
'3'
``````

### 先頭、最後、先頭以外、最後以外を取得

``````f = head
g = tail
h = last
i = init

f [1..4] --> 1
g [1..4] --> [2,3,4]
h [1..4] --> 4
i [1..4] --> [1,2,3]
``````

### リストの要素存在チェック

``````import Data.List (elemIndex, elemIndices)
f = elem
g = notElem
h = elemIndex
i = elemIndices
f 1 [1..10] --> True
g 1 [1..10] --> False
h 1 [1..10] --> Just 0
i 1 [1,2,3,1,2,3] --> [0,3]
``````

### 値を要素の間に挿入

``````import Data.List (intersperse)
f = intersperse

f 0 [1..4] --> [1,0,2,0,3,0,4]
f ',' "hello" --> "h,e,l,l,o"
``````

### 連結

``````f = concat
f ["hello", " ", "world"] --> "hello world"
f [[1,2,3], [4,5,6]] --> [1,2,3,4,5,6] flatten的な

g = msum
g ["hello", " ", "world"] --> "hello world"
``````

### プラスマイナスを入れ替える

``````f = negate
f (-1) --> 1
f 2 --> (-2)
``````

### 引き算する

``````f = subtract
f 1 5 --> 4 , subtract a from b らしい (b - a)
``````

### 最大最小

``````f = maximum
g = minimum
h = max
i = min

f [1..10] --> 10
g [1..10] --> 1
h 1 (-1) --> 1
i 1 (-1) --> -1
``````