å¤å¤ã«ã¤ãã¦æ¬æ°ã§èãã¦ã¿ã
先日のエントリã®åå¿ã¨ãã¦ãå¤å¤ã®æ¹å¤ããã¦ããããã«åãåãããæ¹ããã¾ããã å®éã«ã¯ãå¤å¤ã®æ¹å¤ããã¦ããã®ã§ã¯ãªããGoè¨èªã®ãå¤å¤ã¨ããè¦ãããã©éããã®ããããã¨ããä»æ§ãæ¹å¤ãããã®ã§ããã
ã¾ããã¿ãã«ã«ãã ãã£ã¦ããã¨ããåãåãæ¹ãããæ¹ããã¾ããã ãã®ã¨ã³ããªã§ã¯ããã¿ãã«ã«ãã ãã£ã¦ããã®ã§ã¯ãªããå¤å¤ã«ãã ãã£ã¦ããã®ã ãã¨ãããã¨ã説æãããã¨æãã¾ãã ãã®ã¨ã³ããªã§åºã¦ããã³ã¼ãã¯è¨åããªãéãå¦æ³ä¸ã®ãã®ã§ãå®éã®è¨èªã®ã³ã¼ãã§ã¯ããã¾ããã
é·ããã3è¡ã§ã
- ã¹ã¿ãã¯ãã·ã³ã¨å¤å¤ã¯ä»²è¯ããç¶ç¶ã¨å¤å¤ã仲è¯ãã
- å¤å¤ã¯å¤å¤ãã¿ãã«ã¯ã¿ãã«ãã¿ããªã¡ãã£ã¦ã¿ããªããã
- å¤å¤ã¨ã¯ãã«ã³ãã§åºåãããåãªãè¤æ°ã®å¤ã ããå¦æ³ã ãã©ãã
ããã§æºè¶³ãã¦ä»äºã«æ»ã£ã¦ããã ãã¦æ§ãã¾ããã以ä¸ãªãã±ã
å¤å¤ã¨ã¿ãã«ã®éã
ã¾ããå¤å¤ã¨ã¿ãã«ã®æå³çãªéãã«ã¤ãã¦ãã¯ã£ããããã¦ããã¾ãããã ãã ããå¤å¤ã¯ã¿ãã«ã¨éã£ã¦æ±ããè¨èªãå°ãªã*1ãããåè¨èªã§ã®éãããããªãã«å¤§ãããããããã§ç´¹ä»ããéãã¯åèç¨åº¦ã«èãã¦ãã ããã
ä»ã®å¤ã®ä¸é¨ã«ãªãããã©ãã
ã¿ãã«ã¯ä½ã®å¶ç´ããªããåãªãå¤ã§ãã ãã®ãããä»ã®å¤ã®ä¸é¨ã«ãªãã¾ã*2ã å½ç¶ãã¿ãã«ã®è¦ç´ ã«ã¿ãã«ãå ¥ããã¨ãã風ã«ãå ¥ãåæ§é ãåãã¾ãã
ããã«å¯¾ãã¦ãå¤å¤ã¯ä»ã®å¤ã®ä¸é¨ã«ã¯ãªãã¾ããã ä¾ãã°ãã¯ã©ã¹ã®ãã£ã¼ã«ãã«å¤å¤ãå«ããã¨ãã§ãã¾ããããå¤å¤ã®è¦ç´ ã¨ãã¦å¤å¤ãå«ããã¨ãã§ãã¾ããã ããããå¶ç´ã®ä»ããåã¨è¦ãªããã¨ãã§ãã¾ãããåã«å¤å¤ã¯ãã¡ã¼ã¹ãã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã§ã¯ãªãã¨èãã¦ãããã§ãããã
å¤å¤ã¯å¶éãããã¿ãã«ãªã®ã
ããã¾ã§ã§ã¯ãå¤å¤ã¯å¶éãããã¿ãã«ã§ãããå¤å¤ã«ã¯ä½ã®ã¡ãªããããªãã¨ããæããªãããããã¾ããã ããããå¤å¤ã«ã¯å¹çã¨ãã大ããªã¡ãªãããããã®ã§ãã ãã®è©±ã«å ¥ãåã«ãå¤å¤ã¨ç¸æ§ã®ãããã®ã«ã¤ãã¦è¦ã¦ããã¾ãããã ã¹ã¿ãã¯ãã·ã³ã¨ãç¶ç¶ã§ãã
ã¹ã¿ãã¯ãã·ã³ã¨å¤å¤
ã¾ãã¯ã¹ã¿ãã¯ãã·ã³ã§ãã ã¹ã¿ãã¯ãã·ã³ã¨ããã®ã¯ãã¹ã¿ãã¯ãç¨ãã¦è¨ç®ãè¡ãè¨ç®æ©ã®ãã¨ãè¨ãã¾ãããããã§ã¯è©³ç´°ã«ã¯è¸ã¿è¾¼ã¿ã¾ããã Javaä»®æ³ãã·ã³ãã.NETã®CLRããRubyVM(æ§ç§°YARV)ãªã©ãã¹ã¿ãã¯ãã·ã³ããã¼ã¹ã«ãã¦ãã¾ããå°ãªãã¨ã30åã®ããã¤ã¹ã§ã¹ã¿ãã¯ãã·ã³ã¯åãã¦ãããã¨ã«ãªãã¾ããããããã
ã¹ã¿ãã¯ãã·ã³ã§ã®é¢æ°å¼ã³åºã
ã¹ã¿ãã¯ãã·ã³ã§ã¯ãå¼æ°ãã¹ã¿ãã¯ã«ç©ãã§ããé¢æ°ã«å¦çã移ãã ãã§é¢æ°å¼ã³åºããã§ãã¾ãã ä¾ãã°ãJavaã§æ¬¡ã®ãããªã¡ã½ãããæ¸ããã¨ãã¾ãããã
public static int add(int a, int b) { return a + b; }
ãã®ã¡ã½ããã add(10, 20)
ã®ããã«å¼ã³åºããå ´åã以ä¸ã®ãããªãã¤ãã³ã¼ããåºåããã¾ãã
bipush 10 // byteç¯å²ã«åã¾ãæ°å¤10ãpush bipush 20 // byteç¯å²ã«åã¾ãæ°å¤20ãpush invokestatic add // addã¡ã½ãããå¼ã³åºã
ãããã¹ã¿ãã¯ã®ç¶æ ãå«ãã¦å³ã«ããã¨ããã®ãããªæãã«ãªãã¾ãã
| | bipush 10 | | bipush 20 | 20 | invokestatic add | | | |---------->| 10 |---------->| 10 |----------------->| 30 | +----+ +----+ +----+ +----+
ã¾ãã«ãã¹ã¿ãã¯ã«å¼æ°ãç©ãã§ããé¢æ°ãå¼ã³åºããã¦ãã¾ãã ããã¦ãçµæã¯ã¹ã¿ãã¯ã«ç©ã¾ãã¾ãã
é¢æ°ããæ»ãå ´åã¯ã©ãã§ããããã
ä¸ã§ä½ã£ã add
ã®ãã¤ãã³ã¼ããè¦ã¦ã¿ã¾ãããã
iload_0 // 0çªç®ã®å¼æ°ãæ´æ°ã¨ãã¦push iload_1 // 1çªç®ã®å¼æ°ãæ´æ°ã¨ãã¦push iadd // ã¹ã¿ãã¯ã«ç©ã¾ãã2ã¤ã®æ´æ°ãå ç®ãçµæãpush ireturn // ã¹ã¿ãã¯ã«ç©ã¾ããæ°å¤ãæ»ãå¤ã¨ãã¦ã¡ã½ããããreturn
add(10, 20)
ã§å¼ã³åºãããå ´åã®ã¹ã¿ãã¯ã®ç§»ãå¤ããã¯ãã®ããã«ãªãã¾ãã
| | iload_0 | | iload_1 | 20 | iadd | | ireturn | |-------->| 10 |-------->| 10 |----->| 30 |--------> +----+ +----+ +----+ +----+
ã¹ã¿ãã¯ãã·ã³ä¸ã§ã®å¤å¤ã®è¡¨ç¾
ã¹ã¿ãã¯ãã·ã³ã§ã¯ãå¤å¤ã¯ã¹ã¿ãã¯ä¸ã«ç©ã¾ãã(è¤æ°ã®)å¤ã§ããããã¾ããã nåã®å¤ãç©ãã§é¢æ°ãå¼ã³åºãã¨ãããã¨ã¯ãnå¤ãå ¥åã«ããé¢æ°ãå¼ã³åºãã¨ãããã¨ã§ããã måã®å¤ãç©ãã ç¶æ ã§é¢æ°ããreturnããã¨ãããã¨ã¯ãmå¤ãåºåã¨ãã¦é¢æ°ãçµãããã¨ãããã¨ã§ã*3ã
ããã¯ã¨ã¦ããããã§ãããã ä¾ãã°Goè¨èªãã¹ã¿ãã¯ãã·ã³ä¸ã§å®è£ ããã¦ããã¨ãããã
func add(a int, b int) int { return a + b } func f(a int) (int, int) { return a, a * 2 } add(f(3))
ã¯ã
push 3 call f // 3ãç©ã¾ããç¶æ ã§fãå¼ã³åºããå®è¡ãçµããã¨ã¹ã¿ãã¯ã«å¤ã2ã¤ç©ã¾ãã¦ããã call add // 3ã¨6ãã¹ã¿ãã¯ã«ç©ã¾ããç¶æ ã§addãå¼ã³åºãã
ã¨è¡¨ç¾ããã¦ãããã¨ã§ãããã
ç¶ç¶ã¨å¤å¤
ç¶ç¶ã«ã¤ãã¦èª¬æãã ãã¨å»¶ã ã¨æ¨ªéã«ããã¦ãã£ã¦ãã¾ãã®ã§ã 解説ページ ã¸ã®ãªã³ã¯ã ãç½®ãã¦ããã¾ãã æªå®æé¨åãåãããããã¨ã¯ãã¯ããªãã¨æããã¾ããæ®å¿µã
ãã¦ãç¶ç¶ã¨å¤å¤ã®é¢ä¿ã§ãããç¶ç¶ã¨ã¯ã¤ã¾ãã¨ãããé¢æ°ããã®returnããreturnããå¾ã®å¦çã表ãé¢æ°å¼ã³åºããã¨èãã¦ãã¾ãããã¨ãããã¨ã§ã*4ã ãã®ã¨ãããç¶ç¶ã«æ¸¡ãããå¼æ°ãè¤æ°åãããã¨ãããã¨ã®æå³ãèãã¦ã¿ã¾ãããã ãç¶ç¶ã«æ¸¡ãããå¼æ°ãã¯ããreturnãããå¤ãã«å¯¾å¿ãã¾ãã®ã§ããããè¤æ°åããã¨ãããã¨ã¯ãè¤æ°ã®çµæãé¢æ°ããè¿ã£ã¦ããããã¨ãæå³ãã¾ãã ã¤ã¾ãã¯å¤å¤ã§ãã
ãã¹ã¦ãç¶ç¶ã§èããã°ãreturnã¯ãã¹ã¦é¢æ°ã®å¼æ°ã«ãªãã¾ãã ãã®ä¸çã«ããã¦ã¯ãå¤å¤ã¨ã¯åã«ç¶ç¶(ãããã¯é¢æ°)ã«æ¸¡ãå¼æ°ãè¤æ°ããã¨ããã ãã¨ãªãã¾ãã ãããã¨ã¦ããããã§ããã
ã¡ãã£ã¨ç¡çããã§ãããGoè¨èªã§ããã°ãã®ãããªã¤ã¡ã¼ã¸ã®ä¸çã§ãã
// ã¹ã¿ãã¯ãã·ã³ä¸ã§ã®å¤å¤ã®è¡¨ç¾ã§ä½¿ã£ãããã°ã©ã ãreturnãGoè¨èªã®å¤å¤ãªãã«è¡¨ç¾ãã¦ã¿ãä¾ func add(a int, b int, k func(int)) { k(a + b) // returnã®ä»£ããã«ç¶ç¶ãå¼ã³åºã } func f(a int, k func(int, int)) { k(a, a * 2) // returnã®ä»£ããã«ç¶ç¶ãå¼ã³åºã(å¤å¤ï¼) } func main() { f(3, func(a int, b int) { // fã¯å¤å¤ãé¢æ°ã®å¼æ°ã¨ãã¦æ¸¡ãã¦ãã add(a, b, func(x int) { fmt.Println(x) }) }) }
returnãGoè¨èªã®å¤å¤ã使ã£ã¦ãã¾ãããããã£ã¦ãããã¨ã¯Goè¨èªã®å¤å¤ã使ã£ãã³ã¼ãã¨åãã§ãã
ã¡ãªã¿ã«ãç¶ç¶ãæ±ããè¨èªã§ããSchemeã§ã¯ãå¤å¤ãä½ãé¢æ°ãããå®ç¾©ã§ãã¾ãã
(define (values . xs) (call/cc (lambda (k) (apply k xs))))
call/cc
㧠values
å¼ã³åºã以éã®å¦çãåãåã£ã¦ k
ã¨ãããã®ç¶ç¶ã« values
ã®å¼æ°ãå
¥ããã¨ãããã¾ãã«ãç¶ç¶ã®å¼æ°ãå¤å¤ã§ãããããã®ã¾ã¾è¡¨ããã³ã¼ãã«ãªã£ã¦ãã¾ãã
ãããã ã»ã»ã»ï¼
é¢æ°
ãã¦ãããã¾ã§ã¯å¤å¤ã¨ç¸æ§ã®ãããã®ãè¦ã¦ãã¾ããã ããããã¯ãé¢æ°ã«ã¤ãã¦å°ãèãã¦ã¿ã¾ãã
é¢æ°åããã°ã©ãã³ã°è¨èªã¨é¢æ°
ã¡ã¸ã£ã¼ãªé¢æ°åããã°ã©ãã³ã°è¨èªã§ã¯ãé¢æ°ã¯1å ¥å1åºåã¨ããã¢ãã«ã§ãã
å¤å ¥åãããå ´åã¯ãé¢æ°ãè¿ãé¢æ°ãã®ããã«é¢æ°ãå¤ã¨ãã¦æ±ããããã«ãããã¨ã§è§£æ±ºãã¦ãã¾ã(ã«ãªã¼åã¨ãããã¤)ã
// F# // let add x y = x + yã¨åã let add = fun x -> fun y -> x + y
å¤åºåãããå ´åã¯ã©ãã§ããããã ãããããé¢æ°ãåãåãé¢æ°ãã«ããå®ç¾ã§ãã¾ãã ããã¯ã¤ã¾ããç¶ç¶ã§è¦ãæ¹æ³ã§ã*5ã
// F# let f = fun x -> fun k -> k x (x * 2) // (k x)ã§è¿ã£ã¦ããé¢æ°ã«(x * 2)ãé©ç¨ // ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ã¼ã使ãã¨ã // f 3 (fun x y -> add x y) // ã¨ã // f 3 add // ã¨ãæ¸ãã f 3 (fun x -> fun y -> add x y // (add x)ã§è¿ã£ã¦ããé¢æ°ã«yãé©ç¨ )
ãã®ããã«ãé¢æ°åããã°ã©ãã³ã°è¨èªã§ã¯1å ¥å1åºåã®é¢æ°ã ãã§ãã¹ã¦ã表ããä¸çãä½ã£ã¦ããããã§ã*6ã ããã¯ããã§ãããã§ããã
æç¶ãåããã°ã©ãã³ã°è¨èªã¨é¢æ°
Goè¨èªãé¤ããå¤ãã®æç¶ãåããã°ã©ãã³ã°è¨èªã§ã¯ãé¢æ°ã¯å¤å ¥å1åºåã§ãã ãªãå ¥åã¯è¤æ°è¨±ãã®ã«ãåºåã¯1ã¤ã許ãã¦ãªãã®ã§ããããã èªåã¯Cè¨èªã1ã¤ã®æ»ãå¤ãã許ããªãããã«ãªã£ã¦ããã®ããã£ã¨å¼ããã£ã¦ããã®ã§ã¯ãªãããã¨èãã¦ãã¾ãã
ã¢ã»ã³ããªã¬ãã«ã¾ã§éãã¦ããã°ãããããã¬ã¸ã¹ã¿ã使ã£ã¦è¤æ°ã®å¤ãè¿ããããªãµãã«ã¼ãã³ãªããã¯æ®éã«æ¸ããããã§ãã x86ã§ããã°ãdivã¯eaxã«åããedxã«ä½ããæ ¼ç´ãã¾ããããããå¤å¤ãè¿ãã¦ããã¨è¦ãªãã¾ãã
ã¢ã»ã³ããªã¬ãã«ã¾ã§éããã°å¤å¤ã使ããã®ã«ãä»ã¾ã§ã®ããã°ã©ãã³ã°è¨èªã§ã¯ãããæå¹æ´»ç¨ãã¦ããªãã£ããã¨ã«ãªãã¾ãã ããã¯ãæç¶ãåããã°ã©ãã³ã°è¨èªãè¨ç®æ©ãå¹çãã使ããããã«é²åãã¦ãããã¨ãèããã¨ãå°ãä¸å¹¸ãªæãããã¾ãã Goè¨èªã¯ããããã£ãä¸çãã¶ã¡å£ãã¤ã³ãã¯ããæã£ãè¨èªã ã¨æãã¾ã*7ã
ã¿ãã«ã¨å¤å¤(ã¨æç¶ãåããã°ã©ãã³ã°è¨èª)
å¤å¤ã¯ãã¹ããã§ãã¾ãããä»ã®å¤ã®è¦ç´ ã¨ãªããã¨ãã§ãã¾ããã ãã®å¶ç´ã«ãã£ã¦å¤å¤ã¯ä½ãæã«å ¥ããã®ã§ããããã
ããã¯ãå¹çã§ãã å¤å¤ã¨åããããªãã®ã¨ã¿ããããã¨ãããã¿ãã«ã§ãããã¿ãã«ã¯ããã¾ã§1ã¤ã®å¤ã«è¤æ°ã®å¤ãããã¯ãããã®ã§ãã ããã¯ããå¦ç(ã¿ãã«ã®æ§ç¯)ããã¢ã³ããã¯ããå¦ç(ã¿ãã«ã®å解)ããã©ããã¿ãã¨ããããã§ã¯ããã¾ããã è¨èªå¦çç³»ã«ããã¦ãã¿ãã«ã®å¹çãä¸ãããã¨ãã試ã¿ã¯ããã¤ãããã¾ãããã¿ãã«ãå¤ã§ãã以ä¸ããã¹ã¦ã®ã¿ãã«ãæé©åã§ããããã§ã¯ããã¾ããã
ããã«å¯¾ããå¤å¤ã¯åãªãè¤æ°ã®å¤ã§ãããããèªä½ã¯å¤ã§ã¯ããã¾ãã(ã¹ã¿ãã¯ã«ç©ãã§ããã ããã¬ã¸ã¹ã¿ã«ä¸¦ãã§ããã ã)ã ãã®ãããããã¯ãã¢ã³ããã¯ãªã©ã¨ã¯ç¡ç¸ã®ä¸çã§çãã¦ãããã¾ãã
æç¶ãåããã°ã©ãã³ã°è¨èªã§ãé¢æ°ããã¡ã¼ã¹ãã¯ã©ã¹ã®å¤ã¨ãã¦ä½¿ãããããªè¨èªãå¢ãã¦ãã¾ãããã æç¶ãåããã°ã©ãã³ã°è¨èªã¯æ¬æ¥ãè¨ç®æ©ãå¹çãç¨ããä¿ã£ãã¾ã¾æ½è±¡çã«æ±ããããã«ããè¨èªã§ããã¹ãã§ã¯ãªãã§ãããã(ãã ãç°è«ã¯èªãã)ã ãã®å ´åãé¢æ°ã ã®ã¿ãã«ã ã®ããã¡ã¼ã¹ãã¯ã©ã¹ã§æ±ãããã¨ã«ãã ããããå¹çããæ±ãããã®ãæ±ãã«ã¨ã©ããã¨ããå²ãåã£ãè¨èªããã£ã¦ãããã¨æãã¾ãã
ãã ãã¦ã¼ã¶ã¼å®ç¾©ã§ããåã¨ã¸ã§ããªã¯ã¹ãããã¨ããã ãã§ã¿ãã«ãä½ãã¦ãã¾ãã®ã§ãå¤å¤ããªããããã«ãããã¡ãã¨ããã®ã¯ããããããã¾ããã
å¤å¤ã¨ã¯
ãã¦ãå¤å¤ã¨ã¯ä½è ã§ããããã
- ãåãªãã¹ã¿ãã¯ã«ç©ã¾ããå¤ã ãã
- ãåãªãç¶ç¶ã®å¼æ°ã ãã
- ãGoè¨èªã®å¤å¤ãå¤å¤ã ãã
è²ã ã¨æè¦ã¯ããã§ãããããããããã¯ãã«ã³ãã§åºåãããåãªãè¤æ°ã®å¤ã ããã¨ããå¦æ³ã®ä¸çã®è©±ã§ãã ã¡ãªã¿ã«ãä¸ã®3ã¤ã ã¨ä¸çªä¸ãä¸çªè¿ãã§ã(ããå¥ç©ã§ã)ã
å¤å¤ããããã¹ãï¼
æ¶ç©ºã®è¨èª(WillGoã¨ã§ããã¦ããã¾ããã)ãèãã¾ãã ãã®è¨èªã§ã¯ãå¤å¤ã¯ã«ã³ãã§åºåãããåãªãè¤æ°ã®å¤ã§ãã ã©ããããã¨ãè¦ã¦ã¿ã¾ãããã ã¾ãã¯ãå¤å¤ãè¿ãé¢æ°ã§ãã
// åã¨å¤ã®éã«ã¯ã³ãã³ãç½®ã(趣å³) // å¤å¤ãåºåããé¢æ°ã¯ãåºåããåãããããã«ã³ãã§åºåã£ã¦è¡¨ç¾ãã func f(a: int): int, int { return a, a * 2 // å¤å¤ãè¿ãã¦ããã } x, y := f(3) // x, yã¨ããã®ã¯å¤å¤ã表ãã¦ããã
å¤å¤ã¯ã«ã³ãã§åºåããããã®ã¨ãã¦è¡¨ç¾ããã¦ãã¾ããã
å¤å¤ãåãåãé¢æ°ãè¦ã¦ã¿ã¾ãããã
// a: int, b: int ã¨ããã®ã¯ãå¤å¤ãåãåãé¢æ°ã§ãããã¨ã表ãã¦ããã // é¢æ°ã®åºåã§å¤å¤ã表ãå ´åã¨åã表ç¾ã§ãããã¨ãåããã func add(a: int, b: int): int { return a + b } result := add(1, 2) // 1, 2ã¨ããã®ã¯å¤å¤ã表ãã¦ããã
å½ç¶ãå¤å¤ãè¿ãé¢æ°ã®çµæããã®ã¾ã¾å¤å¤ã渡ãé¢æ°ã«æ¸¡ãã¾ãã
result := add(f(3)) // å¤å¤ã渡ãé¢æ°ã«å¤å¤ãè¿ãé¢æ°ã®çµæããã®ã¾ã¾æ¸¡ãã¦ããã
ããã¾ã§ã¯ããæãã§ããã
å¤å¤ããããã¹ãã»ã»ã»ï¼
ãã¦ãå¤å¤ã¯ãã«ã³ãã§åºåãããåãªãè¤æ°ã®å¤ãã§ããã ãããããå¦æ³ããããªã£ã¦ããã¾ãã
// 4å¼æ°çaddãå®ç¾©ã func add4(a: int, b: int, c: int, d: int): int { return a + b + c + d } result := add4(1, 2, 3, 4) // 1, 2, 3, 4ã¯å¤å¤ã
ãã® add4
ã«å¯¾ãã¦ããããªå¼ã³åºãã¯ã©ãã§ããããã
res1 := add4(1, 2, f(3)) res2 := add4(1, f(2), 3) res3 := add4(f(1), f(2))
ãã©ãããã§ãããå¤å¤ã¯ãã«ã³ãã§åºåãããåãªãè¤æ°ã®å¤ãã§ããã ã§ãããªãã°ããããã«å±éããçµæãvalidã§ããã°validã¨ãããã¨ãã¦ãããã¨æãã¾ãããã ãããã ã»ã»ã»ï¼
ããããªå¤å¤ã®åããã«ãã
ãã¦ããã®æ¶ç©ºã®è¨èªã§ãããé¢æ°å¼ã³åºããè¦ã¦ãå¼æ°ã®åæ°ãåãããªãã¨ããåé¡ãããã¾ãã
res3 := add4(f(1), f(2)) // 2å¼æ°é¢æ°ã«è¦ãã
ä»ã©ãã®IDEãªããã³ã¼ãä¸ã«æ¸ããã¦ããªãæååã表示ããããããã£ã¦ã®ããã®ã§ãIDEåæã§ããã°ä½¿ããããããã¾ããã
// ä¸ã®ã³ã¼ãã¯ãIDEã§éãã¨ããè¦ãã(å®éã«æ¸ãã¦ãªãé¨åã¯ã°ã¬ã¼ã¢ã¦ã)ã res3 := add4(a, b=f(x=1), c,d=f(x=2))
ãããã¯ãé¢æ°åããæ°ãä»ããã°ããã»ã©åé¡ã«ã¯ãªããªãããããã¾ããã
ã¡ãªã¿ã«ãå®å¨ããGoã¨ããè¨èªã¯ãã®åé¡ã«è¶³ãçæ¹å ¥ãã¦ãã¾ãã
// Goè¨èª res := f(g()) // ãã¦ãfã¯ä½å¼æ°é¢æ°ã§ãããï¼
ããã¾ã§ãã£ãã®ã§ããã°ãããããªå¤å¤ã欲ãããªãã¾ãããï¼
å¯å¤é·å¼æ°ã¨å¤å¤ããããã¯å¯å¤é·æ»ãå¤ã¨å¤å¤
ããã¾ã§ããã¨ãè¡ãã¨ããã¾ã§è¡ã£ã¦ã¿ããæ°ããã¾ããã å¼æ°ãä½åã§ãæå®ã§ããé¢æ°ã¨ãããã®ãããã¾ãã
func sum(xs: ...int): int { res := 0 for _, x range xs { res += x } return res } result := sum(1, 2, 3)
ã§ã¯ãæ»ãå¤ãä½åã§ãæå®ã§ããé¢æ°ã¨ããã®ã¯ã©ãã§ããããã
func f(b: bool): ...int { if b { return 1, 2 } else { return 4, 5, 6 } }
ããã¯ãé常ã®æ段ã§ã¯åãåããªãé¢æ°ã¨ãªãã¾ãã ãã®çµæã使ãããã«ã¯ãå¯å¤é·å¼æ°ã®é¢æ°ãå¿ è¦ã§ãã
result := sum(f(true))
ã¾ãã¯ãã³ã³ãã¤ã«æå®æ°ã渡ãããå ´åã®ã¿åãåããããã«ããã®ãé¢ç½ããã§ã*8ã
a, b, c := sum(f(false)) // OK d, e, f := sum(f(true)) // ã³ã³ãã¤ã«ã¨ã©ã¼
ã¹ã©ã¤ã¹ã«å¤æããçµã¿è¾¼ã¿é¢æ°ãããã°(å¹ç以å¤ã¯)åé¡ããã¾ããã
xs := valuesToSlice(f(true)) ys := valuesToSlice(f(false))
ããã§ãå¤å¤ããã«ã³ãã§åºåãããåãªãè¤æ°ã®å¤ãã¨ãã¦ã¿ãªãã¦ãæãç«ã¡ããã ã¨ãããã¨ããªãã¨ãªãããã£ã¦ããã ãããã¨æãã¾ã(便å©ã ã¨ã¯è¨ã£ã¦ããªã)ã
ãã®ããã«ãå¤å¤ã¯å¤å¤ã§é¢ç½ãä¸çãåºãã£ã¦ããã®ã§ãã Goè¨èªã®å¤å¤ã¯å§ã¾ãã§ããããã¾ããã ã¿ãªãããèªåã ãã®å¤å¤ã®ä¸çãèãã¦ãã©ãã©ãå¤å¤ã®ãã°ããããä¸çã«çºä¿¡ãã¦ããã¾ãããï¼
åèãã¼ã¸
- Scheme:多値: Schemeã§ã®å¤å¤ã«ã¤ãã¦ã®è§£èª¬ãã¼ã¸ã
- なんでも継続: ç¶ç¶ã«ã¤ãã¦ã®è§£èª¬ãã¼ã¸ãæªå®ã
- 思考実験: returnを関数と思ってみる話: JavaScript風è¨èªã§returnãé¢æ°ã¨æã£ããã©ãããä¸çãããã®ãèå¯ãããã¼ã¸ã
- なぜ、多値関数は人気がないのだろう - 檜山正幸のキマイラ飼育記: å¤å¤ãæµè¡ã£ã¦ããªãçç±ã®èå¯ãã¼ã¸ã
- ä»ã«ããã£ããã©å¿ããã»ã»ã»
*1:ã¡ã¸ã£ã¼ãªè¨èªã ã¨Scheme/Common Lispã¨Goãããã§ã¯ãªãã§ãããããä½ãã¡ã¸ã£ã¼ã«å ¥ãããã¨ããåé¡ã¯ããã¾ãããâ»Luaãå¤å¤ãæã£ã¦ããããã§ããTwitterã§æãã¦ãããã¾ãããâ»GHCæ¡å¼µã«ãUnboxed Tuplesã¨ããæ§ææ¡å¼µãããããã§ãããããTwitterã§æãã¦ãããã¾ããã
*2:ã¯ã©ã¹ã®ãã£ã¼ã«ãã¨ãã¦æãããããã
*3:ãã ããä¸ã§ä¾ã«æããJavaä»®æ³ãã·ã³(ããä»ã®ä»®æ³ãã·ã³)ã§ã¯ãmåã®å¤ãç©ãã ç¶æ ã§é¢æ°ããreturnãããã¨ã許ãã¦ãã¾ããã
*4:http://www.kmonos.net/wlog/95.html#_1109090307 ã¨ããè¨äºãæãåºããã®ã§ç½®ãã¦ããã¾ãããã®è¨äºã§ã¯ãreturnãé¢æ°ã¨è¦ãªãã¨ã©ããªãã ãããã¨ããæèå®é¨ããã¦ãã¾ãã
*5:å¤å ¥åã¨å¤åºåã§æ»ãå¤ã®ä½ç½®ã«é¢æ°ãããããå¼æ°ã®ä½ç½®ã«é¢æ°ãããããå ¥ãæ¿ããã®ãé¢ç½ãã§ãããå対æ§ã¨ãããã¤ã§ããããã
*6:念ã®ãã: å®éã«ãããªã³ã¼ãã¯æ¸ãã¾ããã
*7:ããããåã®ã¨ã³ããªã§ãæ¸ããããã«ãGoè¨èªã¯å¤å¤ã£ã½ããã©å¤å¤ãããªããã®ãå ¥ãã¦ãã¾ã£ãã®ã§ããã¯ãããæ®å¿µã
*8:ãã ããã³ã³ãã¤ã«æéã¯ç¡è¦ãããã®ã¨ããã