Created
May 19, 2019 05:06
-
-
Save meganehouser/c4df42ddb94296b44d9dc057e3d383d3 to your computer and use it in GitHub Desktop.
Meguro.LYAHFGG#3 Note.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Tuples\n", | |
"tupleはlistと似ているが、複数の値を一つの値に保持する" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1,2)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"(1,\"Hello\")" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"(1, 2)\n", | |
"(1, \"Hello\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- tupleはどれくらい要素があるべきで、それぞれの要素の型がどうあるべきかわかっているときに使う\n", | |
"- tupleは異なった型の要素をもつことができる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"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'><interactive>:1:10: error:<br/> • Couldn't match expected type ‘(a, b)’ with actual type ‘(Integer, Integer, Integer)’<br/> • In the expression: (8, 11, 5)<br/> In the expression: [(1, 2), (8, 11, 5), (4, 5)]<br/> In an equation for ‘it’: it = [(1, 2), (8, 11, 5), (4, 5)]<br/> • Relevant bindings include it :: [(a, b)] (bound at <interactive>:1:1)</span>" | |
], | |
"text/plain": [ | |
"<interactive>:1:10: error:\n", | |
" • Couldn't match expected type ‘(a, b)’ with actual type ‘(Integer, Integer, Integer)’\n", | |
" • In the expression: (8, 11, 5)\n", | |
" In the expression: [(1, 2), (8, 11, 5), (4, 5)]\n", | |
" In an equation for ‘it’: it = [(1, 2), (8, 11, 5), (4, 5)]\n", | |
" • Relevant bindings include it :: [(a, b)] (bound at <interactive>:1:1)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"[(1, 2), (8, 11, 5), (4, 5)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"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'><interactive>:1:3: error:<br/> • Could not deduce (Num String) arising from the literal ‘1’<br/> from the context: Num b bound by the inferred type of it :: Num b => [(String, b)] at <interactive>:1:1-20<br/> • In the expression: 1<br/> In the expression: (1, 2)<br/> In the expression: [(1, 2), (\"One\", 2)]</span>" | |
], | |
"text/plain": [ | |
"<interactive>:1:3: error:\n", | |
" • Could not deduce (Num String) arising from the literal ‘1’\n", | |
" from the context: Num b bound by the inferred type of it :: Num b => [(String, b)] at <interactive>:1:1-20\n", | |
" • In the expression: 1\n", | |
" In the expression: (1, 2)\n", | |
" In the expression: [(1, 2), (\"One\", 2)]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"[(1, 2), (\"One\", 2)]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"tupleが入ったlistを作るとき、以下の場合にエラーがおきる\n", | |
"- tupleの要素数が異なる\n", | |
"- 要素数が同じでもtupleの型の組み合わせが異なる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(\"Christopher\",\"Walken\",55),(\"Tom\",\"York\",50)]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"[(\"Christopher\", \"Walken\", 55), (\"Tom\", \"York\", 50)]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- tpuleは様々なデータを表現するためにも使用できる。\n", | |
"- 例えば上記は、人物の情報(First name, Last name, age)をtupleで表現している" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"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;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(1)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">1</div></div>" | |
], | |
"text/plain": [ | |
"Line 1: Redundant bracket\n", | |
"Found:\n", | |
"(1)\n", | |
"Why not:\n", | |
"1" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>(1) :: forall p. Num p => p</span>" | |
], | |
"text/plain": [ | |
"(1) :: forall p. Num p => p" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"(1)\n", | |
":t (1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 要素が1個のtupleは作ることができない。\n", | |
"- 要素の値そのものと同等であり、tupleにする価値がないため" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### tupleに関する関数\n", | |
"#### ペア(2要素のみ)のtupleでしか使えない関数" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"8" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"\"Wow\"" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"fst (8, 11)\n", | |
"fst (\"Wow\", False)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- **fst**\n", | |
" - 1個目の要素を返す" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"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\">snd (\"Wow\", False)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">False</div></div>" | |
], | |
"text/plain": [ | |
"Line 1: Evaluate\n", | |
"Found:\n", | |
"snd (\"Wow\", False)\n", | |
"Why not:\n", | |
"False" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"11" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"snd (8, 11)\n", | |
"snd (\"Wow\", False)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- **snd**\n", | |
" - 2個目の要素を返す" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### zip" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1,5),(2,5),(3,5),(4,5),(5,5)]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"zip [1,2,3,4,5] [5,5,5,5,5] " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1,\"one\"),(2,\"two\"),(3,\"three\"),(4,\"four\"),(5,\"five\")]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"zip [1 .. 5] [\"one\", \"two\", \"three\", \"four\", \"five\"] " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 2つのlistからindexが同じ要素のペアのtuple listを返す。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(5,\"im\"),(3,\"a\"),(2,\"turtle\")]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"zip [5,3,2,6,2,7,2,5,4,6,6] [\"im\",\"a\",\"turtle\"] " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 要素数が異なる場合、要素数が少ないlistに長さが合わせられる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1,\"apple\"),(2,\"orange\"),(3,\"cherry\"),(4,\"mango\")]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"zip [1..] [\"apple\", \"orange\", \"cherry\", \"mango\"] " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 要素数が少ない方に合わせられるので、無限リストとも組み合わせられる" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### リスト内包表記でtupleのリストを作成する\n", | |
"- 全ての辺の大きさが10以下で、全周の大きさが24の直角三角形のリストを求めたい" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 最初に各辺が10以下の三角形の組み合わせを全て生成するリスト内包表記を書いてみる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(3,4,5),(6,8,10)]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2] \n", | |
"rightTriangles" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"続いて、3平方の定理を使って直角三角形として成立する組み合わせのみ抽出するフィルターを追加する" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(6,8,10)]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24] \n", | |
"rightTriangles'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"最後に、辺の合計が24である組み合わせのみ抽出するフィルターを追加する\n", | |
"\n", | |
"- 関数型プログラミング言語では、ある一つの集合からはじめて、それから変形やt輸出を追加して問題を解決することは一般的なパターンである" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 3. Types and Typeclsses\n", | |
"- Haskellは静的型付けシステムを持っており、コンパイル時に全ての式の型を知っているため、コードをより安全にすることができる\n", | |
" - 実行時にクラッシュする代わりにコンパイル時にエラーを検知できる\n", | |
"- JavaやPascalと違いHaskellには型推論を持っている\n", | |
"- 型(Type)とは全ての式が持っているラベルのようなもの" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"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='get-type'>'a' :: Char</span>" | |
], | |
"text/plain": [ | |
"'a' :: Char" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>True :: Bool</span>" | |
], | |
"text/plain": [ | |
"True :: Bool" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>\"Hello!\" :: [Char]</span>" | |
], | |
"text/plain": [ | |
"\"Hello!\" :: [Char]" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>(True, 'a') :: (Bool, Char)</span>" | |
], | |
"text/plain": [ | |
"(True, 'a') :: (Bool, Char)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>4 == 5 :: Bool</span>" | |
], | |
"text/plain": [ | |
"4 == 5 :: Bool" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
":t 'a'\n", | |
":t True\n", | |
":t \"Hello!\"\n", | |
":t (True, 'a')\n", | |
":t 4 == 5" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- GHCIでは`:t`コマンドで式の型を知ることができる\n", | |
" - 「`:t`コマンドに渡した式 `::`型」 の形式で表示される\n", | |
" - 明示的な型名は先頭文字が大文字" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 関数の型\n", | |
"- 関数を書く時、型定義を明示的に与えることもできる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"removeNonUppercase :: [Char] -> [Char]\n", | |
"removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']] " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- [Char]はStringという型シノニムを持っているため以下のようにも定義できる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"removeNonUppercase :: [Char] -> [Char]\n", | |
"removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']] " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- [補足]GHCは文字列を扱う型としてString, ByteString, Textの3種類の型を持っている\n", | |
" - [Haskell Tips (文字列編) - りんごがでている](http://bicycle1885.hatenablog.com/entry/2012/12/24/234707)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"addThree :: Int -> Int -> Int -> Int \n", | |
"addThree x y z = x + y + z " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"addThree 1 2 3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- 関数が複数の引数を取る場合は->で区切って書く\n", | |
" - 引数と戻り値の型には区別はなく、最後の型が戻り値の型になる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"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='get-type'>plus :: Int -> Int -> Int</span>" | |
], | |
"text/plain": [ | |
"plus :: Int -> Int -> Int" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>addOne :: Int -> Int</span>" | |
], | |
"text/plain": [ | |
"addOne :: Int -> Int" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"plus :: Int -> Int -> Int\n", | |
"plus x y = x + y\n", | |
":t plus\n", | |
"\n", | |
"addOne = plus 1\n", | |
":t addOne" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"addOne 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- [補足] Haskellの関数はデフォルトでカリー化されているため\n", | |
" - [カリー化と部分適用(JavaScriptとHaskell) - Qiita](https://qiita.com/7shi/items/a0143daac77a205e7962)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 代表的な型の概要\n", | |
"- ***Int***\n", | |
" - 整数\n", | |
" - 値の範囲(最小値と最大値)を持つ" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"factorial :: Integer -> Integer \n", | |
"factorial n = product [1..n]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"30414093201713378043612608166064768844377641568960512000000000000" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"factorial 50" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- **Integer**\n", | |
" - 整数\n", | |
" - 制限範囲を持たず巨大な整数を扱うことができる" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"circumference :: Float -> Float \n", | |
"circumference r = 2 * pi * r " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"25.132742" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"circumference 4.0" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- ***Float***\n", | |
" - 単精度浮動小数点数" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"circumference' :: Double -> Double \n", | |
"circumference' r = 2 * pi * r " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"25.132741228718345" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"circumference' 4.0 " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- ***Double***\n", | |
" - 倍精度浮動小数点数" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- ***Bool***\n", | |
" - Bool型。TrueとFalse\n", | |
"- ***Char***\n", | |
" - 文字" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"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='get-type'>() :: ()</span>" | |
], | |
"text/plain": [ | |
"() :: ()" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"()\n", | |
":t ()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"空のtupleは一個の値のみ持つ" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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