2012-09-02 02:33:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// fibonacci is a function that returns
|
|
|
|
// a function that returns an int.
|
|
|
|
func fibonacci() func() int {
|
2012-11-06 02:00:35 +00:00
|
|
|
prev := 0
|
|
|
|
cur := 1
|
|
|
|
return func() int {
|
|
|
|
old_cur := cur
|
|
|
|
cur = prev + cur
|
|
|
|
prev = old_cur
|
|
|
|
return cur
|
|
|
|
}
|
2012-09-02 02:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2012-11-06 02:00:35 +00:00
|
|
|
f := fibonacci()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
fmt.Println(f())
|
|
|
|
}
|
2012-09-02 02:33:20 +00:00
|
|
|
}
|