ãã£ããªã³ãã¥ãŒ
ã°ã¬ãŒããã£ããªã³ã®è¥¿ã®å£ã«ãããŸãã ãããããæžè°·ãšããªãŒãžå·ã®äžæµã®çŽ æŽãããæ¯è²ãèŠããŸãã ãã£ããªã³ã暪åããšããã¯ã€ãã¯ãªãã®å£ãæ±ã®ãã©ãããããããŠã³ãã³ã®åŒ·å€§ãªåå£ã«åæµããŸãã åã®ãã£ããªã³ã®äžæµã«ç¶ããŠãè¹ã®ããã¢ã©ã²ã€ã³æ»ãèŠãããããšããããŸãã 匷倧ãªããªã®ãå·ã¯ã倧ããªæãæŽçªããæµãåºãŠããŸãã 西ãšåã«ã¯åºå€§ãªæ£®ããããäœãã€ã«ã䌞ã³ãŠããŸãã å西ã«ç¶ãå°éã ããããæžè°·ã«éããããšãã§ããŸãã
å 容ïŒ
ãããã€
ããŒã1-å
ããŒã2-ãã©ã¬ã¹ã
ããŒã3-ããªã¢ã
ããŒã4-ãã£ããªã³ã®ãã¥ãŒ
ããŒã5-ããŒã«
ããŒã4
ããã§ã¯ããªãã¡ã¯ã¿ãªã³ã°ãè¡ããããã€ãã®ã¢ã¯ã·ã§ã³ãå®è£ ãããã¿ãŒã³ãããã³ã°ãšååž°ã«ã€ããŠåŠã³ãã¯ãšã¹ãããå®éã®ããã°ã©ã ãäœæããŸãã
ãããŠãçŸåšã®å Žæãå ¥åããŸããããïŒ ãããŠèšã-ããã¯æéã§ãã æãéèŠãªããšããã¹ãŠå®è¡ãããruné¢æ°ãæ¢ã«ãããŸããã€ãŸããçŸåšã®å Žæããã®äžã«ããå¿ èŠããããŸãã runé¢æ°ã§ãå Žæã®èª¬æãæåã«è¡šç€ºããã次ã«ä»ã®ãã¹ãŠã衚瀺ããããšããŸãã ãããã£ãŠãruné¢æ°ã¯çŸåšã®å ŽæãèªèããŠããŸãã ãã®å Žæããã©ã¡ãŒã¿ãŒãšããŠåœŒå¥³ã«æž¡ããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
putStrLn ïŒ evalAction ïŒ convertStringToAction x ïŒ ïŒ
ããã ç§éã¯è©Šã¿ãŸãïŒ
*ã¡ã€ã³>å®è¡ããŒã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åïŒèŠãŠ
ã¢ã¯ã·ã§ã³ïŒèŠãŠïŒ
*ã¡ã€ã³>
ãããããªããšããçãããã°ã©ã ã§ãããïŒ æåŸ ã©ããã«åäœããŸãããããã«çµäºããŸãã ãŸããããã¯ã²ãŒã ã§ã¯ãããŸããã ã²ãŒã ã§ã¯ãéåžžãã€ãã³ããã³ãã©ãŒã¯ã[çµäº]ãã¿ã³ã«ãã£ãŠæ確ã«äžæããããŸã§ã«ãŒãã§ã¹ãã³ããŸãã ãã®ãããªãã®ã欲ããã®ã§ãããQuitããšèšããŸã§ã²ãŒã ã¯åäœããã¯ãã§ãã ã©ããã£ãŠããã®ïŒ ãŸãããŠãŒã¶ãŒããã®ã³ãã³ãã®ç¶ç¶çãªåŠçãæŽçããå¿ èŠããããŸãã æãç°¡åãªæ¹æ³ã¯ãèªåèªèº«ããrunãåŒã³åºãããšã§ãã ãã©ã¡ãŒã¿ããããŸã-çŸåšã®å Žæ-å€ãçŸåšã®å Žæã転éããéã«ãåŸã§äœããèããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
putStrLn ïŒ evalAction ïŒ convertStringToAction x ïŒ ïŒ
putStrLn ãã¿ãŒã³çµäºã \ n " -æ°ãã移å-æ°ããè¡ããã
curLocãå®è¡ããŸã
-ãã¹ãïŒ
*ã¡ã€ã³>å®è¡ããŒã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åïŒèŠãŠ
ã¢ã¯ã·ã§ã³ïŒèŠãŠïŒ
ã¿ãŒã³çµäºã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åããŸãïŒ < Ctrl + C > Interrupted ã
ãã®ããã°ã©ã ã¯ããã¬ã«ãªã«ã¬ã³ãæåããçŽ çŽã«éå§ããŸããã ãŸã é©åãªã€ãã³ãåŠçãè¡ãããŠããªããããããã°ã©ã ãäžæããå¯äžã®æ¹æ³ã¯<Ctrl + C>ãæŒãããšã§ãã ãŸããQuitã³ãã³ãã«åœŒå¥³ãåå¿ãããã«ã¯ãå°ãã®ãªãã¡ã¯ã¿ãªã³ã°ãå¿ èŠã§ãã runé¢æ°ã次ã®ããã«æžãçŽããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
ã®ã±ãŒã¹ ïŒ convertStringToAction x ïŒ
çµäº-> putStrLn ã äŒã ãŸããã...ã
ããã§ãªããã° -> ãã
putStrLn ïŒ evalAction ïŒ convertStringToAction x ïŒ ïŒ
putStrLn ãã¿ãŒã³çµäºã \ n "
curLocãå®è¡ããŸã
ãããããã§äœããæ±ããŠããŸãïŒ..ãããææ¡ããŸãããã å®è¡æ©èœã®éå§ã¯ããªãã¿ã§ãã æåã®doããŒã¯ãŒãã¯ããã§ãŒã³å ã®ã¢ã¯ã·ã§ã³ããªã³ã¯ããŸãã ãã®ãã§ãŒã³ã«ã¯ã©ã®ãããªã¢ã¯ã·ã§ã³ãå«ãŸããŠããŸããïŒ 4ã€ã®ã¿ïŒïŒå Žæã®èª¬æãå°å·ïŒ-ïŒãEnterã³ãã³ãïŒâè¡ãã衚瀺ïŒ-ïŒãŠãŒã¶ãŒããæååãåãåããå€æ°xã«é¢é£ä»ããŸãïŒ-ïŒcase-constructionå ã®åŒãå®è¡ããŸãïŒã å®è¡ãã±ãŒã¹ã«éãããšãç®çã®ä»£æ¿ã«ãžã£ã³ãããŠãããã§ç¶ç¶ããŸãã ãŠãŒã¶ãŒããQuitããšå ¥åããé¢æ°ïŒconvertStringToAction xïŒãQuitã³ã³ã¹ãã©ã¯ã¿ãŒãè¿ããšããŸããããã¯ãæåã®éžæè¢ãæ©èœããããšãæå³ããŸãã ãã®ä»£æ¿æ¹æ³ã«ã¯ããBe See You ...ããšããè¡ãå°å·ããã ãã®ã¢ã¯ã·ã§ã³ããããŸãã ã©ãã«ãã¢ã¯ã·ã§ã³ã¯ãããŸãããQuitãªã«ã¿ããã£ãå ã§ããcase-constructionã®åŸã§ããããŸããããããã£ãŠãruné¢æ°ã¯ãã以äžäœãããããšããªããçµäºããŸãã ãŠãŒã¶ãŒããQuitãã§ã¯ãªããLookããšå ¥åããªãã£ãå Žåãã©ããªããŸããïŒ Quitã®ä»£æ¿æ段ãæ©èœããªãããšã¯æããã§ããã ãã以å€ã®å Žåã¯åžžã«èŠæãããŸã-ããã§å®è¡ãç¶è¡ãããŸãã ãããŠãäœããããŸããïŒ ãã1ã€ã®ããŒã¯ãŒãããããŸãïŒ ãã®ãããããã§æ°ããäžé£ã®ã¢ã¯ã·ã§ã³ãéå§ããããŸã£ããåãæ¹æ³ã§æ®µéçã«å®è¡ãããŸãã ãã®ãã§ãŒã³ã«ã¯ã©ã®ãããªã¢ã¯ã·ã§ã³ãå«ãŸããŠããŸããïŒ 3ã€ã®ã¿ïŒïŒåŠçãããã³ãã³ããå°å·ããïŒ-ïŒãEnd of turnã\ Nãè¡ãå°å·ããïŒ-ïŒruné¢æ°ãå®è¡ããïŒã
äžèŠãããšããããã®é¢æ°ã¯ç解ã§ããŸãããããããã¹ãŠã®ã±ãŒã¹ãdoãleftãrightã®ç¢å°ïŒ ãã ããå®è£ ã泚ææ·±ãç£èŠãããšããã¹ãŠãé©åã«é 眮ãããŸãã èããŠãããšããã«ããã°ã©ã ãå®éã«æ©èœãããã©ããã確èªããã ãã§ãã
*ã¡ã€ã³>å®è¡ããŒã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åïŒèŠãŠ
ã¢ã¯ã·ã§ã³ïŒèŠãŠïŒ
ã¿ãŒã³çµäºã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åïŒçµäº
ããªãã«äŒããŠ...
*ã¡ã€ã³>
ãã£ãïŒ ããã§ããŸããïŒ ããããå®è¡é¢æ°ã¯ãŸã çæ³ããã¯ã»ã©é ããã®ã§ãã åŒã³åºãïŒconvertStringToAction xïŒã2åå«ãŸããŠããããšã«æ³šæããŠãã ãããããã¯æªãããšã§ãã 幞ããªããšã«ãé¢æ°convertStringToActionã¯åçŽã§ããªãœãŒã¹ãå¿ èŠãšããŸãããããããªããšããªãŒããŒã©ã³ã«ãªããŸãã Haskellããã³ä»ã®èšèªã§ã¯ãç¹°ãè¿ããé¿ããå¿ èŠããããŸãã ãã®åŒã³åºãã¯case-constructionã§ããããããã®çµæãå€æ°ã«å ¥ããããšãã§ããŸãã å®è¡é¢æ°ãå°ãå€æŽããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
ã®ã±ãŒã¹ ïŒ convertStringToAction x ïŒ
çµäº-> putStrLn ã äŒã ãŸããã...ã
convertResult- > do
putStrLn ïŒ evalAction convertResult ïŒ
putStrLn ãã¿ãŒã³çµäºã \ n " -æ°ãã移å-æ°ããè¡ããã
curLocãå®è¡ããŸã
ã¯ããããã§ã¯ãªãã convertResultå€æ°ãä»ã§ãã ããå€ã«å¯Ÿãã以åã®éžæè¢ãæ©èœããªãã£ãå ŽåãäžåºŠèšç®ããããã®å€ã¯å€æ°ã«é 眮ãããŠäœ¿çšãããŸãã ãããŠãããã¯åªããå®è·µã§ãããæçœãªçç±ããã ããã¯ä»ã®å Žåãããããã«äžè¬çã§ãã
ãŸããããã¯éåžžã«é£ããã£ãã®ã§ãä»æ¥ã¯ããã ãã§ãã
ã€ã³ãã³ããå¿ããªãã§ãã ãã;ããã§éèŠã§ã; æåãš2çªç®ã®ãdoãã§ã¯ããã¹ãŠã®ã¢ã¯ã·ã§ã³ã¯æåã®ã¢ã¯ã·ã§ã³ãéå§ãããå Žæã«æ£ç¢ºã«é 眮ãããŸãã ããšãã°ã次ã®ããã«ãç°ãªãäœçœ®ã«é 眮ããããšãã§ããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
ã®ã±ãŒã¹ ïŒ convertStringToAction x ïŒ
çµäº-> putStrLn ã äŒã ãŸããã...ã
ããã§ãªããã° -> ãã
putStrLn ïŒ evalAction ïŒ convertStringToAction x ïŒ ïŒ
putStrLn "ã¿ãŒã³ã®çµããã \ n " -æ°ãã移å-æ°ããè¡ããã
curLocãå®è¡ããŸã
åãããã¯ã«ã¯ç¬èªã®ã€ã³ãã³ãããããããããæ··åšãããå¿ èŠã¯ãããŸããã
誰ãããªããçºæ£ã§ãããšèšã£ãïŒ ç¶ããŸãïŒ ãã¡ãããããªãã¯ãã§ã«ããã°ã©ã ãæ¹åããèšç»ãç«ãŠãŠããŸãã ãããŠåœç¶ïŒ Lookã³ãã³ãã®å®éã®ïŒæ¶ç©ºã§ã¯ãªãïŒåŠçããã€ãèªåã§è¿œå ããŸãããGoã³ãã³ãã«ã€ããŠèããŠã¿ãŸãããã Zorkã§ã¯ã©ã®ããã«å®è£ ãããŠããŸããïŒ
æž ç®
...
>西ã«è¡ã
森
...
ãGo Westããšæžããšãã³ãã³ãããŒãµãŒã¯ãã®è¡ã解æããæ¹æ³ãæ確ã§ã¯ãªãããäžæºã«ãªããŸãã
*ã¡ã€ã³>å®è¡ããŒã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åïŒGo West
***äŸå€ïŒ ãã¬ãªã¥ãŒã ã èªã¿åã ïŒè§£æãªã
GoãšWestã®ã³ã³ã¹ãã©ã¯ã¿ãããããããGo Westãã®è¡ã2ã€ã«åããŠå¥ã ã«è§£æããããšãã§ããŸããïŒ ãã¡ãããã¢ã€ãã¢ã¯æ£ããã§ããã確ãã«æ©èœããŸãããGo Westãããã«ç°¡åã«è§£æã§ããªããã°ãHaskellã¯éæ³ã®èšèªã«ã¯ãªããŸããã é ãããè¡ããã èŠããŠããããã«ã3çªç®ã®éšåã«ã¯ãADTåã®ã³ã³ã¹ãã©ã¯ã¿ãŒãç¹å¥ãªé¢æ°ã§ãããšããã€ã³ã»ããããããŸããã ããšãã°ãHomeã¯Locationã¿ã€ãã®é¢æ°ãGoã¯Typeã¢ã¯ã·ã§ã³ã®é¢æ°ãWestã¯Directionã¿ã€ãã®é¢æ°ã§ãã
* Main > ïŒ ã¿ã€ã Home
ããŒã ::ãã±ãŒã·ã§ã³
* Main > ïŒ ã¿ã€ã West
西::æ¹å
* Main > ïŒGoãšå ¥å
Go ::ã¢ã¯ã·ã§ã³
é¢æ°ã«ã€ããŠäœãç¥ã£ãŠããŸããïŒ ããã ãã 圌ãã¯è°è«ããããããããªãããšã ãããŠãã©ã®ãããªè³ªåãããå¿ èŠããããŸããïŒ ããã ãã ãã³ã³ã¹ãã©ã¯ã¿ãé¢æ°ã§ããå Žåãããã¯åŒæ°ãæã€ããšãã§ãããšããããšã§ããïŒããæ³åã®ãšãããããã§ãïŒ ADTã¿ã€ããå®çŸ©ãããšãããã®ã³ã³ã¹ãã©ã¯ã¿ãŒãŸãã¯ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ãåŒæ°ã®ã¿ã€ããæå®ã§ããŸãã Goã³ã³ã¹ãã©ã¯ã¿ã«DirectionåŒæ°ãè¿œå ããŸãã
ããŒã¿ã¢ã¯ã·ã§ã³=
èŠãŠ
| è¡ãæ¹
| åšåº«
| åã
| èœãšã
| 調æ»ãã
| ããã
| ä¿åãã
| è² è·
| æ°ãã
å°åº ïŒ Eq ã Show ã Read ïŒ
ãã®å ŽåãDirectionãååŸããŠActionãè¿ãé¢æ°ãäœæãããããã³ã³ã¹ãã©ã¯ã¿ãŒã®ã¿ã€ãã¯ãããã«å€æŽãããŸãã ã³ã³ã¹ãã©ã¯ã¿ãŒã®æååãžã®å€æãããã³ãã®éã®åäœã確èªããŸãããã
*ã¡ã€ã³> ã·ã§ãŒ ïŒ Go West ïŒ
西ãžè¡ã
*ã¡ã€ã³> ãGo Northãã èªã ::ã¢ã¯ã·ã§ã³
åãžè¡ã
*ã¡ã€ã³> ïŒt Go
移å::æ¹å->ã¢ã¯ã·ã§ã³
ãã£ã¡ ãããŠäœããããç§ãã¡ã®åŽã«ã¯ã»ãšãã©åªåããããŸããã è€åã³ã³ã¹ãã©ã¯ã¿ãŒã¯ä»ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã»ã©è€éã§ã¯ãªããçŽæçã«ããŒã¿ãèšå®ãããããããã«åªããŠããŸãã ãããŠåœŒãšã®ä»äºã¯ãšãŠãç°¡åã§ãïŒ å®è¡ã±ãŒã¹ãæŽæ°ããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
ã®ã±ãŒã¹ ïŒ convertStringToAction x ïŒ
çµäº-> putStrLn ã äŒã ãŸããã...ã
Go dir- > putStrLn ïŒ "You going to" ++ show dir ++ "ïŒ" ïŒ
convertResult- > do
putStrLn ïŒ evalAction convertResult ïŒ
putStrLn ãã¿ãŒã³çµäºã \ n " -æ°ãã移å-æ°ããè¡ããã
curLocãå®è¡ããŸã
-ãã§ãã¯ïŒ
*ã¡ã€ã³>å®è¡ããŒã
ããŒã
ããªãã¯æšè£œã®ããŒãã«ã®çãäžã®éšå±ã«ç«ã£ãŠããŸãã
ã³ãã³ããå ¥åïŒGo West
ããªãã¯è¥¿ã«è¡ãïŒ
ããŒãµãŒãè¡ã®ãGo Somethingããèªèãããšã代æ¿ã®ãGo dirããæ©èœããåããSomethingããdirå€æ°ã«åé¡ãããŸãã ããã§ã¯ãdirå€æ°ã䜿çšããŸãã éæ³ïŒ
Haskellã®ä»£æ°ããŒã¿åã¯ããã«åªããŠããŸãã ãããããåŒæ°ä»ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããŠã第3éšã®ããã èšç®æ©ãã¿ã¹ã¯ãæžãæããæ¹æ³ãæ¢ã«ç解ããŠãããããããŸããã 次ã®ãããªãã®ãååŸã§ããŸãã
ããŒã¿ IntegerArithmOperation =
ãã©ã¹æŽæ° æŽæ°
| ãã€ãã¹æŽæ° æŽæ°
| 補åæŽæ° æŽæ°
| æŽæ°ã®åŠå®
evalOp :: IntegerArithmOperation- > æŽæ°
evalOp op = case op
Plus x y- > x + y- ãopããã©ã¡ãŒã¿ãŒã¯åãªãã·ã§ã³ã«é çªã«ãããã³ã°ãããŸãã
ãã€ãã¹x y- > x - y- ã©ã®ãµã³ãã«ã衚瀺ããããããã®ãããªä»£æ¿ãéžæãããŸãã
Prod x y- > x * y -xãšyã®ä»£ããã«ãã³ã³ã¹ãã©ã¯ã¿ãŒãšäžç·ã«æž¡ãããæ°å€ã眮ãæããããŸãã
åŠå®x -> - x- èšç®çµæãè¿ããŸãã ããšãã°ã2 * 3 = 6ã
* Main > evalOp ïŒ Plus 2 3 ïŒ -ïŒPlus 2 3ïŒã¯2ã€ã®åŒæ°ãæã€ã³ã³ã¹ãã©ã¯ã¿ãŒã§ãã
5
*ã¡ã€ã³> evalOp ïŒãã€ãã¹2 3 ïŒ
-1
* Main > evalOp ïŒ Prod 2 3 ïŒ
6
* Main > evalOp ïŒ Negate 5 ïŒ
-5
åãå Žæãèžã¿ã€ããããã©ããªåéºå®¶ã§ããããïŒ æ³ã§åãéããå Žæãåãæ¿ããå¿ èŠããããŸãïŒ çŸåšã®äœçœ®ãšåãã®æ¹åãååŸããæ°ããäœçœ®ãè¿ãwalké¢æ°ïŒ2çªç®ã®éšåããã®ã¿ã¹ã¯ïŒããããšããŸãã 以äžããã®1ã€ã§ãã
æ©ã::å Žæ->æ¹å->å Žæ
walk curLoc toDir = case curLoc of
Home- > ã±ãŒã¹ toDir
å->åº
å->ãã¬ã³ãã®åº
ãã以å€ã®å Žå ->ããŒã
åº-> ã±ãŒã¹ toDir
å->ãã¬ã³ãã®åº
å->ããŒã
ãã以å€ã®å Žå ->åº
-...ããã«æ®ãã®ãªãã·ã§ã³ãè¿œå ããŸãã
å®éãã²ã©ãã¢ãããŒãã§ãã ãããªã«äœåãªä»äºïŒ ãã¹ããããã±ãŒã¹ãéåžžã«å€ãïŒ ååãªç¥èãããã°ãééããªãä»ã®äœããæãä»ãã§ãããããããŠä»ã§ã¯ãã±ãŒã¹ããªãããã«ãŠã©ãŒã¯é¢æ°ãæ¹åããããšãã§ããã ãã§ãã
æ©ã::å Žæ->æ¹å->å Žæ
æ©ããŠããŒã ããŒã¹=ã¬ãŒãã³
åŸæ©ããŒã å= Friend'sYard
ã¬ãŒãã³ããŒã¹= Friend'sYardãæ©ã
åºåãæ©ã=ããŒã
Friend'sYard North = Homeãæ©ã
Friend'sYard South = Gardenãæ©ã
æ©ãcurLoc _ = curLoc
ãããŒãããã€ã®ãŠã©ãŒã¯æ©èœããããŸãïŒ ãããŠãã¹ãŠåãã§ãããåã®äŸãããè¡ãå°ãªããæ©èœãå€ããªã£ãŠããŸãã ã©ã®ããã«æ©èœããŸããïŒ ãšãŠãç°¡åã§ãã åŒæ°ãæå®ããŠwalké¢æ°ãåŒã³åºããšãé©åãªãªãã·ã§ã³ãéžæãããŸãã ããšãã°ããwalk Garden Southã-4çªç®ãéžæãããHomeã«æ»ããŸãã
*ã¡ã€ã³>ãµãŠã¹ã¬ãŒãã³ãæ©ã
ããŒã
èå³ã¯æåŸã®æ£æ©ã§ãã 圌女ã¯ç§ãã¡ãçŸåšã®å Žæã«çœ®ããŠããŸããã ä»ã®å šå¡ãæ©èœããªãå Žåã§ãæ©èœãããšæšæž¬ã§ããŸãã ãã®äžã§ãæåã®ãã©ã¡ãŒã¿ãŒã¯curLocå€æ°ã«é©åãã2çªç®ã®ãã©ã¡ãŒã¿ãŒã¯ã©ãã«ãé©åããŸããã äžè¬ã«ã2çªç®ã®ãã©ã¡ãŒã¿ãŒã®å 容ã¯éèŠã§ã¯ãªãããã䜿çšããªããããã¢ã³ããŒã¹ã³ã¢èšå·ã䜿çšããŸãã ãã¡ãããããçš®ã®å€æ°ãã¹ãªããããããšã¯ã§ããŸãããäžç·ã¯ããæçœã§ãã åŒæ°ãæå®ããªãã§ãã ããã ããªãããããããšãghciã¯scãã圌ãã¯èšãããªãããªãã¯ãããªã«æ°ãŸããã§ããïŒ..
......
Friend'sYard North = Homeãæ©ã
Friend'sYard South = Gardenãæ©ã
walk curLoc = curLoc
*ã¡ã€ã³> ïŒr
[ 1 of 1 ] Mainã®ã³ã³ãã€ã«ïŒ HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMainãHs ã解éæžã¿ïŒ
HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMain hsïŒ 50 ïŒ 1 ïŒ
ããŠã©ãŒã¯ãã®æ¹çšåŒã®åŒæ°ã®æ°ã¯ç°ãªããŸã
HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMain hsïŒ 50 ïŒ1-24
HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMain hsïŒ 56 ïŒ1-22
倱æããã¢ãžã¥ãŒã«ãããŒããããŸããïŒãªãã
ããã§äœ¿çšãããŠãããã¿ãŒã³ãããã³ã°ã¯äŸ¿å©ãªããŒã«ã§ãããã³ãŒããããæ確ãå®å šãçããªããŸãã ãã¿ãŒã³ãããã³ã°ã«ã¯ä»ã«ã䟿å©ãªæ©èœããããŸãã å®æ°ã ãã§ãªãå€æ°ããããŒã ãªããã§ããçšèªãã³ã³ããŒãã³ãããŒãã«å解ããããšãã§ããŸãã ç¹å¥ãªãšã³ããªã䜿çšãããšããªã¹ãã®æåã®èŠçŽ ãšæ®ãã®èŠçŽ ãç°¡åã«åé¢ã§ããŸãã æååã§ã¯æ¬¡ã®ããã«ãªããŸãã
headLetter :: æåå -> Char
headLetter ïŒ chïŒchs ïŒ = ch
tailLetters :: æåå -> æåå
tailLetters ïŒ chïŒchs ïŒ = chs
*ã¡ã€ã³> headLetter "Abduction"
ãAã
*ã¡ã€ã³> tailLetters "Abduction"
ãBèŽã
æåŸã®ã¹ãããã¯ãå®è¡æ©èœãå€æŽããããšã§ãã ãã¹ãŠãç°¡åã§ãïŒäžé£ã®ã¢ã¯ã·ã§ã³ãGo dir代æ¿ã«è¿œå ããã\ nYou walking toãè¡ã衚瀺ããŸã++ show dir ++ "ã\ N"ãruné¢æ°ãå床å®è¡ããŸãããä»åã¯walkã䜿çšããŠæ°ããçŸåšäœçœ®ãååŸããŸãã
curLoc = doãå®è¡ããŸã
putStrLn ïŒ describe curLocãèšè¿°ïŒ
putStr ãã³ãã³ããå ¥åïŒã
x < -getLine
ã®ã±ãŒã¹ ïŒ convertStringToAction x ïŒ
çµäº-> putStrLn ã äŒã ãŸããã...ã
Go dir- > do
putStrLn ïŒ " \ n You walking to" ++ show dir ++ "ã \ n " ïŒ
å®è¡ïŒ walk curLoc dir ïŒ
convertResult- > do
putStrLn ïŒ evalAction convertResult ïŒ
putStrLn ãã¿ãŒã³çµäºã \ n "
curLocãå®è¡ããŸã
-ãã§ãã¯
*ã¡ã€ã³>å®è¡ããŒã
ããŒã
ããªãã¯ç«ã£ãŠããŸã...
ã³ãã³ããå ¥åïŒGo North
åãžæ©ããŠããŸãã
åº
ããªãã¯åºã«ããŸãã ...
å®éãããããã¹ãŠã§ãã Goã¢ã¯ã·ã§ã³ãæ©èœããŸãã
ååž°ã¯ååž°ã§ããããèªäœããã®é¢æ°åŒã³åºãã§ãã ç§ãã¡ã¯Haskellã§ååž°ãç©æ¥µçã«äœ¿çšããŠããŸã-ããã«ã¯äœã®åé¡ããããŸããã C ++ãªã©ã®åœä»€åèšèªãšã¯ç°ãªããé¢æ°åèšèªã§ã¯ãä»ã®ã¡ãœããã§è¡ãããšãå°é£ãªå Žæã§ååž°ã䜿çšããããšããå§ãããŸãã ãã®å Žåãååž°ã¯åãå ¥ããããŸãã 倧éã®ããŒã¿ãçæããããšã¯ãªããååž°ã1,000åãã¹ãã³ããªãã®ã§ãåãC ++ã§çºçããå¯èœæ§ã®ãããªãœãŒã¹ãªãŒã¯ãæããŠã¯ãããŸããã Haskellã«ã¯ãååž°ãå®å šãªããŒã«ã«ããã¡ã«ããºã ããããŸãã ãã®ãããããã§çŸåšå¿ èŠãªåŒã®ã¿ãèšç®ãããŸããã€ãŸãããªãœãŒã¹ã¯ä»ã®ã³ãŒãã®å·šå€§ãªã¬ã€ã€ãŒã«è²»ããããŸããã ã³ã³ãã€ã©ã¯ãäžåºŠèšç®ããããã®ãåå©çšã§ããŸãã Haskellã«ã¯ãã®ãããªã¹ã¿ãã¯ã¯ãããŸãã-ã¡ã¢ãªããã£ã±ãã«ãªããŸããã æåŸã«ãæ«å°ŸåŒã³åºããžã®ååž°ãæé©åããããšã«ãããã³ã³ãã€ã©ã¯éåžžã®æå³ã§ååž°ããŸã£ãããªãããšãä¿èšŒã§ããŸãã
ç°¡åãªããã°ã©ã ãã³ã³ãã€ã«ããŠããªãœãŒã¹äœ¿çšã®ãã€ããã¯ã¹ã確èªã§ããŸãã ããã°ã©ã ã¯ãååž°ã®åã¹ãããã§ãã£ããããæ°ãèšç®ãããããã®æåŸã衚瀺ããŸãã
-test.hsïŒ
ã¢ãžã¥ãŒã«ã¡ã€ã³
fibs = 0 ïŒ 1 ïŒ zipWith ïŒ + ïŒ fibs ïŒ tail fibs ïŒ
cnt = doãå®è¡ããŸã
putStrLn ïŒ " \ n å転 æ°ïŒ" ++ show cnt ïŒ
putStrLn $ show $ last $ take cnt fibs
å®è¡ïŒ cnt + 1 ïŒ
ã¡ã€ã³= è¡ã
å®è¡1
æé©åã䌎ãã³ã³ãã€ã«ïŒ
ghc - Oãã¹ãã hs
ã³ãŒãã®é åºã¯å¥œãã§ããïŒ ã¯ãšã¹ãã®ã³ãŒãã¯ãäžè¬çã«ã¯æªãã¯ãããŸãããã1ã€ã®éèŠãªãã®ãæ¬ ããŠããŸããããã°ã©ã ãžã®ãšã³ããªãã€ã³ãã§ãã å®è¡å¯èœãã¡ã€ã«ã«ã³ã³ãã€ã«ããããšããã§ããŸããã GHCiã€ã³ã¿ãŒããªã¿ãŒã䜿çšããŠããéãããã«ã€ããŠã¯èããŸããã§ããããå人ãã²ãŒã ããã¬ã€ãããå Žåã¯ã©ãã§ããããïŒ åœŒã«äžããããã®ã¯Haskellã®ã³ãŒãã§ã¯ãããŸãããäžè¬çã«ã¯å人ã§ãããããã°ã©ãã§ã¯ãªããããããŸããã ããããå®è¡å¯èœãã¡ã€ã«ã¯ç°¡åã§ãã ããã°ã©ã ãå®éã«ã³ã³ãã€ã«ããã«ã¯ãã³ãã³ãããã³ããã§ghcã³ãã³ããå®è¡ããQuestMain.hsãžã®ãã¹ãæå®ããŸãã
HïŒ\ Haskell \ QuestTutorial \ Quest > ghc QuestMain ã hs
[ 1 of 1 ]ã³ã³ãã€ã«ã¡ã€ã³ïŒ QuestMainãHs ã QuestMain.o ïŒ
QuestMain hsïŒ 1 ïŒ 1
é¢æ° 'main'ã¯ã¢ãžã¥ãŒã« 'Main'ã§å®çŸ©ãã㊠ããŸãã
GHCã³ã³ãã€ã©ãŒã¯ãmainé¢æ°ãmainã¢ãžã¥ãŒã«ã§èŠã€ãããªãã£ããšèšããŸãã ããããHaskellããã°ã©ã ãžã®ãšã³ããªãã€ã³ãã®åœ¢ææ¹æ³ã§ãã äœããã®åœ¢ã§äž»ãªæ©èœãããä»ã®èšèªã®ããã«èŠããŸãã QuestMain.hsãã¡ã€ã«ã®æåŸã«è¿œå ããŸãã
ã¡ã€ã³= è¡ã
putStrLn "Haskellã§ã®ã¯ãšã¹ãã¢ããã³ãã£ãŒã \ n "
家ã«åž°ã
ãããŠããã¡ã€ã«ã®æåã®éšåã§ãMainã¢ãžã¥ãŒã«ãå®çŸ©ããŸããMainã¢ãžã¥ãŒã«ã«ã¯ããã¹ãŠã®æ©èœãå«ãŸããŸãã
ã¢ãžã¥ãŒã«ã¡ã€ã³
ããã§ãã³ã³ãã€ã©ãŒã¯ãœãŒã¹ãå®å šã«é£ã¹ãå®è¡å¯èœãã¡ã€ã«ã衚瀺ãããŸãã ãã®QuestMain.exeããããŸãã ãšããããæ¡åŒµåã.oããã³.hiã®ãã¡ã€ã«ã衚瀺ãããŸã-ãããã¯äžæãã¡ã€ã«ã§ãïŒãªããžã§ã¯ããšã€ã³ã¿ãŒãã§ã€ã¹ãæã€ãã¡ã€ã«ïŒã æ°ã«ãªãå Žåã¯åé€ã§ããŸãã ãããžã§ã¯ãã¯å°ããã§ãããç¹å¥ãªåœ¹å²ã¯æãããŸããã ãã®åŸãä»ã®èšèªãšåæ§ã«ãéšåã³ã³ãã€ã«ã«äœ¿çšã§ããŸããããã¯ãæåããã³ã³ãã€ã«ãããããã¯ããã«é«éã§ãã ããšãã°ãã¢ãžã¥ãŒã«ãäžåºŠã³ã³ãã€ã«ãããŠå€æŽãããªããªã£ãå ŽåãäŸåããã¢ãžã¥ãŒã«ãå€æŽãããªãã£ãã®ãšåãããã«ãå€ã.oãã¡ã€ã«ãš.hiãã¡ã€ã«ãååŸããã ãã§ãåã³ã³ãã€ã«ããå¿ èŠã¯ãããŸããã ãããã£ãŠãã³ãŒããã¢ãžã¥ãŒã«ã«åå²ããããšããå§ãããŸãã ããã«è¯ã-ã¢ãžã¥ãŒã«ãšãã©ã«ãã«ãã£ãŠ; ãããŠããã«è¯ã-ã¢ãžã¥ãŒã«ããã©ã«ããã©ã€ãã©ãªã«ãã£ãŠã
ã¯ãšã¹ããTypesã¢ãžã¥ãŒã«ãšMainã¢ãžã¥ãŒã«ã®2ã€ã®ã¢ãžã¥ãŒã«ã«åããŸãããã ãããè¡ãã«ã¯ãTypes.hsãã¡ã€ã«ãäœæããæäžéšã§ãmodule Types whereãè¡ã䜿çšããŠã¢ãžã¥ãŒã«ãšããŠå®çŸ©ããQuestMain.hsãã¡ã€ã«ãããã¹ãŠã®ADTã¿ã€ãã転éããŸãã
-Types.hsïŒ
ã¢ãžã¥ãŒã«ã®çš®é¡
ããŒã¿ã®å Žæ=
ããŒã
| ...
å°åº ïŒ Eq ã Show ã Read ïŒ
ããŒã¿ã®æ¹å=
å
| ...
å°åº ïŒ Eq ã Show ã Read ïŒ
ããŒã¿ã¢ã¯ã·ã§ã³=
èŠãŠ
| è¡ãæ¹
| ...
å°åº ïŒ Eq ã Show ã Read ïŒ
ghciãã³ãã³ãrãå®è¡ãããšãã€ã³ã¿ãŒããªã¿ãŒã¯ãããã¯ããŸãããããã®ã¿ã€ããèªèããŸããïŒ
*ã¡ã€ã³> ïŒr
[ 1 of 1 ] Mainã®ã³ã³ãã€ã«ïŒ HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMainãHs ã解éæžã¿ïŒ
HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMain hsïŒ 4 ïŒ 21 ïŒ
ç¯å²å€ïŒ åã³ã³ã¹ãã©ã¯ã¿ãŸã㯠ã¯ã©ã¹ 'Location'
HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMain hsïŒ 7 ïŒ 13 ïŒ
ã¹ã³ãŒãå€ïŒ ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒãããŒã ã
HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMain hsïŒ 8 ïŒ 13 ïŒ
ã¹ã³ãŒãå€ïŒ ããŒã¿ã³ã³ã¹ãã©ã¯ã¿ãŒãFriend'sYardã
...ãããŠããã®ãããªè¡ãããã«20è¡ãããŸãã
ãã¬ãªã¥ãŒã>
é¢ä¿ãããŸããïŒ ã¢ãžã¥ãŒã«ãåã«æ¥ç¶ããå¿ èŠããããŸã-ãããã¯Mainã«è¡šç€ºãããŸãã Mainã¢ãžã¥ãŒã«ã®æäžéšã®ã©ããããmodule Main whereãã®äžã«ãç°¡åãªè¡ãè¿œå ããŸãã
-QuestMain.hsïŒ
ã¢ãžã¥ãŒã«ã¡ã€ã³
ã€ã³ããŒãã¿ã€ã
-...æ®ãã®ã³ãŒã...
ããã§ã³ã³ãã€ã«ãæåããŸããã æ°ã¥ãæéã¯ãããŸãããã§ã«2ã€ã®ã³ã³ãã€ã«æžã¿ãã¡ã€ã«ããããããã¯è«ççã§ãã
ãã¬ãªã¥ãŒã> ïŒr
[ 1/2 ]ã¿ã€ãã®ã³ã³ãã€ã«ïŒ HïŒ\ Haskell \ QuestTutorial \ Quest \ TypesãHs ã解éæžã¿ïŒ
[ 2 of 2 ] Mainã®ã³ã³ãã€ã«ïŒ HïŒ\ Haskell \ QuestTutorial \ Quest \ QuestMainãHs ã解éæžã¿ïŒ
OK ãããŒããããã¢ãžã¥ãŒã«ïŒMain ã Types ã
*ã¡ã€ã³>
ã³ãŒããã¢ãžã¥ãŒã«ã«åå²ããŸãã-ããã¯è¯ãããšã§ãã ãšã³ããªãã€ã³ããè¿œå ããã³ãŒãã¯å®éã®ããã°ã©ã ã«ãªããŸããã ãããããã§ãã æåŸã«ãååž°ãšãã¿ãŒã³ãããã³ã°ã«ç²ŸéããActionåã®1ã€ã®è€åã³ã³ã¹ãã©ã¯ã¿ãŒãèæ¡ããŸããã ãããïŒ ããã§ãååãªäŒæ¯ãšç¥èã®çµ±åãå¿ èŠã«ãªããŸãã
ä¿®æ£ã®ããã®å²ãåœãŠã
1. GameAction.hsãã¡ã€ã«ã«GameActionã¢ãžã¥ãŒã«ãäœæããmain以å€ã®ãã¹ãŠã®é¢æ°ãMainããæœåºããŠå®è¡ããŸãã
2. QuitãGo dirã®åŠçã®æšªã«Lookã³ãã³ãã®åŠçãè¿œå ããŸãïŒãŸã ãªãå ŽåïŒã éè€ã³ãŒã "ïŒdescribeLocation curLocïŒ"ã2ååŒã³åºããªãããã«æ¹åããæ¹æ³ãæ€èšããŠãã ããã
3.ãŠãŒã¶ãŒãæ°ããã²ãŒã ãéå§ããããšã確信ããŠãããã©ããã確èªããããã«ãNewã³ãã³ãã®åŠçãè¿œå ããŸãã
ãã®éšåã®ãœãŒã¹ ã
ç®æ¬¡ãåç §ãããã³è¿œå æ å ±ã¯Greetingsã«ãããŸãã