Skip to content

Commit 3b237a0

Browse files
committed
Introduce the function to refactor into point-free
1 parent c32b48a commit 3b237a0

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

assets/18.md

+17-22
Original file line numberDiff line numberDiff line change
@@ -657,39 +657,34 @@ ghci> (4 -) 4
657657

658658
### やり過ぎ注意、あるいはどこまでやるか
659659

660-
hoge: ポイントフリースタイルについてここで説明
661-
662-
例1: `yes``Yes``yEs`などを探す
663-
664-
前提: `filter`関数。第一引数が`True`を返した要素だけのリストを返す
660+
復習を兼ねて、ここまでで紹介した種々のテクニックを活用して、次⬇️の関数をより簡潔に書き換えてみましょう。この課題の学習する内容と直接関係がない知識も混ざりますが、ついでに学ぶと思ってご容赦ください。
665661

662+
```haskell
663+
selectYes :: [String] -> [String]
664+
selectYes strs = filter (\str -> map toUpper str == "YES") strs
666665
```
667-
ghci> :t filter
668-
filter :: (a -> Bool) -> [a] -> [a]
669666

670-
-- 3より大きい数
671-
ghci> filter (\x -> x > 3) [4, 3, 6]
672-
[4,6]
667+
この`selectYes`関数は、文字列のリストから`"yes"`という文字列を探して、`"yes"`のみが含まれるリストを返します。ただし、大文字と小文字の区別をしません。つまり、`"yes"`だけでなく`"Yes"`でも`"yEs"`等も含みます。これだけではあまり役に立たないですが、まぁ例と言うことで。もっと拡張したら役に立つシーンもあるかも知れませんし。
673668

674-
-- 当然、第一引数の関数はラムダ抽象の代わりにセクションで書けるので
675-
ghci> filter (> 3) [4, 3, 6]
676-
[4,6]
677-
```
669+
実際に書き換える前に、`selectYes`関数をGHCiに入力して、動作を確かめておきましょう:
678670

679-
```
671+
```haskell
672+
ghci> -- 利用する toUpper 関数は Data.Char モジュールにあるので、importしておきます
680673
ghci> import Data.Char
681674

682-
-- 大文字にすると「YES」になる文字列だけを返す
683-
ghci> filter (\str -> map toUpper str == "YES") ["no", "yes", "yES", "iie", "YES"]
675+
ghci> -- GHCi に入力する場合、:{ と :} で囲むのを忘れずに
676+
ghci> :{
677+
ghci| selectYes :: [String] -> [String]
678+
ghci| selectYes strs = filter (\str -> map toUpper str == "YES") strs
679+
ghci| :}
680+
681+
ghci> selectYes ["no", "yes", "yES", "iie", "YES"]
684682
["yes","yES","YES"]
685683
```
686684

687-
`toUpper`した結果が`"YES"`と等しい」
685+
hoge: `filter`より先に`map`を書き換えて、`filter`を書き換えるときに`filter`を紹介
688686

689-
```
690-
ghci> filter ((== "YES") . map toUpper) ["no", "yes", "yES", "iie", "YES"]
691-
["yes","yES","YES"]
692-
```
687+
hoge: ポイントフリースタイルについてここで説明
693688

694689
このくらいなら序の口?
695690

0 commit comments

Comments
 (0)