Last active
December 28, 2015 18:19
-
-
Save ascjones/7542675 to your computer and use it in GitHub Desktop.
tour.golang.org solutions
This file contains 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
/ ***********************************************************/ | |
/ * Exercise: Fibonacci closure, http://tour.golang.org/#44 */ | |
/ ***********************************************************/ | |
package main | |
import "fmt" | |
func fibonacci() func() int { | |
x, y := 0, 1 | |
return func() int { | |
x,y = y,x+y | |
if x == 1 && y == 1 { | |
return 0 | |
} | |
return x | |
} | |
} | |
func main() { | |
f := fibonacci2() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment