Created
March 30, 2012 15:06
-
-
Save beala/2252161 to your computer and use it in GitHub Desktop.
Solution for the fib exercise on the Go language tour.
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
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
last := 0 | |
cur := 1 | |
count := 0 | |
return func() int { | |
switch count { | |
case 0: | |
count += 1 | |
return last | |
case 1: | |
count += 1 | |
return cur | |
} | |
cur, last = last+cur, cur | |
return cur | |
} | |
} | |
func main() { | |
f := fibonacci() | |
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