SICPãä½å¹´ãåã«èªãã§ããã®ã§ãããéä¸ã¾ã§èªãã§ãã®ã¾ã¾ç©ãèªã«ãªã£ã¦ã¾ããããã®ãã¨ããµã¨æãåºããã®ã§ã¾ãèªã¿å§ãã¦ããã¾ããèªã¿ãªããã³ã¼ãæ¸ãæã®ç°å¢ã«ã¤ãã¦ã®æ¥è¨ã§ãã
SICPã¨ã¯è¨ç®æ©ããã°ã©ã ã®æ§é ã¨è§£éã¨ããæ¬ã®ç¥ç§°ã
翻訳ããPDFãå ¬éãã¦ããã¦ããæ¹ããããã¨ã¦ãæè¬ã§ãã
ã©ããªæãã§èªããã
REPLã使ãï¼ Guile + rlwrap ï¼
èªã¿ãªããã³ã¼ãåçµããããªã£ããåé¡ã解ãããã¨ããããã¾ããããããã¨ãã¯Guileã®REPLã使ã£ã¦ã¾ããPCã¯macãchromebookãæ¥ã«ãã£ã¦ã
guile
ã§èµ·åãã¦ã ,h
ã§ãã«ãã ,q
ã§çµäºã
$ guile scheme@(guile-user)> (+ 1 2) $1 = 3 scheme@(guile-user)> ,h Help Commands [abbrev]: ,help [all | GROUP | [-c] COMMAND] [,h] - Show help. ... scheme@(guile-user)> ,q
ãã®ã¾ã¾ã ã¨ã Ctrl-p
ã Ctrl-a
ãå¹ããè¾ãã®ã§rlwapã使ãã¾ããrlwrapã¯å¯¾è©±ã¢ã¼ãã§åããããªã³ãã³ãã«readlineã®æ©è½ãå¾ä»ããã¦ãã³ãã³ãå±¥æ´ã Ctrl-p
ã§é¡ããããã«ãããEmacsãã¼ãã¤ã³ãã使ããããã«ãã¦ããã¾ãã
guileåä½ã§èµ·åã㦠Ctrl-p
ããã¨ä»¥ä¸ã®ããã«ãªãã
$ guile scheme@(guile-user)> (+ 1 2) $1 = 3 scheme@(guile-user)> ; Ctrl-pãå ¥åããã¨... scheme@(guile-user)> ^[[A ; Ctrl-p ã使ããªãï¼
rlwrapã¨ãã£ããã«ä½¿ããrlwrap便å©ï¼
$ rlwrap guile scheme@(guile-user)> (+ 1 2) $1 = 3 scheme@(guile-user)> ; Ctrl-pãå ¥åããã¨... scheme@(guile-user)> (+ 1 2) ; å±¥æ´ã使ããï¼
ãã¡ã¤ã«ã«æ¸ãã¦èªã¿è¾¼ã
é¢æ°ã®è¡æ°ãå¢ãã¦ããã¨REPLã§è¨è¿°ããã®ãè¾ããééãã¦æ¶ãã¡ãã£ããæ²ããã®ã§ãVS Codeã§æ¸ãã¦ãã¡ã¤ã«ã«ä¿åãã¦ããREPLã§èªã¿è¾¼ãã§å®è¡ãã¦ã¾ãã
VS Codeã®schemeæ¡å¼µæ©è½ã§ã³ã¼ããã¤ã©ã¤ãã¨ã¤ã³ãã³ãã¥ãã楽ã«ã
rlwrapã® -c
ãªãã·ã§ã³ã使ãã¨ãã¡ã¤ã«åã®è£å®ãå¹ãã®ã§ä¾¿å©ã
;; fib.scm (define (fib x) (cond ((= x 0) 0) ((= x 1) 1) (else (+ (fib (- x 1)) (fib (- x 2))))) )
$ rlwrap -c guile scheme@(guile-user)> (load "fib.scm") ; tabã§ãã¡ã¤ã«åã®è£å®ãå¹ãï¼ scheme@(guile-user)> (fib 5) $1 = 5
,trace
,trace使ãã¨ãããªé¢¨ã«è¡¨ç¤ºãããã
scheme@(guile-user)> ,trace (fib 5) trace: (fib 5) trace: | (fib 3) trace: | | (fib 1) trace: | | 1 trace: | | (fib 2) trace: | | | (fib 0) trace: | | | 0 trace: | | | (fib 1) trace: | | | 1 trace: | | 1 trace: | 2 trace: | (fib 4) trace: | | (fib 2) trace: | | | (fib 0) trace: | | | 0 trace: | | | (fib 1) trace: | | | 1 trace: | | 1 trace: | | (fib 3) trace: | | | (fib 1) trace: | | | 1 trace: | | | (fib 2) trace: | | | | (fib 0) trace: | | | | 0 trace: | | | | (fib 1) trace: | | | | 1 trace: | | | 1 trace: | | 2 trace: | 3 trace: 5
æ«å°¾å帰ã®å½¢ã«æ¸ãæããã¨ãå復å¦çã«ãªã£ã¦ããã¨ãåãããã¡ãªã¿ã« fib
å
é¨ã§ fib-iter
ãè¨è¿°ãã㨠,trace
ã®åºåã«ã§ã¦ããªããæ®å¿µãåºåãããæ¹æ³ããã®ããªã
;; fib.scm (define (fib x) (fib-iter x 1 0) ) (define (fib-iter x a b) (cond ((= x 0) b) (else (fib-iter (- x 1) (+ a b) a))) )
scheme@(guile-user)> ,trace (fib 5) trace: | (fib 5) trace: | (fib-iter 5 1 0) trace: | (fib-iter 4 1 1) trace: | (fib-iter 3 2 1) trace: | (fib-iter 2 3 2) trace: | (fib-iter 1 5 3) trace: | (fib-iter 0 8 5) trace: | 5
ããã¾ã
ãããªæãã§èªã¿é²ãã¦è¡ãããåé¡è§£ãããã¨ããããgistã«è¿½å ãã¦ããã£ããªãã