ð¡ é次ãéžæãå埩ã®3èŠçŽ ã§ãã¥ãŒãªã³ã°å®å š
é¢æ°åã³ãŒãã«ãããé次ãéžæãå埩ã®3èŠçŽ ã玹ä»ãã
ãã¥ãŒãªã³ã°å®å šãšæ§é åå®ç
- é¢æ°åããã°ã©ãã³ã° ãšã¯ããã®ããã«æ°åŒãšãããããã¯ãã€ãªãåãããŠçµã¿ããã ã ã ã§ããã¶ãã£ãŠããŸãææ³
- ããã°ã©ãã³ã°èšèªã§ããªãã§ãã§ããããã«ãªããããšã ãã¥ãŒãªã³ã°å®å š ãšèšã
- ãã¥ãŒãªã³ã°å®å šã«ãªãããã«ã¯ é次 ã éžæ ã å埩 ã® ïŒèŠçŽ ããã§ããã°è¯ã ãšãã æ§é åå®ç ããã
é次ãéžæãå埩ã®3èŠçŽ ã¯é¢æ°åã³ãŒãã®åŒã§è¡šçŸå¯èœ
- é次ãéžæãå埩ã®3èŠçŽ ã¯çŽ æŽã«ã¿ãŠåœä»€åã®ãããŒã®æŠå¿µ
- é¢æ°åã³ãŒãã¯ãæ°åŠã®åŒãªã®ã§ã é次ãéžæãå埩ã®3èŠçŽ ãçŽ æŽãªãããŒã®å¶åŸ¡æ§é ã§ã¯ãªããåŒã§è¡šçŸããã
é次åŠçã®ããã®åŒ
- æ°åŠã®åŒã¯ãæŒç®åªå é äœã ãã§ãããã§ãæŒç®ã®é çªãã³ã³ãããŒã«ã§ãã ã®ã§ã é¢æ°åã³ãŒãããšãããã代æ°æ§é ã«ã¯ é次 ã®æ©èœããããã°ã©ãã³ã°èšèªãèšèšãã人éããããããšèããå¿ èŠãããªãããããããåãã£ãŠãã
- Haskellã®ãããªçŽç²é¢æ°åèšèªã§ã¯ãé次åŠçããåªå é äœãããæ°åŒã®æŒç®ã§åŠçããããšããã¹ã¿ã€ã«ã§åŸ¹åºããŠãã
- é¢æ°åããã°ã©ãã³ã°ãšã¯æ°åŠãã®ãã® ãšããååååã«å¯èœãªéãå¿ å®ã§çŽç²ã§ããããšè¿œæ±ããŠããã®ããHaskellã®ãããªçŽç²é¢æ°åèšèªã§ããã®ããã«ãé次åŠçãåŒã®æŒç®ã代æ°æ§é ã§è¡šçŸããŠãã
Haskellã®ã³ãŒãã®äºé æŒç®åãšIO Monad
ä»ã®ãšãããåèŠã§ã¯Haskellã®ã³ãŒãã¯ãããã«ããã ãã
ããã§ã do
ãå±éãããšãã«çŸãã
>>
ã >>=
ãããã®é 次åŠçã®ããã®æŒç®ã§äœ¿ãããŠããäºé
æŒç®å
ãã®ãããªé 次åŠçã®ããã®åŒãæŒç®ãHaskellã§ã¯ã IO Monad ãšåŒãã§ããããåŸã§è©³ãã玹ä»ãã
- ãã ããé次åŠçãIO Monad ã®åŒã§è¡šçŸããã³ãŒããæžãããšããã®ã¯äººéã«ãšã£ãŠã¯ãªããªãçŽæçã§ã¯ãªã倧å€ãªã®ã§ãHaskellã§ã¯æåã«ç€ºããããšãã
do
ããŒã¯ãŒãã䜿ã£ãSyntaxSugarã䜿ããã
- ãã®ä»å€ãã®çŽç²ã§ã¯ãªãé¢æ°åèšèªã§ã¯ãããã¯é©åºŠã«ç·©ããŠãããåŒãè£åŽã«ãã
do
ãããã°ã©ãã«æžãããããšã¯ãããå··ã®åœä»€åèšèªãšåãããã«ãåã«ã³ãŒãã®äžããäžã«åæãããããšãèš±å¯ããŠãã
- çŽç²é¢æ°åèšèªHaskellãšä»ã®çŽç²ã§ã¯ãªãé¢æ°åèšèªã®æã倧ããªéãã¯ãé次åŠçã®è¡šçŸã«IO MonadãšããæŒç®åŒã䜿ããªããã°ãããªãã®ããããã§ã¯ãªããã
- IO Monadã¯ããã¹ãŠãæŒç®ã§è¡ããšããåçååãè¿œæ±ããŠããHaskellã®ãããªçŽç²é¢æ°åèšèªã«ãšã£ãŠã¯ãé次åŠçãå®çŸããããã«å¿ é ã ããçŽç²ã§ãªãé¢æ°åèšèªã§ã¯ãåœä»€åã¹ã¿ã€ã«ã§ã³ãŒãã®äžäžã®é åºã§åŒã䞊ã¹ãã ãã§é次åŠçãå®çŸã§ããã®ã§ãããã䜿ãå¿ èŠããªã
- ããã§ã¯çŽç²é¢æ°åèšèªHaskellã§ã¯ãªãã F#ãšTypeScriptãé¢æ°åèšèªãšããŠå©çšããã®ã§ãIO Monadã䜿ã£ãæŒç®åŒã®ã¹ã¿ã€ã«ã§é次åŠçã®ã³ãŒããæžãããšã¯ãªã
- ãã ããMonadã®åºç€ãã§ããŠããã¯IO Monadãåé¢æ°åèšèªã«å®è£ ããŠæåããšãã¥ã¬ãŒã·ã§ã³ããããšã¯é£ãããªãã®ã§ãããã¯åŸã§ææŠããŠã¿ãã
éžæã®ããã®åŒ
- éžæã¯åã« æ¡ä»¶åŒ ãã€ãã ãé¢æ°åã³ãŒãã®åŒã¯ãããŒã§ã¯ãªãã®ã§åå²ã§ã¯ãªãæ°åŠã§ãããšããã® ãå Žååããã®éžæ ã
- æ¡ä»¶åŒã¯ãåŒãªã®ã§ã å¿
ãå€ãæã£ãŠããïŒå€ãè¿ãïŒ ã åœä»€åœ¢ãããŒã®æ§æã®ãšãã®ããã«
else
ãçç¥ããããšã¯ã§ããªã
- Haskellã§ã¯ãåœä»€åã®æ¡ä»¶åå²ã®ããã«èŠãã
if
then
else
ããŒã¯ãŒãã§ãããã¹ãŠåŒã«ãªã£ãŠãã
- Rustã®ãããªé¢æ°åã¹ã¿ã€ã«ãåãå ¥ããã¢ãã³ãªèšèªãæ¡ä»¶åŒã«ãªã£ãŠãã
- Verseãæ¡ä»¶åŒ
- F#ãæ¡ä»¶åŒ
- F#ãHaskellããããRustãªã©ã
if
ãšelse
ã®ããŒã¯ãŒãããã®ãŸãŸèžè¥²ããŠããããCç³»ã®IF-ELSEæ§æã§ãªãã C èšèªã® äžé æŒç®åã«ããæŒç®ãšãŸã£ããåãã§ããïŒæ¡ä»¶ïŒåŒ ã«ãªã£ãŠãã
- JavaScriptãå«ã Cèšèªç³»ã®ããã°ã©ãã³ã°èšèªã§ã¯äžé æŒç®å ãçšæãããŠããããšãå€ãããã㯠æ¡ä»¶åŒ ã
äžé æŒç®å
condition ? exprIfTrue : exprIfFalse
let x = true;
let result =
x == true
? "x is true!"
: "x is false!";
// "x is true!"
- JavaScriptã§ã¯ããè€éãªæ¡ä»¶åå²ã®éã«ã
switch
-case
æ§æã䜿ãããããã¬ã é¢æ°åèšèªã§ã¯åŒ·åãªãã¿ãŒã³ããã ãšããä»çµã¿ããããF#ã§ã¯match
åŒã䜿ã ãJavaScriptã§ã¯èšèªã«ãã¿ãŒã³ãããã¯å«ãŸããŠããªãã®ã§ããµãŒãããŒãã£ã®NPMã©ã€ãã©ãªãªã©ã䜿ããããªã
æ¡ä»¶åŒ
if condition then exprIfTrue else exprIfFalse
let x = true
let result =
if x = true
then "x is true!"
else "x is false!"
// "x is true!"
ãã¿ãŒã³ããã
let x = 2
let result =
match x with
| 1 -> "one"
| 2 -> "two"
| _ -> "other"
// "two"
- ãã®ä»ã®ã»ãšãã©ã®JavaScriptãå«ãCèšèªç³»ã®ããã°ã©ãã³ã°èšèªã§ã¯ãåœä»€åã®
if
else
æ§æã§ãããåŒã«ã¯ãªã£ãŠããªããã€ãŸãå€ãæããªã
å埩ã®ããã®åŒ
åœä»€åã³ãŒããšé¢æ°åã³ãŒããæ¯èŒ
é¢æ°åã³ãŒãã® å埩ã®ããã®åŒ ã玹ä»ãã以åã«ããŸãåœä»€åã§ã©ãããã³ãŒããæžããŠããã確èªããŠãããšãããã¯ãã¡ãã for
ãšãããããŒã®å¶åŸ¡æ§æã䜿ã£ãã«ãŒã
é¢æ°åã³ãŒãã§ã¯äžèšã®ãããªç°¡æœãªã³ãŒããšãªã
åœä»€åã³ãŒãã§ã¯ããèšç®çµæããšãªã å€ sum
ãæå 0
ã«èšå®ãããŠããŠãããã« i
ãšããã«ãŠã³ã¿ãŒå€æ°ãããããã®2ã€ã®å€ãã«ãŒãæ¯ã«ã³ãã³ãå€ãå€åããŠããããšããå®ã¯éåžžã«è€éãªåŠçããã£ãŠãã
é¢æ°åã³ãŒãã§ã¯ããã®ãããªã«ãŒãã®ãããªãããŒã¯ååšããªããããã äºé
æŒç®ãããŠäžçºã§ãèšç®çµæã sum
ãæ±ãŸã£ãŠãã
ãªããããªããšãå¯èœãªã®ãïŒãšèšããšã.reduce
ãšããé¢æ°ãäºé
æŒç®ãšããŠèŠãã°æŒç®åãªã®ã ãããããé
åã®FoldïŒç³ã¿èŸŒã¿ïŒåŠçãããããšãã§ãããã
ã€ãŸããåœä»€åã³ãŒãã§ã¯ãå¶åŸ¡æ§æããã£ãŠã³ãŒããããã¯æåããæåŸãŸã§å šäœã§è€éãªããšãããã¹ã¿ã€ã«ã«å¯ŸããŠãé¢æ°åã³ãŒãã§ã¯ã1ã€ã®æŒç®åã«ããããå埩åŠçãè©°ã蟌ãã§ããŸã£ãŠãã
ãã®çµæãã³ãŒãã¯ç°¡æœãªäºé æŒç®ã«ãªããèŠéããããããã°ã®çºç確çã¯æ¿æžããã ãã
ã«ãŒãæ¯ã«å€ãå€ããå€æ°ãWatchããŠãããŒã®äžèº«ãããããæ€èšãã代ããã«ããããã®äºé æŒç®ã®åŒã¯åŠ¥åœãïŒããšããåé¡ã«ãªãã®ã§ããããã°ãç°¡æœã«ãªã
ãã®ãããªããšã以äžèª¬æããŠãã
- ãã¥ãŒãªã³ã°å®å šã«ãªãããã®æåŸã®èŠçŽ ã§ããå埩 ã ããã ãããã®é¢æ°åããã°ã©ãã³ã°ã®æç§æžçãªè§£èª¬ã§ã¯ ååž° ãå©çšãããšããããååž°ãšããã®ã¯ èªå·±åç § ã€ãŸãèªåã§èªåèªèº«ã®ããšãèšåãããããããšã
- ããšãã°ã
ãã0 ãŸã§ã®èªç¶æ°ãããã¶è¶³ã ããšããååž°ãå©çšããã³ãŒãã¯5 sum
èªèº«ãåç §ããsum
é¢æ°ãã€ãã£ãŠæžããrec
ããŒã¯ãŒãã§sum
ãèªå·±åç §ãããååž°é¢æ°ã§ããããšãã³ã³ãã€ã©ã«æ瀺ããŠãã
let rec sum =
fun n ->
if n = 0 then 0
else n + sum (n - 1)
let result = sum 5
// 15
- ååž°ã¯ã èªå·±åç §ããé¢æ° ã§ãã äžåç¹ã³ã³ãããŒã¿ïŒYã³ã³ãããŒã¿ãæåïŒãã©ã ãåŒã ãã§æžãã ãã€ãŸã ã©ã ãåŒã ãã§ãã¥ãŒãªã³ã°å®å šã«ãªã ã®ã§ãé¢æ°åããã°ã©ãã³ã°ã®çè«ã§ããèšåããã
- çè«çã«ã¯éèŠ ã§ãããšãã°ãã¡ã€ã«ã·ã¹ãã ã®ãã£ã¬ã¯ããªãªã©ååž°æ§é ã«ãªã£ãŠãã察象ãããã°ã©ãã³ã°ããéã«ã¯å¿ é ã ãããã以å€ã§ã¯ äžå¿ èŠã«è€éãªã³ãŒãã«ãªãããèšç®éãççºããããããã©ãŒãã³ã¹ã®åé¡ãããã®ã§ãèªå·±åç §ããé¢æ°ãå®çŸ©ããååž°ããã°ã©ãã³ã°ã¯é¿ããã»ããè¯ã ã代ããã«ãããŸã§ãã£ããšãã代æ°æ§é ã®ã³ãŒããæžã
- 2ã€ã®åŒïŒãããã¯ïŒã
ãšããæŒç®åã§ã€ãªãåãããããšãäœåºŠãç¹°ãè¿ããŠã¿ãããããã¯å埩ã+
-
+ +
æŒç®åã¯ãå·ŠçµåïŒå·Šããå³æ¹åãžæŒç®ãé²ãã§ããïŒãããã«ãŒã«ãªã®ã§ã ã¯ãæé»ã«ã0 + 1 + 2 ãšåãã«ãªã((0 + 1) + 2))
- äºé æŒç®ã®ç¹°ãè¿ãã¯ã æãç³ã¿ æ§é ã«ãªã£ãŠããããšãããã
çµåæ§
æãç³ã¿ã¯ãå¿ ããã å·Šããå³ ïŒ å·Šçµå ïŒãšã¯éããªããæ瀺çã«ãã«ãã³ãã€ããããšã§ã å³ããå·Š ïŒ å³çµå ïŒãžçµåé åºãå€ããããšãã§ãã
+
æŒç®ã§ãå·Šçµåãå³çµåãäž¡è
ã®çµæãåããªã®ã¯ã ãã€ãªããé çªã«ãã£ãŠçµæãå€ãããããªããšã¯ãªãããã€ãªããé çªã¯ã©ãã§ãè¯ãããšããæ§è³ª ïŒ çµåæ§ ããããããããã¯ãšãŠãéèŠãªæ§è³ªãªã®ã§åŸã§è©³ãã調ã¹ãã
ææ°æŒç®å **
ã®ããã«ãããã©ã«ã㧠å³çµå ã®æŒç®åãããããã®æŒç®åã¯å·Šããå
ã«æŒç®ãããšçµæã¯ç°ãªãã®ã§ã çµåæ§ ããªã
- é¢æ°åèšèªãªãã°ããã®ç¹°ãè¿ãïŒæãç³ã¿ïŒåŠçãããããã® Fold é¢æ°ãæäŸãããŠãã
- JavaScriptã§ã¯å·Šçµåã®æãç³ã¿ã¯ã
.reduce
ãå³çµåã®æãç³ã¿ã¯ã.reduceRight
ãšããååã®Array
ïŒé åïŒãªããžã§ã¯ãã«çŽã¥ããŠããé¢æ°ïŒã¡ãœããïŒã«ãªã£ãŠãã
let plus = (a,b) => a + b;
let sum = [0,1,2,3,4,5].reduce(plus);
console.log(sum); // 15
-
[0,1,2,3,4,5]
ãšããåŒïŒãããã¯ïŒ ãšplus
é¢æ°ãšããåŒïŒãããã¯ïŒ ãã€ãªãåãããã®ã.reduce
é¢æ°
- JavaScriptãšããèšèªã®ä»æ§äžãªããžã§ã¯ãæåã®ã¡ãœããè¡šèšã«ãªã£ãŠããããæ ¹æ¬çã«ã¯ãå·Šã«ããŒã¿ãå³ã«é¢æ°ããã®ïŒã€ãã€ãªãåããã äºé
æŒç®åãšããŠã®
.reduce
ãšæããããšãã§ãã
- ãã¡ãããã€ãªãåãããåŒïŒãããã¯ïŒã足ãç®ã§ãªãæãç®ã®é¢æ°ã«ããããšãã§ãã
- ãŸããåã®äºé
æŒç®åã¯ãã€ãã©ã€ã³æŒç®åã ã£ãããä»åã¯Foldç³»ã®(
.reduce
)æŒç®åã«åãæ¿ããããŠãã
- 2ã€ã®ãããã¯æ¥ç¶ïŒäºé æŒç®ïŒããã§ããã°ãèªç±ã«3ã€ä»¥äžæ¥ç¶ããŠãããã®ã§ã
-
plus
é¢æ°ã¯ãF#ã§ã¯çŽæ¥(+)
é¢æ°ãšããŠåãåºãããã[0;1;2;3;4;5]
ãšããåŒïŒãããã¯ïŒ ãš(+)
é¢æ°ãšããåŒïŒãããã¯ïŒ ãã€ãªãåãããã®ãList.fold
é¢æ°ããList.reduce
é¢æ°
F#ã®ãã€ãã©ã€ã³æŒç®å |>
以äžã®èª¬æã¯ã説æã®éœåäžã UNIT2 BASIC ã®å 容ãå åãããŠããŸã£ãŠãã
åŸã§è©³ãã説æããªããã®ã§ãä»ã®ãšããã¯ããããããã®ãããã®ã çšåºŠã«èªã¿æµããŠããã£ãŠæ§ããªã
ãã¡ã°ãå€æ ã®èŠç¹ãšããŠã¯ã
é¢æ°åã³ãŒãã§ã¯ãç¹°ãè¿ãåŠçãæŒç®ïŒåŒïŒã§æžãã ããšããããš
- F#ã®ãã€ãã©ã€ã³æŒç®å
|>
ãšçµã¿åãããŠ\rhd\textup{reduce} |> List.reduce
ãšããããšã§(+)
ãšã€ãªããäºé æŒç®åã«ãªãã
[0;1;2;3;4;5] |> List.reduce (+)
// 15
- ïŒã€ã®åŒïŒãããã¯ïŒãã€ãªãæŒç®å
+
ã¯(+)
ãšããããšã§ãé¢æ°ãšããåŒãšãªããã€ãªãåããããåŽã®ãããã¯ãšãªã
- ç³ã¿èŸŒã¿æäœãããããã«çšæãããŠãã
List.reduce
é¢æ°ã¯ããã€ãã©ã€ã³æŒç®å|>
ãšã€ãªãåãããã|> List.reduce
ãšãã²ãšãŸãšãŸãããšèŠãªãããšã§ãç³ã¿èŸŒã¿å¯Ÿè±¡ãšãªããªã¹ã[1;2;3;4;5]
ãš(+)
ãšãã2ã€ã®åŒïŒãããã¯ïŒãã€ãªãæŒç®åãšãªã
- ãã®ããã« æŒç®åãé¢æ°ãšãªããé¢æ°ãæŒç®åãšãªããçžäºå ¥ãæ¿ãããªãããäºé æŒç®åŒãæ§ç¯ããŠãã