1. ã¯ããã«
ããã°ã©ãã³ã°ã®äžçã¯å€å²ã«ããããããŸããŸãªèãæ¹ãææ³ãååšããŸãããã®äžã§ããé¢æ°åããã°ã©ãã³ã°ãã¯ãç¬ç¹ã®å²åŠãšã¢ãããŒããæã£ãŠããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãé¢æ°åããã°ã©ãã³ã°ãäœã§ãããããªããããéèŠãªã®ãããããŠRacketèšèªããã®åéã§ã©ã®ããã«äœçœ®ã¥ããããŠããã®ãã解説ããŸãã
1.1 é¢æ°åããã°ã©ãã³ã°ãšã¯
é¢æ°åããã°ã©ãã³ã°ã¯ãèšç®ãäžé£ã®é¢æ°ãšããŠæããç¶æ ãå€æŽå¯èœãªããŒã¿ãé¿ããããã°ã©ãã³ã°ã®ãã©ãã€ã ã§ããäžèšã§èšããšãããã¯ããã°ã©ã ãæ°åŠçãªé¢æ°ã®éãŸããšããŠèããææ³ã§ãããã®èãæ¹ã®ã¡ãªãããšããŠã以äžã®ç¹ãæããããŸãïŒ
ã¡ãªãã | 説æ |
---|---|
äºæž¬å¯èœæ§ | é¢æ°ã¯åãå ¥åã«å¯ŸããŠåžžã«åãåºåãè¿ãããããã°ãå°ãªãããããã°ã容æã§ãã |
åå©çšæ§ | çŽç²ãªé¢æ°ã¯ä»ã®éšåãšç¬ç«ããŠãããããåå©çšãçµã¿åãããç°¡åã§ãã |
䞊è¡æ§ | ç¶æ ãå€æŽããªãé¢æ°ã¯äžŠè¡åŠçãç°¡åã«ãªããŸãã |
ãããã®ã¡ãªããã¯ããœãããŠã§ã¢éçºã®ããŸããŸãªã¹ããŒãžã§å©çããããããŸãã
1.2 Racketèšèªã®æŠèŠ
Racketã¯ãé¢æ°åããã°ã©ãã³ã°ããµããŒãããå€æ§ãªããã°ã©ãã³ã°èšèªã®1ã€ã§ããç¹ã«ãæè²ã®çŸå Žãç 究ã«ãããŠãããã°ã©ãã³ã°ã®åºç€ãåŠã¶ããã®ããŒã«ãšããŠå€ãå©çšãããŠããŸããRacketã®ç¹åŸŽãšããŠã¯ãã·ã³ãã«ãªææ³ãé«ãæ¡åŒµæ§ããããŠè±å¯ãªã©ã€ãã©ãªãæããããŸããRacketã䜿çšãããšãé¢æ°åããã°ã©ãã³ã°ã®ååãå®éã«äœéšããªãããããé«åºŠãªãããã¯ã«ã觊ããããšãã§ããŸãã
ãããããé¢æ°åããã°ã©ãã³ã°ã®æŠå¿µãæ·±æãããªãããRacketã䜿ã£ãå®äŸã亀ããŠããã®é åãšå¿çšãæ¢ã£ãŠãããŸãã
2. Racketã®åºæ¬ææ³
Racketã¯ãé¢æ°åããã°ã©ãã³ã°ãåŠã¶ã®ã«æé©ãªèšèªã®äžã€ã§ããããããå®è·µããããã«ã¯ããŸããã®åºæ¬çãªææ³ã«æ £ããããšãäžå¯æ¬ ã§ãããã®ã»ã¯ã·ã§ã³ã§ã¯ãå€æ°ãé¢æ°ã®å®çŸ©æ¹æ³ãäž»èŠãªããŒã¿åããããŠãªã¹ãã®åºæ¬çãªæäœã«ã€ããŠè§£èª¬ããŸãã
2.1 å€æ°ãšé¢æ°ã®å®çŸ©
Racketã«ãããå€æ°ã®å®çŸ©ã¯éåžžã«ã·ã³ãã«ã§ããäŸãã°ãæ°åã®5ããnumããšããå€æ°ã«å²ãåœãŠãå Žåã(define num 5)
ã®ããã«èšè¿°ããŸãã
é¢æ°ã®å®çŸ©ããŸããçŽæçã«è¡ããŸããäŸãšããŠã2ã€ã®æ°å€ãå ç®ããé¢æ°ãå®çŸ©ããŠã¿ãŸãããããã®å Žåã以äžã®ãããªã³ãŒãã«ãªããŸãïŒ
(define (add x y) (+ x y))
ãã®ã³ãŒãã§ã¯ãaddãšããååã®é¢æ°ãå®çŸ©ããããã2ã€ã®åŒæ°xãšyãåãåãããã®åãè¿ãããšã瀺ãããŠããŸãã
2.2 åºæ¬çãªããŒã¿å
Racketã«ã¯ãå€ãã®ããã°ã©ãã³ã°èšèªãšåæ§ã«ãããã€ãã®åºæ¬çãªããŒã¿åãååšããŸããæŽæ°ãæµ®åå°æ°ç¹æ°ãæååãçåœå€ãªã©ããããã®ããŒã¿åã¯ããã°ã©ã ã®åå°ãšãªãéšåã§ãã
ããŒã¿å | äŸ | 説æ |
---|---|---|
æŽæ° | 5, -3, 0 | æ°åŠçãªæŽæ°ãè¡šãã |
æµ®åå°æ°ç¹æ° | 5.5, -3.14, 0.0 | å°æ°ç¹ãæã€æ°åã |
æåå | “Hello”, “Racket” | ããã¹ãæ å ±ãè¡šçŸããã |
çåœå€ | #t (true), #f (false) | çãŸãã¯åœã®å€ã |
2.3 ãªã¹ãæäœã®åºæ¬
é¢æ°åããã°ã©ãã³ã°ã«ãããŠããªã¹ãã¯éåžžã«äžå¿çãªåœ¹å²ãæãããŸããRacketã«ããããªã¹ãã®åºæ¬æäœãç解ããããšã§ãããè€éãªããã°ã©ã ã®æ§ç¯ã容æã«ãªããŸãã
ãªã¹ããäœæããããã«ã¯ã(list 1 2 3)
ã®ããã«list
é¢æ°ã䜿çšããŸãããŸãããªã¹ãã®å
é ã«èŠçŽ ãè¿œå ããã«ã¯cons
é¢æ°ããªã¹ãããå
é ã®èŠçŽ ãååŸããã«ã¯car
é¢æ°ãå
é èŠçŽ ãé€ããæ®ãã®ãªã¹ããååŸããã«ã¯cdr
é¢æ°ã䜿çšããŸãã
ãããã®é¢æ°ãçµã¿åãããããšã§ããªã¹ãã«å¯Ÿããæ§ã ãªæäœãè¡ãããšãã§ããŸããäŸãã°ããªã¹ãã®åèŠçŽ ã2åã«ãããç¹å®ã®æ¡ä»¶ãæºããèŠçŽ ã ããæœåºãããšãã£ãããšããé¢æ°ã®çµã¿åããã«ãã£ãŠå¯èœã«ãªããŸãã
3. é¢æ°åããã°ã©ãã³ã°ã®æ žå¿
é¢æ°åããã°ã©ãã³ã°ã®é åãšãã®å匷ããç解ããããã«ã¯ãããã€ãã®äžå¿çãªæŠå¿µã«ã€ããŠç¥ãå¿ èŠããããŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ãçŽç²é¢æ°ãé«éé¢æ°ããããŠååž°ã«é¢é£ããç¥èãæ·±ããŠãããŸãã
3.1 çŽç²é¢æ°ãšå¯äœçš
çŽç²é¢æ°ãšã¯ãå€éšã®ç¶æ ã«äŸåãããäžããããå ¥åã«å¯ŸããŠåžžã«åãåºåãè¿ãé¢æ°ã®ããšãæããŸãããã®ç¹æ§ã«ãããçŽç²é¢æ°ã¯äºæž¬å¯èœã§ããããã¹ãããããã°ã容æã«ãªããŸãã
äžæ¹ãå¯äœçšãæã€é¢æ°ã¯ãå€éšã®ç¶æ ãå€æŽããããå€éšããã®æ å ±ã«äŸåããããããããåãå ¥åã«å¯ŸããŠå¿ ãããåãåºåãè¿ããšã¯éããŸãããå¯äœçšã¯é¿ããããªãå Žé¢ããããããé¢æ°åããã°ã©ãã³ã°ã§ã¯ãã®åœ±é¿ãæå°éã«æããããšãæšå¥šãããŸãã
3.2 é«éé¢æ°ã®æŽ»çš
é«éé¢æ°ãšã¯ãä»ã®é¢æ°ãåŒæ°ãšããŠåãåã£ãããé¢æ°ãè¿ãé¢æ°ãæããŸãããã®ç¹æ§ã«ãããããã°ã©ã ã®æœè±¡åºŠãé«ããããç°¡æœã§èªã¿ãããã³ãŒããæžãããšãå¯èœãšãªããŸãã
äŸãã°ãmap
ãfilter
ã®ãããªé¢æ°ã¯é«éé¢æ°ã®äžäŸã§ãããããã®é¢æ°ã掻çšããããšã§ããªã¹ããé
åã«å¯ŸããŠãç¹°ãè¿ãã®åŠçãå¹ççã«é©çšããããšãã§ããŸããé«éé¢æ°ã®å©ç¹ã¯ãæ¢åã®é¢æ°ãçµã¿åãããŠæ°ããé¢æ°ãçæããæè»æ§ã«ãããŸãã
3.3 ååž°ãšååž°çãªããŒã¿æ§é
ååž°ã¯ãé¢æ°ãèªåèªèº«ãåŒã³åºãããšãæããŸããé¢æ°åããã°ã©ãã³ã°ã«ãããŠãååž°ã¯ç¹°ãè¿ãã®åŠçãå®çŸããäž»èŠãªæ段ãšãªããŸãã
ååž°çãªããŒã¿æ§é ãç¹ã«ãªã¹ãã¯é¢æ°åããã°ã©ãã³ã°ã«ãããŠé »ç¹ã«åãäžãããããããã¯ã§ããäŸãã°ãRacketã«ããããªã¹ãã¯ååž°çãªæ§è³ªãæã£ãŠãããcar
é¢æ°ã§å
é ã®èŠçŽ ããcdr
é¢æ°ã§æ®ãã®ãªã¹ããååŸããããšã§ããªã¹ãã®èŠçŽ ã«é 次ã¢ã¯ã»ã¹ããããšãã§ããŸãããã®ãããªæ§é ã¯ãååž°çãªé¢æ°ã®å®è£
ã«ãããŠæ¥µããŠæçšã§ãã
4. Racketã«ãããé¢æ°åã®å®è·µ
é¢æ°åããã°ã©ãã³ã°ã®çè«ãåŠãã åŸãå ·äœçã«Racketã§ã©ã®ããã«ãããã®æŠå¿µãå®è·µããã®ããæ¢æ±ããŸããããã§ã¯ãåºæ¬çãªæäœããé«åºŠãªæ©èœãŸã§ãRacketã§ã®é¢æ°åããã°ã©ãã³ã°ã®ãã¯ããã¯ã詳ããèŠãŠãããŸãã
4.1 ãããããã£ã«ã¿ãŒããªãã¥ãŒã¹ã®å©çš
ãããããã£ã«ã¿ãŒããªãã¥ãŒã¹ã¯é¢æ°åããã°ã©ãã³ã°ã®3倧èŠçŽ ãšãèšããæäœã§ãããããã¯ãªã¹ããã³ã¬ã¯ã·ã§ã³ã«å¯Ÿããæäœãæœè±¡åããå¹ççã«ããŒã¿ãåŠçããããã®ããŒã«ãšãªããŸãã
ãããã¯åèŠçŽ ã«å¯ŸããŠé¢æ°ãé©çšãããã®çµæããæ°ãããªã¹ããçæããŸãããã£ã«ã¿ãŒã¯ããæ¡ä»¶ãæºããèŠçŽ ã ããåãåºããŠæ°ãããªã¹ããäœãããã®æäœã§ããäžæ¹ããªãã¥ãŒã¹ã¯ãªã¹ãã®èŠçŽ ãå·Šããé ã«çµåããåäžã®çµæãåŸãããã®æäœã§ãã
4.2 ã©ã ãé¢æ°ãšã¯ããŒãžã£
ã©ã ãé¢æ°ã¯ãååãæããªãäžæçãªé¢æ°ãå®çŸ©ããããã®æ©èœã§ããRacketã§ã¯lambda
ããŒã¯ãŒãã䜿çšããŠç°¡åã«ã©ã ãé¢æ°ãäœæããããšãã§ããŸãããã®æ©èœã¯ç¹ã«ãçãé¢æ°ãäžåºŠã ã䜿çšããå Žåããé«éé¢æ°ã®åŒæ°ãšããŠé¢æ°ãæž¡ãå Žåã«éåžžã«åœ¹ç«ã¡ãŸãã
ã¯ããŒãžã£ã¯ãé¢æ°ãšãã®é¢æ°ãå®çŸ©ãããç°å¢ãšãçµã¿åããããã®ã§ããããã«ãããé¢æ°å€éšã®å€æ°ã«ã¢ã¯ã»ã¹ããããšãã§ããŸããRacketã«ãããã¯ããŒãžã£ã®å©çšã¯ãããŒã¿ã®ã«ãã»ã«åãç¶æ ã®ä¿æã«åœ¹ç«ã€æè¡ãšããŠé »ç¹ã«äœ¿çšãããŸãã
4.3 ãã¿ãŒã³ãããã³ã°ãšåå²
ãã¿ãŒã³ãããã³ã°ã¯ãããŒã¿ã®æ§é ãå€ã«åºã¥ããŠåŠçãåå²ãããæè¡ã§ããRacketã§ã¯ãmatch
é¢æ°ã䜿çšããŠãè€éãªããŒã¿æ§é ã«å¯ŸããŠãç°¡æœãªèšè¿°ã§ãããã³ã°ãè¡ãããšãã§ããŸãã
åå²ã«é¢ããŠã¯ãRacketã«ã¯cond
ãcase
ãªã©ã®è€æ°ã®åå²æ©èœãæäŸãããŠããŸããããããé§äœ¿ããããšã§ãããã°ã©ã ã®å¶åŸ¡ãããŒãå¹æçã«æ§ç¯ããããšãã§ããŸãã
5. é¢æ°åããã°ã©ãã³ã°ã®å¿çšäŸ
ãããŸã§ã®ã»ã¯ã·ã§ã³ã§ãé¢æ°åããã°ã©ãã³ã°ãšRacketã®åºæ¬çãªèŠç¹ã«ã€ããŠåŠã³ãŸãããæ¬ã»ã¯ã·ã§ã³ã§ã¯ããããã®ç¥èãåºã«ãå®éã®ããã°ã©ã éçºã§ã®å¿çšäŸãæ¢æ±ããŸãã
5.1 ç¶æ 管çã®æŠç¥
é¢æ°åããã°ã©ãã³ã°ã®äžå¿çãªç¹åŸŽã¯ãç¶æ ãå€æŽããã«åŠçãé²ããããšã«ãããŸãããã®èãæ¹ã¯ãç¶æ 管çã®æŠç¥ã«ãããŠã匷åãªããŒã«ãšããŠæ©èœããŸããç¶æ ãçŽæ¥å€æŽãã代ããã«ãæ°ããç¶æ ãè¿ãé¢æ°ã䜿çšããããšã§ãããã°ã©ã ã®è€éããå€§å¹ ã«åæžããããšãã§ããŸãã
5.2 ã¢ãžã¥ã©ãªãã£ãšã³ãŒãã®åå©çš
é¢æ°åããã°ã©ãã³ã°ã¯ãåé¢æ°ãç¬ç«ããŠåäœãããããã¢ãžã¥ã©ãªãã£ãé«ãŸããŸãããã®ç¹æ§ã¯ãã³ãŒãã®åå©çšæ§ãåäžãããä¿å®ãæ¡åŒµã容æãªããã°ã©ã ãäœæããäžã§éåžžã«æå©ã§ããRacketã§ã¯ãã¢ãžã¥ãŒã«ã·ã¹ãã ã掻çšããããšã§ãã³ãŒãã®çµç¹åãåå©çšãããã«é²åãããããšãå¯èœã§ãã
5.3 Racketã«ãããé¢æ°åãã¶ã€ã³ãã¿ãŒã³
ãã¶ã€ã³ãã¿ãŒã³ã¯ãç¹å®ã®åé¡ã解決ããããã®äžè¬çãªã¢ãããŒãã瀺ããã®ã§ããé¢æ°åããã°ã©ãã³ã°ã«ãããŠãããããã®ãã¿ãŒã³ã¯ååšããRacketã«ãããŠãå€ãã®é¢æ°åãã¶ã€ã³ãã¿ãŒã³ã䜿çšãããŠããŸããäŸãã°ãã¢ãããé¢æ°åæãªã©ã®ãã¿ãŒã³ããããŸãããããã®ãã¿ãŒã³ãç解ããé©åã«äœ¿çšããããšã§ãããé«å質ãªãœãããŠã§ã¢ã®éçºãå¯èœãšãªããŸãã
6. é¢æ°åããã°ã©ã ã®å©ç¹ãšæ¬ ç¹
é¢æ°åããã°ã©ãã³ã°ã¯ããã®ç¬ç¹ã®æèæ¹æ³ãšå®è£ ã¹ã¿ã€ã«ãããç¬èªã®å©ç¹ãšæ¬ ç¹ãæã¡ãŸãããã®ã»ã¯ã·ã§ã³ã§ã¯ããããã®èŠç¹ãæããã«ããããã°ã©ã ã®ããã©ãŒãã³ã¹ãã³ãŒãã®ä¿å®æ§ããããŠåŠã³ããããšãã£ã芳ç¹ããè°è«ããŸãã
6.1 ããã©ãŒãã³ã¹ãšå¹çæ§
é¢æ°åããã°ã©ã ã¯ãäžè¬ã«ã€ãã¥ãŒã¿ãã«ãªããŒã¿æ§é ã䜿ããããå¯äœçšãå°ãªãããã°ãçºçãã«ãããšãããŸãããã ããã€ãã¥ãŒã¿ãã«ãªããŒã¿æ§é ã¯æ°ãããªããžã§ã¯ãã®çæãé »çºãããããã¡ã¢ãªäœ¿çšéãå¢ãããããšããæ¬ ç¹ããããŸãã
6.2 ã³ãŒãã®ä¿å®æ§ãšæ¡åŒµæ§
é¢æ°åããã°ã©ã ã¯ãã¢ãžã¥ãŒã«æ§ãé«ããã³ãŒãã®åå©çšã容æã§ããããã¯ãé·æçã«èŠãã°ä¿å®ãšæ¡åŒµã容æã§ãããšãã倧ããªå©ç¹ã«ã€ãªãããŸããããããã®äžæ¹ã§ãåããŠè§Šãã人ã«ãšã£ãŠã¯ãé¢æ°åã®æèæ¹æ³ãçŽæã«åããå Žåããããšããæ¬ ç¹ããããŸãã
6.3 åŠã³ããããšå¯èªæ§
é¢æ°åããã°ã©ã ã¯ããã®æ§é ãæå¿«ã§ãããããäžæŠæ £ããã°éåžžã«èªã¿ãããã³ãŒããæžããŸãããŸããæ°åŠçãªçè«ã«åºã¥ããããè«ççãªæèã匷ãæ±ããããŸããããã¯ãäžæ¹ã§åŠã¶éã®éå£ãšãªãå Žåãããããã泚æãå¿ èŠã§ãã
å©ç¹ | æ¬ ç¹ |
---|---|
ãã°ã®çºçãå°ãªã | ã¡ã¢ãªäœ¿çšéãå¢ãããã |
ã³ãŒãã®ä¿å®æ§ãé«ã | ååŠè ã«ã¯é£ããå Žåããã |
æå¿«ã§å¯èªæ§ã®é«ãã³ãŒã | åŠç¿ã®éå£ãé«ãå Žåããã |
以äžã®ããã«ãé¢æ°åããã°ã©ãã³ã°ã¯äžé·äžçã®åŽé¢ãããããããžã§ã¯ãã®èŠæ±ãããŒã ã®ç¶æ³ã«å¿ããŠãé©åãªæ¡çšãæ€èšããããšãéèŠã§ãã
7. ãŸãšã
æ¬èšäºãéããŠãé¢æ°åããã°ã©ãã³ã°ã®æ žå¿ãããRacketã«ãããå ·äœçãªå®è·µäŸããã®å©ç¹ãšæ¬ ç¹ãŸã§ãåŠãã§ããŸãããé¢æ°åããã°ã©ã ã¯ãã®ç¹æ§ããå€ãã®ã¡ãªããã享åããããšãã§ããŸãããããã«äŒŽãæ¬ ç¹ãç¡èŠã§ããªããã®ããããŸãã
7.1 Racketã§ã®é¢æ°åããã°ã©ãã³ã°ã®å°æ¥æ§
Racketã¯ãSchemeã®æ¹èšãšããŠéçºãããé«åºŠãªãã¯ãã·ã¹ãã ããªãããªã©ã€ãã©ãªã»ãããæã€ããšãããé¢æ°åããã°ã©ã ã®å®è·µã«æé©ãªèšèªãšãããŠããŸãããã®æè»æ§ãšæ¡åŒµæ§ãããä»åŸãå€ãã®éçºè ã«æãããããšã§ãããã
7.2 ãããªãåŠã³ãžã®ã¹ããã
é¢æ°åããã°ã©ãã³ã°ã¯ãåå¿è ã«ã¯ããŒãã«ãé«ããšæãããããããããŸãããããã®èåŸã«ããæ°åŠççè«ããã³ãŒãã®ä¿å®æ§ã»å¯èªæ§ã®åäžãšãã£ãé·æçãªã¡ãªãããè¿œæ±ããããšãã§ããŸããããã«æ·±ãåŠã³ããæ¹ã¯ãRacketã®å ¬åŒããã¥ã¡ã³ããé¢æ°åããã°ã©ãã³ã°ã®åºæ¬ãåŠã¶æžç±ãèªãããšãããããããŸãã
æšå¥šãªãœãŒã¹ | 説æ |
---|---|
Racketå ¬åŒããã¥ã¡ã³ã | èšèªã®è©³çŽ°ã䜿çšæ¹æ³ã詳ããåŠã¶ããšãã§ããã |
é¢æ°åããã°ã©ã ã®æç§æž | é¢æ°åã®åºæ¬æŠå¿µããå¿çšãŸã§ãåŠã¶ããšãã§ããã |
é¢æ°åããã°ã©ãã³ã°ã®éã¯æ·±ããå€ãã®çºèŠãšåŠã³ãåŸ ã£ãŠããŸãããã®èšäºãããã®ç¬¬äžæ©ãšããŠã®åèã«ãªãã°å¹žãã§ãã