Created
May 11, 2019 06:00
-
-
Save tae0y/55cfab62d32d08ebc28d8f88a5714118 to your computer and use it in GitHub Desktop.
gotour fibonacci clousure
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 { | |
fibo, prev, tmp := 0, 1, 0 | |
return func() int { | |
tmp = fibo | |
fibo += prev | |
prev = tmp | |
return fibo | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} | |
// 문제 푸는 데 1시간, 초기값을 1로 주고 시작했더니 f(0)값이 안나와서 헤매다가 | |
// 초기값을 0, 증가값을 1로 초기화해서 해결 ..... 으 | |
// 고민 : fibo를 상위 함수 지역변수로 넣을 필요가 있을까? 그렇게 안하려면?? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BETTER CODE
순서가 엇갈린 수열을 두개 만들기, 초기값1 증가값0
f를 하위함수 내부에 선언할 수 있게 되었다