Skip to content

Instantly share code, notes, and snippets.

@meganehouser
Last active April 3, 2019 15:17
Show Gist options
  • Save meganehouser/5ed7b9c795f96d57b895c82b586997cd to your computer and use it in GitHub Desktop.
Save meganehouser/5ed7b9c795f96d57b895c82b586997cd to your computer and use it in GitHub Desktop.
MeguroLYAHFGG#2(すごいHaskell本を原書で読む会 )のノート。@nnm_techさんがまとめたものに実行結果と補足を追加したものです。
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# An intro to lists"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4,8,15,16,23,42]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"let lostNumbers = [4,8,15,16,23,42]\n",
"lostNumbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- listは同じ型のデータをもつ\n",
"- \"hello\"`は `['h','e','l','l','o']`のシンタックスシュガー、文字列はlistなので"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4,8,15,16,23,42]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"lostNumbers' = [4,8,15,16,23,42]\n",
"lostNumbers'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"GHC 8からはghciでもletが不要になった。\n",
"\n",
"## リスト演算子"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,2,3,4,9,10,11,12]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"hello world\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"woot\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[1,2,3,4] ++ [9,10,11,12]\n",
"\"hello\" ++ \" \" ++ \"world\"\n",
"['w','o'] ++ ['o','t']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `++` list結合\n",
" - 2つのリストを結合するとき、Haskellは内部で `++`の左側のリストを走査するので、大きなリストを扱うときは注意"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"ASMALL CAT\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[5,1,2,3,4,5]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"'A':\"SMALL CAT\"\n",
"5:[1,2,3,4,5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `:` 先頭に要素を追加\n",
" - `[1,2,3]`は `1:2:3:[]`のシンタックスシュガー\n",
" \n",
" Note: `[]`, `[[]]`, `[[],[],[]]`は全て異なるものである最初は空リスト、2番目は空リストを含むリスト、最後は3つの空リストを含むリスト"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'B'"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"33.2"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\"Steve Buscemi\" !! 6\n",
"[9.4,33.2,96.2,11.2,23.25] !! 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `!!` 特定のindexの要素を取りだす"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3],[1,1,1,1]]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[[6,6,6],[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[1,2,2,3,4]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"let b = [[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]\n",
"b\n",
"b ++ [[1,1,1,1]] \n",
"[6,6,6]:b\n",
"b !! 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- listは入れ子にすることもできる\n",
"- list中に複数のリストを保持するとき、長さが異なってもいいが内包する各リストの型が異なることは許されない"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[3,2,1] > [2,1,0] \n",
"[3,2,1] > [2,10,100]\n",
"[3,4,2] > [3,4]\n",
"[3,4,2] > [2,4]\n",
"[3,4,2] == [3,4,2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `<`, `<=`, `>` と `>=` 辞書的な比較\n",
" - 先頭要素を比較していき、もし同じ値の場合次の要素の比較をする\n",
"\n",
"\n",
"## list関数"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"head [5,4,3,2,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `head` 先頭要素を返す"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4,3,2,1]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tail [5,4,3,2,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `tail` 先頭要素以外のリストを返す"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"last [5,4,3,2,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `last` 末尾要素を返す"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5,4,3,2]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"init [5,4,3,2,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `init` 末尾要素以外のリストを返す"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>/* Styles used for the Hoogle display in the pager */\n",
".hoogle-doc {\n",
"display: block;\n",
"padding-bottom: 1.3em;\n",
"padding-left: 0.4em;\n",
"}\n",
".hoogle-code {\n",
"display: block;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"}\n",
".hoogle-text {\n",
"display: block;\n",
"}\n",
".hoogle-name {\n",
"color: green;\n",
"font-weight: bold;\n",
"}\n",
".hoogle-head {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-sub {\n",
"display: block;\n",
"margin-left: 0.4em;\n",
"}\n",
".hoogle-package {\n",
"font-weight: bold;\n",
"font-style: italic;\n",
"}\n",
".hoogle-module {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-class {\n",
"font-weight: bold;\n",
"}\n",
".get-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"display: block;\n",
"white-space: pre-wrap;\n",
"}\n",
".show-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"margin-left: 1em;\n",
"}\n",
".mono {\n",
"font-family: monospace;\n",
"display: block;\n",
"}\n",
".err-msg {\n",
"color: red;\n",
"font-style: italic;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"display: block;\n",
"}\n",
"#unshowable {\n",
"color: red;\n",
"font-weight: bold;\n",
"}\n",
".err-msg.in.collapse {\n",
"padding-top: 0.7em;\n",
"}\n",
".highlight-code {\n",
"white-space: pre;\n",
"font-family: monospace;\n",
"}\n",
".suggestion-warning { \n",
"font-weight: bold;\n",
"color: rgb(200, 130, 0);\n",
"}\n",
".suggestion-error { \n",
"font-weight: bold;\n",
"color: red;\n",
"}\n",
".suggestion-name {\n",
"font-weight: bold;\n",
"}\n",
"</style><span class='err-msg'>Prelude.head: empty list</span>"
],
"text/plain": [
"Prelude.head: empty list"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"head []\n",
"tail []\n",
"last []\n",
"init []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 上記関数、空リストのときにエラーが起きる点注意"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"length [5,4,3,2,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `length` リストの長さを返す"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>/* Styles used for the Hoogle display in the pager */\n",
".hoogle-doc {\n",
"display: block;\n",
"padding-bottom: 1.3em;\n",
"padding-left: 0.4em;\n",
"}\n",
".hoogle-code {\n",
"display: block;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"}\n",
".hoogle-text {\n",
"display: block;\n",
"}\n",
".hoogle-name {\n",
"color: green;\n",
"font-weight: bold;\n",
"}\n",
".hoogle-head {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-sub {\n",
"display: block;\n",
"margin-left: 0.4em;\n",
"}\n",
".hoogle-package {\n",
"font-weight: bold;\n",
"font-style: italic;\n",
"}\n",
".hoogle-module {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-class {\n",
"font-weight: bold;\n",
"}\n",
".get-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"display: block;\n",
"white-space: pre-wrap;\n",
"}\n",
".show-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"margin-left: 1em;\n",
"}\n",
".mono {\n",
"font-family: monospace;\n",
"display: block;\n",
"}\n",
".err-msg {\n",
"color: red;\n",
"font-style: italic;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"display: block;\n",
"}\n",
"#unshowable {\n",
"color: red;\n",
"font-weight: bold;\n",
"}\n",
".err-msg.in.collapse {\n",
"padding-top: 0.7em;\n",
"}\n",
".highlight-code {\n",
"white-space: pre;\n",
"font-family: monospace;\n",
"}\n",
".suggestion-warning { \n",
"font-weight: bold;\n",
"color: rgb(200, 130, 0);\n",
"}\n",
".suggestion-error { \n",
"font-weight: bold;\n",
"color: red;\n",
"}\n",
".suggestion-name {\n",
"font-weight: bold;\n",
"}\n",
"</style><div class=\"suggestion-name\" style=\"clear:both;\">Evaluate</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">null []</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">True</div></div>"
],
"text/plain": [
"Line 1: Evaluate\n",
"Found:\n",
"null []\n",
"Why not:\n",
"True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"null [1,2,3]\n",
"null []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `null` リストが空リストの場合 `True`を返し、そうで無い場合 `False` を返"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[1,2,3,4,5]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"reverse [5,4,3,2,1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `reverse` リストの要素を逆順にして返す"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>/* Styles used for the Hoogle display in the pager */\n",
".hoogle-doc {\n",
"display: block;\n",
"padding-bottom: 1.3em;\n",
"padding-left: 0.4em;\n",
"}\n",
".hoogle-code {\n",
"display: block;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"}\n",
".hoogle-text {\n",
"display: block;\n",
"}\n",
".hoogle-name {\n",
"color: green;\n",
"font-weight: bold;\n",
"}\n",
".hoogle-head {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-sub {\n",
"display: block;\n",
"margin-left: 0.4em;\n",
"}\n",
".hoogle-package {\n",
"font-weight: bold;\n",
"font-style: italic;\n",
"}\n",
".hoogle-module {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-class {\n",
"font-weight: bold;\n",
"}\n",
".get-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"display: block;\n",
"white-space: pre-wrap;\n",
"}\n",
".show-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"margin-left: 1em;\n",
"}\n",
".mono {\n",
"font-family: monospace;\n",
"display: block;\n",
"}\n",
".err-msg {\n",
"color: red;\n",
"font-style: italic;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"display: block;\n",
"}\n",
"#unshowable {\n",
"color: red;\n",
"font-weight: bold;\n",
"}\n",
".err-msg.in.collapse {\n",
"padding-top: 0.7em;\n",
"}\n",
".highlight-code {\n",
"white-space: pre;\n",
"font-family: monospace;\n",
"}\n",
".suggestion-warning { \n",
"font-weight: bold;\n",
"color: rgb(200, 130, 0);\n",
"}\n",
".suggestion-error { \n",
"font-weight: bold;\n",
"color: red;\n",
"}\n",
".suggestion-name {\n",
"font-weight: bold;\n",
"}\n",
"</style><div class=\"suggestion-name\" style=\"clear:both;\">Take on a non-positive</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">take 0 [6, 6, 6]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[]</div></div>"
],
"text/plain": [
"Line 1: Take on a non-positive\n",
"Found:\n",
"take 0 [6, 6, 6]\n",
"Why not:\n",
"[]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[5,4,3]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[3]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[1,2]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"take 3 [5,4,3,2,1]\n",
"take 1 [3,9,3]\n",
"take 5 [1,2]\n",
"take 0 [6,6,6]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `take` 先頭から指定した数の要素数のリストを返す。0のときは空リストを返す"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,5,6]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[1,2,3,4]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"drop 3 [8,4,2,1,5,6]\n",
"drop 0 [1,2,3,4]\n",
"drop 100 [1,2,3,4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `drop` 先頭から指定した数の要素数を取り除いたリストを返す。指定した数がリストの長さ以上の場合は空リストを返す"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"minimum [8,4,2,1,5,6]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `minimum` リストの最小値を返す"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"maximum [1,9,2,3,4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `maximum` リストの最大値を返す"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sum [5,2,1,6,3,2,5,7] "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `sum` リストの要素の和を返す"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"24"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"product [6,2,1,2]\n",
"product [1,2,5,6,7,9,2,0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" `product` リストの要素の積を返す"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"4 `elem` [3,4,5,6]\n",
"10 `elem` [3,4,5,6]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `elem` リストに指定した値が入っている場合 `True`を返し、そうで無い場合 `False` を返す\n",
" - よく *infix* functionとして呼ばれる"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Texas ranges"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"abcdefghijklmnopqrstuvwxyz\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[1..20]\n",
"['a'..'z']\n",
"['A'..'Z']\n",
"['A'..'z']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- rangeは等差数列で要素が列挙可能なlistを作成する一つの方法"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2,4,6,8,10,12,14,16,18,20]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[3,6,9,12,15,18]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[2,4..20]\n",
"[3,6..20]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- rangeはstepを指定することもできる\n",
"- 2のべき乗みたいな等比数列はうまく生成できない"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[0.1, 0.3..1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 浮動小数点のrangeは丸め誤差がでるので使わない方がよい"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 無限リスト\n",
"- 上限を指定しないことで無限listを作成することができる\n",
"- Haskellは遅延評価と無限listを利用してlist生成のスマートな書き方ができる\n",
" - not smart `[13,26..24*13]`\n",
" - smart `take 24 [13,26..]`\n",
" - Haskellは即時に無限リストを評価することはない\n",
"\n",
"### 無限listを生成する関"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,2,3,1,2,3,1,2,3,1]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"LOL LOL LOL \""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"take 10 (cycle [1,2,3])\n",
"take 12 (cycle \"LOL \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `cycle` 指定したリストの要素を繰り返す無限listを返す"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>/* Styles used for the Hoogle display in the pager */\n",
".hoogle-doc {\n",
"display: block;\n",
"padding-bottom: 1.3em;\n",
"padding-left: 0.4em;\n",
"}\n",
".hoogle-code {\n",
"display: block;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"}\n",
".hoogle-text {\n",
"display: block;\n",
"}\n",
".hoogle-name {\n",
"color: green;\n",
"font-weight: bold;\n",
"}\n",
".hoogle-head {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-sub {\n",
"display: block;\n",
"margin-left: 0.4em;\n",
"}\n",
".hoogle-package {\n",
"font-weight: bold;\n",
"font-style: italic;\n",
"}\n",
".hoogle-module {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-class {\n",
"font-weight: bold;\n",
"}\n",
".get-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"display: block;\n",
"white-space: pre-wrap;\n",
"}\n",
".show-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"margin-left: 1em;\n",
"}\n",
".mono {\n",
"font-family: monospace;\n",
"display: block;\n",
"}\n",
".err-msg {\n",
"color: red;\n",
"font-style: italic;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"display: block;\n",
"}\n",
"#unshowable {\n",
"color: red;\n",
"font-weight: bold;\n",
"}\n",
".err-msg.in.collapse {\n",
"padding-top: 0.7em;\n",
"}\n",
".highlight-code {\n",
"white-space: pre;\n",
"font-family: monospace;\n",
"}\n",
".suggestion-warning { \n",
"font-weight: bold;\n",
"color: rgb(200, 130, 0);\n",
"}\n",
".suggestion-error { \n",
"font-weight: bold;\n",
"color: red;\n",
"}\n",
".suggestion-name {\n",
"font-weight: bold;\n",
"}\n",
"</style><div class=\"suggestion-name\" style=\"clear:both;\">Use replicate</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">take 10 (repeat 5)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">replicate 10 5</div></div>"
],
"text/plain": [
"Line 1: Use replicate\n",
"Found:\n",
"take 10 (repeat 5)\n",
"Why not:\n",
"replicate 10 5"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[5,5,5,5,5,5,5,5,5,5]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"take 10 (repeat 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `repeat` 指定した値を繰り返す無限listを返す"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5,5,5,5,5,5,5,5,5,5]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"replicate 10 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `take`と `repeat`を使う場合、 `replicate`関数を用いて簡単に書ける"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## I'm a list comprehension"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2,4,6,8,10,12,14,16,18,20]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[x*2 | x <- [1..10]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- listだけでは表現しにくい複雑な集合を取りたい場合にリスト内包表記を記述する"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[12,14,16,18,20]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[x*2 | x <- [1..10], x >= 6]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[52,59,66,73,80,87,94]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[ x | x <- [50..100], x `mod` 7 == 3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 述語によって`filtering`できる"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"BOOM!\",\"BOOM!\",\"BANG!\",\"BANG!\"]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"boomBangs xs = [ if x < 10 then \"BOOM!\" else \"BANG!\" | x <- xs, odd x]\n",
"boomBangs [7..13]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10,11,12,14,16,17,18,20]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[ x | x <- [10..20], x /= 13, x /= 15, x /= 19]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 述語はカンマ区切りで複数指定できる"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[16,20,22,40,50,55,80,100,110]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[ x*y | x <- [2,5,10], y <- [8,10,11]]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[55,80,100,110]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"lazy hobo\",\"lazy frog\",\"lazy pope\",\"grouchy hobo\",\"grouchy frog\",\"grouchy pope\",\"scheming hobo\",\"scheming frog\",\"scheming pope\"]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"let nouns = [\"hobo\",\"frog\",\"pope\"]\n",
"let adjectives = [\"lazy\",\"grouchy\",\"scheming\"]\n",
"[adjective ++ \" \" ++ noun | adjective <- adjectives, noun <- nouns]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- ソースとなるリストも複数指定可能"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"length' xs = sum [1 | _ <- xs]\n",
"length' [5,4,2,1]\n",
"length' \"hello\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- リストからとってきた変数を利用しないときは `_`と書く"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"HA\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"\"ILIKEFROGS\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]\n",
"removeNonUppercase \"Hahaha! Ahahaha!\"\n",
"removeNonUppercase \"IdontLIKEFROGS\""
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[2,2,4],[2,4,6,8],[2,4,2,6,2,6]]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"let xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]\n",
"[ [ x | x <- xs, even x ] | xs <- xxs]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [集合 - Wikipedia](https://ja.wikipedia.org/wiki/%E9%9B%86%E5%90%88#%E8%A8%98%E6%B3%95)\n",
"\n",
"- [補足]Pythonにもリスト内包表記がある(Haskellを参考にした)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"codemirror_mode": "ihaskell",
"file_extension": ".hs",
"name": "haskell",
"pygments_lexer": "Haskell",
"version": "8.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment