Last active
January 19, 2017 07:42
-
-
Save yicone/2cdd48a3fa8e85eb815d9db814943ee6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// fibonacci 函数会返回一个返回 int 的函数。 | |
func fibonacci() func() int { | |
n0 := 0 | |
n1 := 1 | |
return func() int { | |
n2 := n0 + n1 | |
n0 = n1 | |
n1 = n2 | |
return n2 | |
} | |
} | |
func main() {func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci() { | |
var n0 = 0 | |
var n1 = 1 | |
return function() { | |
n2 = n0 + n1 | |
n0 = n1 | |
n1 = n2 | |
return n2 | |
} | |
} | |
var f = fibonacci() | |
for (var i = 0; i < 10; i++) { | |
console.log(f()) | |
} |
golang 中,匿名函数的递归如何表达:
https://groups.google.com/forum/#!topic/golang-nuts/r295ChGvgPg
var fib_n func(n0, n1, n int) int
fib_n = func(n0, n1, n int) int {
if n == 0 {
return n0
} else {
return fib_n(n1, n0+n1, n-1)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
用闭包为函数传递香火