Skip to content

Instantly share code, notes, and snippets.

@yicone
Last active January 19, 2017 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yicone/2cdd48a3fa8e85eb815d9db814943ee6 to your computer and use it in GitHub Desktop.
Save yicone/2cdd48a3fa8e85eb815d9db814943ee6 to your computer and use it in GitHub Desktop.
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())
}
}
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())
}
@yicone
Copy link
Author

yicone commented Jan 19, 2017

用闭包为函数传递香火

@yicone
Copy link
Author

yicone commented Jan 19, 2017

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