A Tour of Go Exercise: Fibonacci closure

Let‘s have some fun with functions.

Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
    before := 0
    now := 0
    return func() int {
        if before == 0 {
            before++
            return before
        }
        if now == 0 {
            now++
            return now
        }
        temp := now
        now = before + now
        before = temp
        return now
    }
}

func main() {
    f := fibonacci()
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}
时间: 2024-08-06 22:44:16

A Tour of Go Exercise: Fibonacci closure的相关文章

Exercise: Fibonacci closure

Exercise: Fibonacci closure 题目: Let's have some fun with functions. Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers (0, 1, 1, 2, 3, 5, ...). 代码: package main import "fmt" // fibonacci

A Tour of Go Exercise: Images

Remember the picture generator you wrote earlier? Let's write another one, but this time it will return an implementation of image.Image instead of a slice of data. Define your own Image type, implement the necessary methods, and callpic.ShowImage. B

A Tour of Go Exercise: HTTP Handlers

Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server. type String string type Struct struct { Greeting string Punct string Who string } For example, you should be able to regist

A Tour of Go Exercise: Loops and Functions

As a simple way to play with functions and loops, implement the square root function using Newton's method. In this case, Newton's method is to approximate Sqrt(x) by picking a starting point z and then repeating: To begin with, just repeat that calc

A Tour of Go Exercise: Errors

Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers. Create a new type type ErrNegativeSqrt float64 an

A Tour of Go Exercise: Slices

Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice

Exercise: Maps (单词统计)

A Tour of Go Exercise: Maps https://tour.golang.org/moretypes/23 WordCount (单词统计) 是一个很经典的小程序了,在很多编程入门教程中都会出现. 这道题比较简单,但也有一些知识点值得一提. 上面这个答案我是参考了网上别人写的.但在参考别人之前我也自己解题了,其中,唯一不同之处是这一句: m[word]++ 我本来写的是: _, ok := m[word] if ok { m[word]++ } else { m[word]

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

Golang,开车上路。

2017年1月29日,开始学习 golang, 一边学习一边做笔记. ◆ Golang 的开发环境非常容易搭建,双击安装包安装后,设置一个系统变量即可. ◆ 利用当前时间来做随机数的种子. ◆ 教程里说: A nil map has no keys, nor can keys be added. 但是没有给出具体的例子,于是我做了以下试验: var m map[string]int // 构建一个空map m["abc"] = 123 // 对空map添加key fmt.Println