ãã¯ãã¼ã¸ã£ããããªã«ï¼ãã£ã¦ã¨ã³ããªã¼ã§ãããã¯ãã¼ã¸ã£ãã£ã¦è¨èã®æå³ãããããªããã¨è¨ã£ãã®ã§ãããlethevertããããã®ãã©ãã¯ããã¯ãshiroããã®ã³ã¡ã³ãã§å¤å°ã¯ç¶æ³ãè¦ãã¦ãã¾ããã
解決ãããã£ãã¨ããã§ä½è¨ãªãã¨ããã£ã¦æ··ä¹±ã«æè»ãããã¦ã¿ãã
ã§ç¢ºãã«æ··ä¹±ã¯ãããã®ã®ãããã«lethevertããã®è§£èª¬ãç¶ããã®ã§ãèªåãªãã®ç®æã¯ã¤ããæ°ããã¾ãã
æè¿ã®å¾å
ã©ã ãå¼ï¼ã«ç¸å½ãããã®ï¼ããã¼ã¿ã®ããã«æ±ããã³ã¼ããããã¯ãã¯ãã¼ã¸ã£ã¨å¼ã¶ã®ãæè¿ã®å¾åã¿ããã§ãããããããããã°ã©ãã³ã°è¨èªã®æ©è½ï¼è½åã¨ãã¦æãããã¨ãå¤ãããã§ããããã³ãã«è¨èªã¯ã¯ãã¼ã¸ã£ã使ãããã¿ãããªç¨æ³ã§ã
å¤ãã®äººãããããæå³ã§ä½¿ããªãããæ¬æ¥ã¯â¦ãã¿ãããªãã¨è¨ã£ã¦ããããããªãã®ã§ãå¥ã«ãããã
ãã©ã ãå¼ï¼ç°å¢ãã¨ãããã¼ã¿æ§é
åèªèº«ã¯ãã¯ãã¼ã¸ã£ï¼ã©ã ãå¼ï¼ç°å¢ ã¨æã£ã¦ãã¾ãããsumiiããã®è¨ãããã¼ã¿æ§é ã®ä¸ã¤ãã«è¿ãç解ã§ãããã¤ã©ãã§ããæãè¾¼ãã ãã¯æ¶ãã¦ãã¾ãããããããã«ãã¦ãæã®è©±ãªã®ã§ããã¯ãã¼ã¸ã£ãã®æå³ãæ¡æ£ããåã®ãã¨ã§ãããï¼ã¨ã¯è¨ã£ã¦ããã©ã³ãã£ã³ã®ãªãªã¸ãã«è«æã¨ãã¯å ¨ç¶ç¥ãã¾ãã*1ï¼ã
åã®æãã¨ãã¦ã¯ã(λx.(a*x + b), {a : 2, b : 1})ã¿ãããªã®ãã¯ãã¼ã¸ã£ã§ããã㯠λx.(2*x + 1) ã表ç¾ãã¦ãããã¨ã«ãªãã¾ãã{a : 2, b : 1}ã¯è¨å·è¡¨ï¼ååã»å¤ãããï¼ã®ã¤ããã§ããè¨å·è¡¨ã¯ç°å¢ã¨ãå¼ã³ã¾ã*2ãletã使ã£ã¦ã(let a = 2, b = 1; λx.(a*x + b)) ã¨ã (let b = 1; (let a = 2; λx.(a*x + b))) ã¨æ¸ãã¦ãåãç¶æ³ã表ãã¾ãã
æåãå¾ããå¤ããããã©ã ãå¼
åãæ°ã«ãªã£ã¦ãããã¨ã¯ãã©ã ãå¼ï¼ç¡åé¢æ°ï¼ã®éãã¼ã«ã«å¤æ°ã®æ±ãã§ããããã§ä¾ã¨ãã¦åºããã®ãï¼
function makeAffineLinear(a) { return function(x) { return a*x + b; } }
ããã¯ãä¸æ¬¡é¢æ°ï¼affine linear functionï¼ãä½ãé¢æ°ã§ããä¸æ¬¡é¢æ°ã®å®ç¾©æï¼makeAffineLinearãå¼ã³åºããã¨ãï¼ãaã¯ç¢ºå®ãã¾ããbã¯ç¢ºå®ããã¨ã¯éãã¾ããã
js> var f = makeAffineLinear(2) // é¢æ° 2*x + b ãå®ç¾©ããã js> f(3) NaN js> var b = 1 js> f(3) 7 js> var b = 2 js> f(3) 8
å¤æ°fã«ã¯ λx.(2*x + b) ï¼ã«ç¸å½ããç¡åé¢æ°ï¼ãå ¥ã£ã¦ã¾ãããbãæªå®ç¾©ã 㨠f(3) = NaNï¼NaN㯠not a numberï¼ãb = 1 ã§ã¯ f(3) = 2*3 + 1 = 7ããã㦠b = 2 ã§ã¯ f(3) = 2*3 + 2 = 8ãã¨ããã®ããã«ãbã®å¤ãã©ãã«ã§ãã§ããç¹ãåã¯æ°æã¡æªãã¦ãã¯ãã¼ã¸ã£ã¨å¼ãã§ããã®ãï¼ãã¨çåãæãã¦ããã®ã§ããï¼æ¬¡ã«ç¶ãï¼ã
éãã¼ã«ã«å¤æ°ã®æç¸
makeAffineLinearã®ä¾ã§ãä¸æ¬¡é¢æ°ãä½ãããæç¹ã§ãå®ã¯aã¨bã®æç¸ã¯æ±ºå®ããã¦ãã¾ãããæç¸ã決å®ããã¦ãããã¨ã¯ãå¤ãåºå®ããããã¨ã§ã¯ãªãã¦ãa, bã®å¤ã欲ããã¨ãã«ã©ããæ¢ãã°ããããåãã£ã¦ãããã¨ã§ããå¤æ°ã®ååããå¤æ°ã®å¤ãæ±ããã«ã¯è¨å·è¡¨ãå¼ãã¾ãããã©ã®è¨å·è¡¨ãå¼ãã°ãããã決ã¾ãã£ã¦ãã¨ã§ãã
JavaScriptãä¾ã¨ãã¦èª¬æããã¨ãmakeAffineLinear(2) ã¨ããå¼ã³åºãã§ï¼é常ã®è¨èªã®ã¹ã¿ãã¯ãã¬ã¼ã ã«ç¸å½ããï¼å¼ã³åºããªãã¸ã§ã¯ããçæããã¾ããå¼æ°å¤æ°ãå«ãããã¼ã«ã«å¤æ°ã¯ããã®å¼ã³åºããªãã¸ã§ã¯ãã®ããããã£ã§ããå¼ã³åºããªãã¸ã§ã¯ãã«ååã¯ããã¾ããããä»®ã«_callã ã¨ããã¨ããã¼ã«ã«å¤æ°aã®å¤ã¯_call["a"]ã§æ±ã¾ãã¾ããä¸æ¬¡é¢æ°Î»x.(a*x + b)ã¨è¨å·è¡¨_callãçµã«ãã(λx.(a*x + b), _call)ã§aã®æç¸ã¯æ±ºå®ããã¾ããã§ãã_call["b"]ã¯å¤ãæã¡ã¾ããã
ããbã¯ã©ãããï¼ JavaScriptã®ã«ã¼ã«ã§ã¯ãéãã¼ã«ã«å¤æ°ã¯å¸¸ã«å¤§åå¤æ°ã§ã*3ãããã¦ã大åå¤æ°ã¯å¤§åãªãã¸ã§ã¯ãã®ããããã£ã§ãã大åãªãã¸ã§ã¯ããååãä»ãã¦ã¾ãã*4ããä»®ã«_globalã¨ããã¨ã大åå¤æ°bã®å¤ã¯_global["b"]ã§ããçµå±ãä¸æ¬¡é¢æ°Î»x.(a*x + b)ã®bãæ±ããã«ã¯è¨å·è¡¨_globalãå¼ãã¾ããã¤ã¾ãã_callã«å¤ããªããã°_globalãå¼ããã¨ã«ãªãã¾ãã
ã ãããã®æãã¨ãã¦ã¯ãmakeAffineLinear(2)ã«ãã (λx.(a*x + b), _call, _global) ã¿ãããªãã¼ã¿ãä½ãããããã_callã_globalã¨ããè¨å·è¡¨ï¼ç°å¢ï¼ã«ããã¹ã³ã¼ããã§ã¼ã³ãã©ã ãå¼Î»x.(a*x + b)ã«ãã£ã¤ãã¦ãããã¼ã¿ã¨è¦ã¦ããã§ãããã
æç¸ã¯æ±ºã¾ã£ã¦ãå¤ã¯å¤ãããã
bã®å¤ãå¾ããèªç±ã«è¨å®ã§ããã®ã¯ãè¨å·è¡¨_globalããã¾ãã¾ãã¥ã¼ã¿ãã«ï¼å¤æ´å¯è½ï¼ã ã£ãããã§ããã¡ãã£ã¨ç´°å·¥ãããã¨ããã¼ã«ã«ãªaãããå¾ããå¤æ´ã§ãã¾ãã
function makeALFuncAndASetter(a) { return [ // affine linear function function(x) { return a*x + b; }, // setter for the variable a function(x) { a = x; } ]; }
js> var alas = makeALFuncAndASetter(2) js> var f = alas[0] js> var setA = alas[1] js> var b = 1 js> f(3) 7 js> setA(1) js> f(3) 4
ããã«ãä½çºçã§ãããfunction(x) {a = x;}
ãçµç±ãã¦å¼ã³åºããªãã¸ã§ã¯ãã®å
é¨ãããã£ã¦ã¾ããããããã¨ãλx.(a*x + b) ãä½ã£ãå¾ã§ã©ãã«ã§ãæåãå¤ãããã¨ãã§ãã¾ãããa, b ã®å¤ãæ¯é
ãã¦ããè¨å·è¡¨ã¯ãã£ã¨åããã®ã使ãç¶ãã¾ãã
éãã¦ããããã¯ãã¼ã¸ã£ï¼
ããã§ãmakeAffineLinear(2)ã§çæï¼å®ç¾©ï¼ãããã©ã ãå¼ã§ã¯ãéãã¼ã«ã«å¤æ°ã®æç¸ï¼ä½¿ç¨ããè¨å·è¡¨ï¼ã¯æ±ºå®æ¸ã¿ã ãããã®è¨å·è¡¨ãå¤æ´å¯è½ãªãå¤ã¯å¤ãããããããªãäºæ ã¯ãããã¾ããã
åèªèº«ã¯ãéãã¼ã«ã«å¤æ°ã®å¤ãã©ãæ±ããã¹ãã決å®ãã¦ãªããã¤ã¾ãâéãã¦ããâç¶æ ã ã¨ã¯ãã¼ã¸ã£ã¨å¼ã¶ã®ã«æµæããã£ãã®ã§ãããmakeAffineLinearã®ä¾ã§ããã¯ããã©ã ãå¼ã¨ç°å¢ï¼è¨å·è¡¨ï¼ãä¸ç·ã«ãªã£ããã®ãçæããã¾ã -- ãããã¯ãã¼ã¸ã£ã§ããããã£ã¨ããåé ã§è¿°ã¹ãæ¨ä»ã®å¾åããããã°ããããªãã¨ã«ãã ããå¿ è¦ã¯å ¨ç¶ãªãã®ããããã¾ããã
å®ã¯ããéãã¦ãããã£ã¦è¨ãæ¹ã®èªæºã®ã»ãã§ãã¾ã ã«åã¯æ··ä¹±ãã¦ããã®ã§ããããã®ããã·ã¯å¥ãªæ©ä¼ã«ãã¾ãã
*1:ã©ã³ãã£ã³ï¼Landinï¼ã¨ãã人åã¯è³ã«ãããã¨ãããã¾ããæããã©ã³ãã£ã³ãã©ãããããã¹ãã¬ã¼ãã£ããããããã¨ãè¨ã£ã¦ãã人ãããã®ã§ããªãããã©ããããããããé¨åã¯ã¾ã£ããæ¶ãã¦ãªãã
*2:ä»å¤ï¼valuationï¼ã¨ãå¤å²ãå½ã¦ï¼value assignmentï¼ã¨å¼ã¶ãã¨ãããã¾ãã
*3:ãã©ã¦ã¶å ã§åä½ããã¨ãã¯éã£ãã¹ã³ã¼ãã³ã°ã«ãªããã¨ãããå¾ã¾ãã
*4:å®éã«ã¯ãå¦çç³»å®è£ ãã¨ã«åºæã®ååãä»ãã¦ã¾ããä¾ãã°ãwindowã¨ãã