すごいhaskell読書会#1 in 大阪
TRANSCRIPT
![Page 1: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/1.jpg)
すごいH本 読書会 #1@yashigani
![Page 2: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/2.jpg)
[today tableOfContents]
はじめに
1章,2章のおさらい
関数呼出/定義
リスト
タプル
型
練習問題的な
![Page 3: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/3.jpg)
はじめに
関数型言語の経験は?
(私の場合)ほんのちょっとの Scheme と少しの Scala
なんで Haskell やろうと思いましたか?
ちなみに今回の発表はドシロウトが発表しますので,わいわいツッコミを入れながら楽しんでいただけると幸いです
わからないことは随時手を挙げて発言してください
誰かが答えます
![Page 4: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/4.jpg)
基本的なところ
だいたい想像通りです
+,-,*,/,div,mod
True | False
not True # => False
True == False # => False
True /= False # => True
“Selipnir” + 3 # => ?
![Page 5: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/5.jpg)
実は...
今までのは全部関数呼び出しです
succ 5 # => 6
max 9 10 # => 10
max (9 + 2) 10 # => 11
succ 9 * 10 # => 100
5 * 2 # => 10
9 `max` 10 # => 10
(*) 5 2 # => 10
![Page 6: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/6.jpg)
関数定義
関数名 引数 = 処理
doubleUpMe x = x * 2
if 式
odd’ n = if n `mod` 2 == 1 then True else False
else は必須
![Page 7: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/7.jpg)
リスト
連結
[1, 2, 3] ++ [4, 5, 6] # => [1, 2, 3, 4, 5, 6]
1:[2, 3, 4] # => [1, 2, 3, 4]
要素にアクセス
“Haskell” !! 3 # => ‘k’
ネスト
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
比較
![Page 8: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/8.jpg)
リストの操作
head
tail
last
init
![Page 9: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/9.jpg)
大事なことなので
head
tail
init
last
![Page 10: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/10.jpg)
本当に大事なことなので...
head
tail
init
last
![Page 11: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/11.jpg)
リストの操作(2)
length
null
reverse
take
drop
maximum
minimum
elem
![Page 12: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/12.jpg)
Range
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # => ugly!
[1..10] # => cool!
[‘a’..’z’] # => “abcdefghijklmnopqrstuvwxyz”
[2, 4..2 * 9] # => [2, 4, 6, 8, 10, 12, 14, 16, 18]
[1..] # => [1, 2, 3, 4, 5.....]
[2, 4..] # => 2の倍数の無限リスト
take 12 (cycle "hoge ") # => "hoge hoge ho"
take 3 (repeat 5) # => [5, 5, 5]
replicate 3 5 # => [5, 5, 5]
![Page 13: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/13.jpg)
リスト内包表記
[1 | _ <- [1, 2, 3], _ <- [4, 5, 6]]
[[x * 2 | x <- xs] | xs <- [[1,2,3], [4,5,6], [7,8,9]]]
[x * 2 | x <- [1..10]][x * 2 | x <- [1..10], x > 5]
ジェネレータ フィルタ
![Page 14: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/14.jpg)
タプル
複数の違う型の要素をまとめて1つの値として使う
(“Mountain Lion”, 10.8)
違う型を入れられる
固定長
zip
zip [1, 2, 3] [4, 5, 6] # => [(1, 4), (2, 5), (3, 6)]
zip [1,2,3,4] (repeat "hoge") # => [(1,"hoge"),(2,"hoge"),(3,"hoge"),(4,"hoge")]
![Page 15: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/15.jpg)
型
型宣言
zip :: [a] -> [b] -> [(a, b)]
型にあってないものはコンパイルできない
型推論
コンパイラが予想して補完してくれてた
read “5” みたいなのは実行時まで推論できない
read “5” :: Int のように明示してやる
-> Haskell ではコンパイル時にすべての型が自明
![Page 16: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/16.jpg)
型変数
オブジェクト指向のクラス...ではない
ジェネリクスみたいなもの
型クラスは抽象的なインターフェース
型は複数の型クラスのインスタンスである
(==) :: Eq a => a -> a -> Bool型変数
![Page 17: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/17.jpg)
代表的な型クラス
Eq
Ord
Show
Read
Enum
Bounded
Num
Floating
Integral
![Page 18: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/18.jpg)
ここまでで質問ある方言いたいことのある方
どうぞ!
![Page 19: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/19.jpg)
[today exercise1]
以下の関数を自分で定義してみる
null
sum
product
elem
python のスライスを実装する
ex) slice 2 5 [1..7] # => [3, 4, 5]
![Page 20: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/20.jpg)
[today exercise2]
フィボナッチ数列のn番目の数列を返す関数を作る
FizzBuzzの n 項目から m 項目までのリストを返す関数を作る
ex) fizzBuzz 50 100 # => 50番目から100番目までを返す
fizzBuzz :: Int -> Int -> [[Char]]
![Page 21: すごいHaskell読書会#1 in 大阪](https://reader031.vdocuments.pub/reader031/viewer/2022020116/55992ac21a28ab31298b47db/html5/thumbnails/21.jpg)
[today exercise3]
3けたの整数のうち、次の条件を満たすものを「良い整数」とよぶことにします。
条件:3けたの整数を2つの整数に分けてその和を考えると、常にもとの整数の約数になっている。
(例)330は3と30に分けても、33と0に分けても和が330の約数になっています。このため、330は「良い整数」となります。ですが、702は7と02に分けた場合は約数になりますが、70と2に分けてしまうと約数になりません。よって、702は「良い整数」ではありません。
一の位が0でない「良い整数」を4個求めなさい。