Created
September 15, 2020 12:40
-
-
Save oludouglas/4e06d9b3b02a0055abcc5cd938b1d683 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
//fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2) | |
func fib() func() int { | |
a, b := 0, 1 | |
return func() int { | |
defer func() { | |
a, b = b, a+b | |
}() | |
return a | |
} | |
} | |
func main() { | |
f := fib() | |
// Function calls are evaluated left-to-right. | |
for i := 0; i <= 4; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment